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.

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.

7 Responses to “Quick Delicious API Ruby script”

  1. Justin Says:
    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.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    
    require 'open-uri'
    require 'rexml/document'
    require 'cgi'
    print 'Enter Tag: '
    open('http://del.icio.us/api/posts/all?&tag=' << CGI::escape(gets.strip), :http_basic_authentication =>  ["m4dc4p", "s0n1cuth"]) do |f|
      doc = REXML::Document.new f.read
      if doc.elements["posts/post"].nil?
        puts 'No tags found.'
      else
        doc.elements.each("posts/post") {|element| puts element.attributes["href"] }
      end
    end
    
  2. TAD Says:
    Thanks Justin! That's exactly the kind of stuff I wanted to see.
  3. Fikyjala Says:

    Greetings!.. charter fishing+ florida offshore

  4. Wycqoxox Says:

    big davia pizza sausage big davia pizza sausage

  5. Sjcoursa Says:

    Egyptian burial chamber Egyptian burial chamber Burial record Burial ford president Burial service Burial grounds

  6. Andrey Says:

    Thanks for advice. Now I’m looking for integration ruby & delicious. And your article was helpful. Thanks.

  7. TAhijggg Says:

    difference between valium and xanax difference between valium and xanax

Leave a Reply