ActiveResource is a great concept which consumes rails-style REST API but unfortunately most of the REST API's are not rails-style. This means that very frequently you will end up modifying ActiveResource to consume non rails-style REST API's. This article is about understanding ActiveResource and how to tweak/extend it to consume non rails-style REST API's. We will mainly concentrate on reading data i.e. the GET method.
Active Youtube 75
- Namespace in class names: Video, User, StandardFeed and Playlist classes have been moved to "Youtube" module, to prevent any conflicts with your ActiveRecord models.
- CustomMethods related change: In last version, only response from "find" was converting "entry" object to array of "entry" object. Now, the same behavior is implemented for custom http calls like Video.find().get(:comments)
- Small patch for better namespacing: Its basically some code from the rails trunk on ActiveResource, for better handling of namespaces while creating ActiveResource objects.
Gem Installation:
sudo gem install active_youtube |
Example Usage:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#### Video ## search for videos on 'ruby' search = Youtube::Video.find(:first, :params => {:vq => 'ruby', :"max-results" => '5'}) puts search.entry.length ## video information of id = ZTUVgYoeN_o vid = Youtube::Video.find("ZTUVgYoeN_o") puts vid.group.content[0].url ## video comments comments = Youtube::Video.find_custom("ZTUVgYoeN_o").get(:comments) puts comments.entry[0].link[2].href ## searching with category/tags results = Youtube::Video.search_by_tags("Comedy") puts results[0].entry[0].title #### STANDARDFEED ## retrieving standard feeds most_viewed = Youtube::Standardfeed.find(:most_viewed, :params => {:time => 'today'}) puts most_viewed.entry[0].group.content[0].url #### USER ## user's profile - guthrie user_profile = Youtube::User.find("guthrie") puts user_profile.link[1].href #### PLAYLIST ## get playlist - multiple elements in playlist playlist = Youtube::Playlist.find("EBF5D6DC4589D7B7") puts playlist.entry[0].group.content[0].url |
ActiveResource and YouTube
This article is about consuming YouTube API in your Ruby/Rails project using ActiveResource. Moreover, this article is an example of how to extend ActiveResource to consume non rest-style API.
Benefits of using our extension to ActiveResource :
- ActiveResource provides a ActiveRecord style interface.
- You can modify our extension according to your interface requirement.
- No not need to use and rely on Ruby library for YouTube REST API.
HTML::Tag class in scrapi
While working with scrapi, I found there is no external documentation for HTML::Tag class. This article is to ensure no one says this again.
In scrapi, HTML::Node represents a html node which can be of 2 types: HTML::Text and HTML::Tag for a text node and html tag node respectively. Here is a code snippet in which scrapi returns the html node as a HTML::Tag object.
QScraper : hpricot interface to scrapi
QScraper is a wrapper over scrapi to provide Hpricot like interface.
Motivation: Hpricot interface is simple and easy to use while scrapi is more powerful because of bundle scraping and anonymous classes. I was using hpricot for quick testing and checking but scrapi for project implementation. To avoid working with two html scrapers, I wrote this wrapper over scrapi.
Bundle Scraping: It refers to extraction of multiple attributes of an element from a web page in a single parse. Most screen scraping tools extract only multiple elements but not multiple attributes of an element. Lets take an example of blog scraping, each blog post would be an element and I would like to extract multiple attributes of blog post like info about author, published on, title and content. Rather than making individual calls like doc.search(author_selector), doc.search(published_selector) etc., I would like to do doc.find(author_selector, date_selector, title ...).
