Joe Williams home
A while back I wrote some code in Erlang to grab your public Amazon wishlist based off of your email address. Today I had the urge to rewrite it using OTP, specifically gen_server. The code is up on github. Switching it over to use gen_server is fairly trivial since the sequential code is already written. Basically all that needs to be done is to setup the handle_call method to send any email addresses over to the get_wishlist method for retrieval. Here's what the handle_call looks like:
handle_call(Email_addr, _From, State) -> %Reply back with the result from get_wishlist Reply = get_wishlist(Email_addr), {reply, Reply, State}.
The rest of the functions look pretty much as they did before so I won't put them here. One other change I made was to start_link to have it also run inets:start() so we can make the http requests in the other functions. Here's how the thing works:
[zeusfaber@der-dieb ebin]$ erl Erlang (BEAM) emulator version 5.6.5 [source] [64-bit] [smp:2] [async-threads:0] [hipe] [kernel-poll:false] Eshell V5.6.5 (abort with ^G) 1> c('../src/amazon_wishlist_otp.erl'). {ok,amazon_wishlist_otp} 2> amazon_wishlist_otp:start_link(). ok 3> gen_server:call(amazon_wishlist_otp, ""). [["Algorithms on Strings, Trees and Sequences: Computer Science and Computational Biology", "Algorithms: Sequential, Parallel, and Distributed", "Starting Forth: An Introduction to the Forth Language and Operating System for Beginners and Professionals (Prentice-Hall Software Series)", "Biobazaar: The Open Source Revolution and Biotechnology", "Nmap Network Scanning: The Official Nmap Project Guide to Network Discovery and Security Scanning", "American Nerd: The Story of My People", "FreeDarko presents The Macrophenomenal Pro Basketball Almanac: Styles, Stats, and Stars in Today's Game", "CP Technologie USB TO SERIAL ADAPTER ( CP-US-03 )", "Made to Stick: Why Some Ideas Survive and Others Die"]]
Fork me on GitHub