One of the ideas that the people that I use to chat with about web development told me, was to use the del.icio.us API to publish, in my site, my last bookmarked links. In this post I want to tell you how you can do something quite like I have on my home.
The first thing you need to do is open the controller that manages the view where you'll put the bookmarks, and in the desired method of that controller, put the following code:
require 'net/https'
require "rexml/document"
The first one will help to connect to del.icio.us API, and the second one will help on the XML parsing of the data outputed by the API. So, let's start by connecting to the del.icio.us API. To accomplish this an authentication is needed via SSL, so you'll have to pass an username and password, the ones of your account.
@delicious = ""
http = ""
resp = ""
http = Net::HTTP.new('api.del.icio.us', 443)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Get.new('/v1/posts/recent?&count=7')
request.basic_auth 'putyourusernamehere', 'putyourpasswordhere'
response = http.request(request)
response.value
resp = response.body
end
By default this action will try to retrieve the last 7 bookmarked links on del.icio.us. Don't forget to substitute the putyour... words by your own username and password.
But, as you can see in the del.icios.us API site, there are much more commands that you would possibly want to execute, besides the last 7 bookmarked links. The method of retrieving should be the same for every action. Besides that, see on the API documentation the optional or required arguments that each command has. In recent?, for example, I defined the argument &count=7 to define a specific number of bookmarks returned. If none defined it'd retrieve 15.
Now that we have our output stored in the variable resp, it's time to do the parsing and put the final result on our webpage.
doc = REXML::Document.new(resp)
doc.elements.each("*/post") { |element| @delicious += "<a href=\"" + element.attributes["href"] + "\">" + element.attributes["description"] + "</a><br />" }
First, we're declaring a new document based on the output received and storaged in the variable resp. Then, we're sliding through each item post (we used */ for not having to declare all the path to the post item). Then, to access each element's attributes we have only to element.attributes["attribute name"]. I saved the result on a @delicious variable with all the html that I needed to output my last bookmarks as links to the links themselves and the required line breaks.
Everything is done now, you just have to put <%= @delicious %> on your web page and all should be fine.
But, and what if the host is unavailable or the parsing fails? We've to catch those exceptions, so add this final code to the end of your controller's method:
rescue SocketError
@delicious = "Host unavailable"
rescue REXML::ParseException => e
@delicious = "error parsing XML " + e.to_s
Try it and enoy it.
The first thing you need to do is open the controller that manages the view where you'll put the bookmarks, and in the desired method of that controller, put the following code:
require 'net/https'
require "rexml/document"
The first one will help to connect to del.icio.us API, and the second one will help on the XML parsing of the data outputed by the API. So, let's start by connecting to the del.icio.us API. To accomplish this an authentication is needed via SSL, so you'll have to pass an username and password, the ones of your account.
@delicious = ""
http = ""
resp = ""
http = Net::HTTP.new('api.del.icio.us', 443)
http.use_ssl = true
http.start do |http|
request = Net::HTTP::Get.new('/v1/posts/recent?&count=7')
request.basic_auth 'putyourusernamehere', 'putyourpasswordhere'
response = http.request(request)
response.value
resp = response.body
end
By default this action will try to retrieve the last 7 bookmarked links on del.icio.us. Don't forget to substitute the putyour... words by your own username and password.
But, as you can see in the del.icios.us API site, there are much more commands that you would possibly want to execute, besides the last 7 bookmarked links. The method of retrieving should be the same for every action. Besides that, see on the API documentation the optional or required arguments that each command has. In recent?, for example, I defined the argument &count=7 to define a specific number of bookmarks returned. If none defined it'd retrieve 15.
Now that we have our output stored in the variable resp, it's time to do the parsing and put the final result on our webpage.
doc = REXML::Document.new(resp)
doc.elements.each("*/post") { |element| @delicious += "<a href=\"" + element.attributes["href"] + "\">" + element.attributes["description"] + "</a><br />" }
First, we're declaring a new document based on the output received and storaged in the variable resp. Then, we're sliding through each item post (we used */ for not having to declare all the path to the post item). Then, to access each element's attributes we have only to element.attributes["attribute name"]. I saved the result on a @delicious variable with all the html that I needed to output my last bookmarks as links to the links themselves and the required line breaks.
Everything is done now, you just have to put <%= @delicious %> on your web page and all should be fine.
But, and what if the host is unavailable or the parsing fails? We've to catch those exceptions, so add this final code to the end of your controller's method:
rescue SocketError
@delicious = "Host unavailable"
rescue REXML::ParseException => e
@delicious = "error parsing XML " + e.to_s
Try it and enoy it.