×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Ruby
Posted by: Joshua Binder
Added: Mar 19, 2013 2:02 PM
Views: 22
Tags: no tags
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.
YouTube references

http://code.google.com/apis/youtube/developers_guide_protocol.html http://code.google.com/apis/youtube/reference.html
  1. require 'rubygems'
  2. require 'activeresource'
  3.    
  4. class ActiveYouTube < ActiveResource::Base
  5.   class << self
  6.   ## Remove format from the url.
  7.   def element_path(id, prefix_options = {}, query_options = nil)
  8.     prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  9.     "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
  10.   end
  11.    
  12.   ## Remove format from the url.
  13.   def collection_path(prefix_options = {}, query_options = nil)
  14.     prefix_options, query_options = split_options(prefix_options) if query_options.nil?
  15.     "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
  16.   end
  17.  
  18.   ## For a collection call, ActiveResource formatting is not
  19.   ## compliant with YouTube's output.
  20.   def instantiate_collection(collection, prefix_options = {})
  21.     unless collection.kind_of? Array
  22.       [instantiate_record(collection, prefix_options)]
  23.     else
  24.       collection.collect! { |record| instantiate_record(record, prefix_options) }
  25.     end
  26.   end
  27.  
  28.   ## To convert output into proper standard.
  29.   ## If single element is present in output then entry is not an array.
  30.   ## So this method will ensure entry is always an array.
  31.   alias :old_find :find
  32.   def find(*args)
  33.     output=old_find(*args)
  34.     if output.respond_to?:entry and !(output.entry.kind_of? Array)
  35.       output.entry=[output.entry]
  36.     end
  37.     output
  38.   end
  39.  
  40.   ## When using ActiveResource::CustomMethods, ActiveResource first tries to retrieve the id using find()
  41.   ## and then makes a get() call using that id.
  42.   ## But, youtube returns the url of this item as id, which we don't want. This method overrides the behavior.
  43.   ## Example: comments = Video.find_custom("ZTUVgYoeN_o").get(:comments)
  44.   def find_custom(arg)
  45.     object = self.new
  46.     object.id = arg
  47.     object
  48.   end
  49.  
  50.   ##  Following method from ActiveResource::CustomMethods extends the capabilities of activeresource for non-standard urls ;-)
  51.   ##  The objects returned from this method are not automatically converted into ActiveResource instances - they are ordinary Hashes.
  52.   ##  Modifications below ensures that you get ActiveResource instances.
  53.   def get(method_name, options = {})
  54.     object_array = connection.get(custom_method_collection_url(method_name, options), headers)
  55.     if object_array.class.to_s=="Array"
  56.       object_array.collect! {|record| self.class.new.load(record)}
  57.     else
  58.       self.class.new.load(object_array)
  59.     end
  60.   end
  61.   end
  62.  
  63.   ## Instance Methods: (modifying the ActiveRecord::CustomMethods).
  64.  
  65.   ## This modification is same as defined in above method
  66.   def get(method_name, options = {})
  67.     self.class.new.load(connection.get(custom_method_element_url(method_name, options), self.class.headers))
  68.   end
  69.  
  70.   ## Modifying the url formation to make it Youtube API complaint
  71.   def custom_method_element_url(method_name, options = {})    
  72.     "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/" +
  73.     "#{method_name}#{self.class.send!(:query_string, options)}"
  74.   end
  75. end
  76.