Class: ActiveYoutube
- Inherits:
-
ActiveResource::Base
- Object
- ActiveResource::Base
- ActiveYoutube
- Defined in:
- lib/activeyoutube.rb
Overview
module Youtube
Direct Known Subclasses
Youtube::Playlist, Youtube::Standardfeed, Youtube::User, Youtube::Video
Class Method Summary collapse
-
.collection_path(prefix_options = {}, query_options = nil) ⇒ Object
Remove format from the url.
- .convert_entry_to_array(object) ⇒ Object
-
.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object
Remove format from the url.
- .find(*args) ⇒ Object
-
.find_custom(arg) ⇒ Object
When using ActiveResource::CustomMethods, ActiveResource first tries to retrieve the id using find() and then makes a get() call using that id.
-
.get(method_name, options = {}) ⇒ Object
Following method from ActiveResource::CustomMethods extends the capabilities of activeresource for non-standard urls ;-) The objects returned from this method are not automatically converted into ActiveResource instances - they are ordinary Hashes.
-
.instantiate_collection(collection, prefix_options = {}) ⇒ Object
For a collection call, ActiveResource formatting is not compliant with YouTube’s output.
-
.old_find ⇒ Object
To convert output into proper standard.
Instance Method Summary collapse
-
#custom_method_element_url(method_name, options = {}) ⇒ Object
Modifying the url formation to make it Youtube API complaint.
-
#get(method_name, options = {}) ⇒ Object
This modification is same as defined in above method.
Class Method Details
.collection_path(prefix_options = {}, query_options = nil) ⇒ Object
Remove format from the url.
14 15 16 17 |
# File 'lib/activeyoutube.rb', line 14 def collection_path( = {}, = nil) , = () if .nil? "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}" end |
.convert_entry_to_array(object) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/activeyoutube.rb', line 59 def convert_entry_to_array object if object.respond_to?:entry and !(object.entry.kind_of? Array) object.entry=[object.entry] end object end |
.element_path(id, prefix_options = {}, query_options = nil) ⇒ Object
Remove format from the url.
8 9 10 11 |
# File 'lib/activeyoutube.rb', line 8 def element_path(id, = {}, = nil) , = () if .nil? "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}" end |
.find(*args) ⇒ Object
33 34 35 |
# File 'lib/activeyoutube.rb', line 33 def find(*args) convert_entry_to_array(old_find(*args)) end |
.find_custom(arg) ⇒ Object
When using ActiveResource::CustomMethods, ActiveResource first tries to retrieve the id using find() and then makes a get() call using that id. But, youtube returns the url of this item as id, which we don’t want. This method overrides the behavior. Example: comments = Video.find_custom(“ZTUVgYoeN_o”).get(:comments)
41 42 43 44 45 |
# File 'lib/activeyoutube.rb', line 41 def find_custom(arg) object = self.new object.id = arg object end |
.get(method_name, options = {}) ⇒ Object
Following method from ActiveResource::CustomMethods extends the capabilities of activeresource for non-standard urls ;-)
The objects returned from this method are not automatically converted into ActiveResource instances - they are ordinary Hashes.
Modifications below ensures that you get ActiveResource instances.
50 51 52 53 54 55 56 57 |
# File 'lib/activeyoutube.rb', line 50 def get(method_name, = {}) object_array = connection.get(custom_method_collection_url(method_name, ), headers) if object_array.class.to_s=="Array" object_array.collect! {|record| convert_entry_to_array(self.class.new.load(record))} else convert_entry_to_array(self.class.new.load(object_array)) end end |
.instantiate_collection(collection, prefix_options = {}) ⇒ Object
For a collection call, ActiveResource formatting is not compliant with YouTube’s output.
21 22 23 24 25 26 27 |
# File 'lib/activeyoutube.rb', line 21 def instantiate_collection(collection, = {}) unless collection.kind_of? Array [instantiate_record(collection, )] else collection.collect! { |record| instantiate_record(record, ) } end end |
.old_find ⇒ Object
To convert output into proper standard. If single element is present in output then entry is not an array. So this method will ensure entry is always an array.
32 |
# File 'lib/activeyoutube.rb', line 32 alias :old_find :find |
Instance Method Details
#custom_method_element_url(method_name, options = {}) ⇒ Object
Modifying the url formation to make it Youtube API complaint
75 76 77 78 |
# File 'lib/activeyoutube.rb', line 75 def custom_method_element_url(method_name, = {}) "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/" + "#{method_name}#{self.class.send!(:query_string, options)}" end |
#get(method_name, options = {}) ⇒ Object
This modification is same as defined in above method
70 71 72 |
# File 'lib/activeyoutube.rb', line 70 def get(method_name, = {}) self.class.convert_entry_to_array(load(connection.get(custom_method_element_url(method_name, ), self.class.headers))) end |