fallenrogue.com

ma.gnolia Blossom in Ruby on Rails

Since this was pretty easy to implement in RoR I thought I would go over some of the finer points of making a call to a REST service in Rails. You may have noticed a new set of images below my tag cloud on the main page. That's the magnoliaBlossom that I threw in. The good people at Ma.gnolia (a social bookmarking service) have just released a new api for it which includes…screenshots of your favorite pages!!! Woo-hoo! So, I figured, why not have an image based blog-roll from magnolia. A feature that they are lovingly calling a Blossom on their site. Here's what I did in Rails to get that sucka working…

First I created a model to represent that class that we were going to be using. I called it MagnoliaBlossom. I know, I know… creative, right?  Anyway, once you've got the model, you can start filling it in. 

 

#uses the Ma.gnolia API to share images of websites the user has bookmarked.
require 'net/http'
require 'uri'
require 'time'
require 'rexml/document'

class MagnoliaBlossom < ActiveRecord::Base
  include REXML
 
  attr_accessor :bookmarks
 

  #Small Struct that will hold our objects
  class Bookmark < Struct.new(:url, :screenshot, :title, :description)
    def to_s
      "Title: #{@title}"
    end
    def date=(value); super(Time.parse(value)) end
  end
 
  def initialize(limit)
    @limit = limit
    @uri = URI.parse('http://ma.gnolia.com/api/rest/1/bookmarks_find')
    @bookmarks = []
    self.make_the_call
  end
 
  protected
 
  def make_the_call
    res = Net::HTTP.post_form(@uri,{'person'=>ENV['Magnolia_Username'], 'limit'=>@limit, 'api_key'=>ENV['Magnolia_Key']})
    parse(res.body)
  end
 
  def parse(xml_body)
    xml = Document.new(xml_body)
    
    XPath.each(xml, "//bookmarks/bookmark/") do |elem|
     
      bm = Bookmark.new
      bm.url = XPath.match(elem, "url/text()").to_s
      bm.screenshot = XPath.match(elem, "screenshot/text()").to_s
      bm.title = XPath.match(elem, "title/text()").to_s
      bm.description = XPath.match(elem, "description/text()").to_s
      
      @bookmarks << bm
    end
  end
end

 

Granted, in this example I didn't have to inherit from ActiveRecord in order to get this work. I only do that out of habit these days. A plain Jane class would be just fine. Anyway, in the constructor you can pass this bad boy a value for how many items you'd like to return and presto…you've got a ma.gnolia blossom of your very own.

NOTES: Be sure that you have 2 ENV variables for the username and the API Key that's needed when making calls to Ma.gnolia. Otherwise this whole thing won't work. 

There you have it. A simple way to query a REST service using POST. Please feel free to help me improve this code with comments and questions. Thanks! 


articleStats

Here are some silly little facts about this ma.gnolia Blossom in Ruby on Rails...

It was written by Leon over 2 years ago.
It has 4113 letters in it.
It has 439 words in it.
It has a total of 1 comments in all.
So far FexextOrieria has the last word!

article Links

These are the links that appear in this article. They probably don't make sense out of context... but just in case. :)

RoR
 

The other stuff...

What the kids are saying...

19 days ago FexextOrieria said...

http://golegole.com
7YUIjhred453Dfkjhew

Leave a comment
*name:
*email: (never sold or published.)
url :

©2000-2008 fallenrogue.com | Some Rights reserved.