Quick Delicious API Ruby script
April 19th, 2006
One of the first projects that I have planned for Ruby on Rails involves calling the del.icio.us API and formatting results tailored to each user's desires. Actually, Lindsay thought the whole idea up, and it probably won't make much sense until we debut it "sometime soon."
My first task was just figuring out how to call one of the APIs through Ruby. After a lot of detective work, I figured out a first, very rough go at it.
This page contains the information for how to call del.icio.us' API. It's a REST API and the best examples that I can find for calling this sort of API use the open-uri ruby libraries.
I started trying to figure this out by googling "delicious api ruby" and quickly found this page. This more or less set me on the right path. I took special notice of the first comment because it seemed like a much cleaner way to handle the call, but I wasn't sure how to handle the parameters, like the tag parameter you can see in the url in the code above.
At this point, I googled for open-uri and found the page linked above. I did some more googling and found this excellent example. Amit's code combined with the :basic_authentication example pushed me over the top. There was still one missing piece though.
Amit uses the rexml/document library to query the results. I couldn't quite figure out how to properly query my results - I tried the pattern he used and just couldn't get it to work. After just one more google, I found the rexml tutorial page. This explains exactly how to access any part of the xml coming back from an API like this. Perfect! This led to the completion of the code you see above.
If you use the code snippet, remember to stick in your delicious userid and password. When you run the script from the console, it uses gets to get the tag which it then appends to the API call. Then I call the open method from the open-uri library to make the call. Then in the following block, I use the results to create an xml document which I then iterate through and spit out some of the results.
I'm really amazed at how effecient and elegant Ruby is in performing this sequence of events. A similar Java or c# application would easily be 3 or 4 times as many lines of code and wouldn't be any easier to read.
As pleased as I am with myself, though, I get the feeling that there may be an even easier, simpler way of doing this. If someone knows a better way to handle REST API calls, please let me know!!
UPDATE: I've added a video demonstration of a Ruby on Rails application that I built around this script. Check it out here.
My first task was just figuring out how to call one of the APIs through Ruby. After a lot of detective work, I figured out a first, very rough go at it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
require 'open-uri' require "rexml/document" include REXML user = "your_login" pass = "your_password" url='http://del.icio.us/api/posts/all?&tag=' puts 'Enter Tag: ' tag=gets print url+tag open(url+tag, :http_basic_authentication => [user, pass] ) do |f| result = f.read doc = Document.new result doc.elements.each("posts/post") {|element| puts element.attributes["href"] } end |
This page contains the information for how to call del.icio.us' API. It's a REST API and the best examples that I can find for calling this sort of API use the open-uri ruby libraries.
I started trying to figure this out by googling "delicious api ruby" and quickly found this page. This more or less set me on the right path. I took special notice of the first comment because it seemed like a much cleaner way to handle the call, but I wasn't sure how to handle the parameters, like the tag parameter you can see in the url in the code above.
At this point, I googled for open-uri and found the page linked above. I did some more googling and found this excellent example. Amit's code combined with the :basic_authentication example pushed me over the top. There was still one missing piece though.
Amit uses the rexml/document library to query the results. I couldn't quite figure out how to properly query my results - I tried the pattern he used and just couldn't get it to work. After just one more google, I found the rexml tutorial page. This explains exactly how to access any part of the xml coming back from an API like this. Perfect! This led to the completion of the code you see above.
If you use the code snippet, remember to stick in your delicious userid and password. When you run the script from the console, it uses gets to get the tag which it then appends to the API call. Then I call the open method from the open-uri library to make the call. Then in the following block, I use the results to create an xml document which I then iterate through and spit out some of the results.
I'm really amazed at how effecient and elegant Ruby is in performing this sequence of events. A similar Java or c# application would easily be 3 or 4 times as many lines of code and wouldn't be any easier to read.
As pleased as I am with myself, though, I get the feeling that there may be an even easier, simpler way of doing this. If someone knows a better way to handle REST API calls, please let me know!!
UPDATE: I've added a video demonstration of a Ruby on Rails application that I built around this script. Check it out here.
June 10th, 2007 at 07:21 PM Not a bad script. I updated it to be more "ruby-like" and also added URL encoding to the tag entered. I mostly removed unnecessary local variables and added some basic checking to indicate no tags were found. I also used the 'print' instead of the 'puts' function to display the prompt. Notice the use of gets in the open() call - no need to stick it in its own variable.
June 10th, 2007 at 07:21 PM Thanks Justin! That's exactly the kind of stuff I wanted to see.
August 28th, 2007 at 09:58 PM
Greetings!.. charter fishing+ florida offshore
October 4th, 2007 at 08:13 PM
big davia pizza sausage big davia pizza sausage
October 14th, 2007 at 06:20 PM
Egyptian burial chamber Egyptian burial chamber Burial record Burial ford president Burial service Burial grounds
October 21st, 2007 at 11:47 PM
Thanks for advice. Now I’m looking for integration ruby & delicious. And your article was helpful. Thanks.
October 29th, 2007 at 11:59 AM
difference between valium and xanax difference between valium and xanax