active_youtube 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,54 @@
1
+ ##### Examples #######
2
+
3
+ #### VIDEO
4
+ ## search for videos
5
+ search = Youtube::Video.find(:first, :params => {:vq => 'ruby', :"max-results" => '5'})
6
+ puts search.entry.length
7
+
8
+ ## video information of id = ZTUVgYoeN_o
9
+ vid = Youtube::Video.find("ZTUVgYoeN_o")
10
+ puts vid.group.content[0].url
11
+
12
+ ## video comments
13
+ comments = Youtube::Video.find_custom("ZTUVgYoeN_o").get(:comments)
14
+ puts comments.entry[0].link[2].href
15
+
16
+ ## searching with category/tags
17
+ results = Youtube::Video.search_by_tags("Comedy")
18
+ puts results[0].entry[0].title
19
+ # more examples:
20
+ # Video.search_by_tags("Comedy", "dog")
21
+ # Video.search_by_tags("News","Sports","football", :exclude=>["Comedy","soccer"])
22
+ # Video.search_by_tags("News","Sports","football", :exclude=>"soccer")
23
+
24
+
25
+ #### STANDARDFEED
26
+ ## retrieving standard feeds
27
+ most_viewed = Youtube::Standardfeed.find(:most_viewed, :params => {:time => 'today'})
28
+ puts most_viewed.entry[0].group.content[0].url
29
+
30
+ #### USER
31
+ ## user's profile - guthrie
32
+ user_profile = Youtube::User.find("guthrie")
33
+ puts user_profile.link[1].href
34
+
35
+ ## user's playlist - john
36
+ user_playlist = Youtube::User.find_custom("john").get(:playlists)
37
+ puts user_playlist.link[1].href
38
+
39
+ ## user's upload or favorites
40
+ rick_video = Youtube::User.find_custom("rick").get(:uploads)
41
+ puts rick_video.entry[0].group.content[0].url
42
+
43
+ ## user's subscription
44
+ user_subscriptions = Youtube::User.find_custom("guthrie").get(:subscriptions)
45
+ puts user_subscriptions.to_yaml
46
+
47
+ #### PLAYLIST
48
+ ## get playlist - multiple elements in playlist
49
+ playlist = Youtube::Playlist.find("EBF5D6DC4589D7B7")
50
+ puts playlist.entry[0].group.content[0].url
51
+
52
+ ## get playlist - single element in playlist
53
+ playlist = Youtube::Playlist.find("45C563323B344971")
54
+ puts playlist.entry[0].group.content[0].url
@@ -0,0 +1,5 @@
1
+ require 'rubygems'
2
+ require 'active_resource'
3
+ require File.expand_path(File.dirname(__FILE__) + "/patch")
4
+ require File.expand_path(File.dirname(__FILE__) + "/activeyoutube")
5
+ require File.expand_path(File.dirname(__FILE__) + "/youtube")
@@ -0,0 +1,80 @@
1
+ require 'rubygems'
2
+ require 'activeresource'
3
+
4
+ #module Youtube
5
+ class ActiveYoutube < ActiveResource::Base
6
+ class << self
7
+ ## Remove format from the url.
8
+ def element_path(id, prefix_options = {}, query_options = nil)
9
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
10
+ "#{prefix(prefix_options)}#{collection_name}/#{id}#{query_string(query_options)}"
11
+ end
12
+
13
+ ## Remove format from the url.
14
+ def collection_path(prefix_options = {}, query_options = nil)
15
+ prefix_options, query_options = split_options(prefix_options) if query_options.nil?
16
+ "#{prefix(prefix_options)}#{collection_name}#{query_string(query_options)}"
17
+ end
18
+
19
+ ## For a collection call, ActiveResource formatting is not
20
+ ## compliant with YouTube's output.
21
+ def instantiate_collection(collection, prefix_options = {})
22
+ unless collection.kind_of? Array
23
+ [instantiate_record(collection, prefix_options)]
24
+ else
25
+ collection.collect! { |record| instantiate_record(record, prefix_options) }
26
+ end
27
+ end
28
+
29
+ ## To convert output into proper standard.
30
+ ## If single element is present in output then entry is not an array.
31
+ ## So this method will ensure entry is always an array.
32
+ alias :old_find :find
33
+ def find(*args)
34
+ convert_entry_to_array(old_find(*args))
35
+ end
36
+
37
+ ## When using ActiveResource::CustomMethods, ActiveResource first tries to retrieve the id using find()
38
+ ## and then makes a get() call using that id.
39
+ ## But, youtube returns the url of this item as id, which we don't want. This method overrides the behavior.
40
+ ## Example: comments = Video.find_custom("ZTUVgYoeN_o").get(:comments)
41
+ def find_custom(arg)
42
+ object = self.new
43
+ object.id = arg
44
+ object
45
+ end
46
+
47
+ ## Following method from ActiveResource::CustomMethods extends the capabilities of activeresource for non-standard urls ;-)
48
+ ## The objects returned from this method are not automatically converted into ActiveResource instances - they are ordinary Hashes.
49
+ ## Modifications below ensures that you get ActiveResource instances.
50
+ def get(method_name, options = {})
51
+ object_array = connection.get(custom_method_collection_url(method_name, options), headers)
52
+ if object_array.class.to_s=="Array"
53
+ object_array.collect! {|record| convert_entry_to_array(self.class.new.load(record))}
54
+ else
55
+ convert_entry_to_array(self.class.new.load(object_array))
56
+ end
57
+ end
58
+
59
+ def convert_entry_to_array object
60
+ if object.respond_to?:entry and !(object.entry.kind_of? Array)
61
+ object.entry=[object.entry]
62
+ end
63
+ object
64
+ end
65
+ end
66
+
67
+ ## Instance Methods: (modifying the ActiveRecord::CustomMethods).
68
+
69
+ ## This modification is same as defined in above method
70
+ def get(method_name, options = {})
71
+ self.class.convert_entry_to_array(load(connection.get(custom_method_element_url(method_name, options), self.class.headers)))
72
+ end
73
+
74
+ ## Modifying the url formation to make it Youtube API complaint
75
+ def custom_method_element_url(method_name, options = {})
76
+ "#{self.class.prefix(prefix_options)}#{self.class.collection_name}/#{id}/" +
77
+ "#{method_name}#{self.class.send!(:query_string, options)}"
78
+ end
79
+ end
80
+ #end
@@ -0,0 +1,36 @@
1
+ ##
2
+ ## copied from edge rails code
3
+ module ActiveResource
4
+ class Base
5
+ private
6
+ # Tries to find a resource in a non empty list of nested modules
7
+ # Raises a NameError if it was not found in any of the given nested modules
8
+ def find_resource_in_modules(resource_name, module_names)
9
+ receiver = Object
10
+ namespaces = module_names[0, module_names.size-1].map do |module_name|
11
+ receiver = receiver.const_get(module_name)
12
+ end
13
+ if namespace = namespaces.reverse.detect { |ns| ns.const_defined?(resource_name) }
14
+ return namespace.const_get(resource_name)
15
+ else
16
+ raise NameError
17
+ end
18
+ end
19
+
20
+ # Tries to find a resource for a given name; if it fails, then the resource is created
21
+ def find_or_create_resource_for(name)
22
+ resource_name = name.to_s.camelize
23
+ ancestors = self.class.name.split("::")
24
+ if ancestors.size > 1
25
+ find_resource_in_modules(resource_name, ancestors)
26
+ else
27
+ self.class.const_get(resource_name)
28
+ end
29
+ rescue NameError
30
+ resource = self.class.const_set(resource_name, Class.new(ActiveResource::Base))
31
+ resource.prefix = self.class.prefix
32
+ resource.site = self.class.site
33
+ resource
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ #### Create classes for YouTube resources.
2
+ module Youtube
3
+ class Video < ActiveYoutube
4
+ self.site = "http://gdata.youtube.com/feeds/api"
5
+
6
+ ## To search by categories and tags
7
+ def self.search_by_tags (*options)
8
+ from_urls = []
9
+ if options.last.is_a? Hash
10
+ excludes = options.slice!(options.length-1)
11
+ if excludes[:exclude].kind_of? Array
12
+ from_urls << excludes[:exclude].map{|keyword| "-"+keyword}.join("/")
13
+ else
14
+ from_urls << "-"+excludes[:exclude]
15
+ end
16
+ end
17
+ from_urls << options.find_all{|keyword| keyword =~ /^[a-z]/}.join("/")
18
+ from_urls << options.find_all{|category| category =~ /^[A-Z]/}.join("%7C")
19
+ from_urls.delete_if {|x| x.empty?}
20
+ self.find(:all,:from=>"/feeds/api/videos/-/"+from_urls.reverse.join("/"))
21
+ end
22
+ end
23
+
24
+ class User < ActiveYoutube
25
+ self.site = "http://gdata.youtube.com/feeds/api"
26
+ end
27
+
28
+ class Standardfeed < ActiveYoutube
29
+ self.site = "http://gdata.youtube.com/feeds/api"
30
+ end
31
+
32
+ class Playlist < ActiveYoutube
33
+ self.site = "http://gdata.youtube.com/feeds/api"
34
+ end
35
+ end
@@ -0,0 +1,58 @@
1
+ require "test/unit"
2
+ require File.join(File.dirname(__FILE__), "../lib", "active_youtube")
3
+
4
+ class YoutubeTest < Test::Unit::TestCase
5
+
6
+ def test_video
7
+ ## search for video
8
+ search = Youtube::Video.find(:first, :params => {:vq => 'ruby', :"max-results" => '5'})
9
+
10
+ assert_equal(5,search.entry.length)
11
+
12
+ ## video information of id = ZTUVgYoeN_o
13
+ vid = Youtube::Video.find("ZTUVgYoeN_o")
14
+ assert_equal("http://www.youtube.com/v/ZTUVgYoeN_o",vid.group.content[0].url)
15
+
16
+ ## video comments
17
+ comments = Youtube::Video.find_custom("ZTUVgYoeN_o").get(:comments)
18
+ assert_equal("http://www.youtube.com/watch?v=ZTUVgYoeN_o",comments.entry[0].link[2].href)
19
+
20
+ ## searching with category/tags
21
+ results = Youtube::Video.search_by_tags("Comedy")
22
+ assert_equal("Russell Peters",results[0].entry[0].title)
23
+ end
24
+
25
+ def test_standardfeed
26
+ ## retrieving standard feeds
27
+ most_viewed = Youtube::Standardfeed.find(:most_viewed, :params => {:time => 'today'})
28
+ assert_equal("http://www.youtube.com/v/aiLvLxebfk0",most_viewed.entry[0].group.content[0].url)
29
+ end
30
+
31
+ def test_user
32
+ ## user's profile - guthrie
33
+ user_profile = Youtube::User.find("guthrie")
34
+ assert_equal("http://www.youtube.com/profile?user=guthrie",user_profile.link[1].href)
35
+
36
+ ## user's playlist - john
37
+ user_playlist = Youtube::User.find_custom("john").get(:playlists)
38
+ assert_equal("http://www.youtube.com/profile_play_list?user=john",user_playlist.link[1].href)
39
+
40
+ ## user's upload or favorites
41
+ rick_video = Youtube::User.find_custom("rick").get(:uploads)
42
+ assert_equal("http://www.youtube.com/v/eNpPK4W0Z3M",rick_video.entry[0].group.content[0].url)
43
+
44
+ ## user's subscription
45
+ #user_subscriptions = Youtube::User.find_custom("guthrie").get(:subscriptions)
46
+ #puts user_subscriptions.to_yaml
47
+ end
48
+
49
+ def test_playlist
50
+ ## get playlist - multiple elements in playlist
51
+ playlist = Youtube::Playlist.find("EBF5D6DC4589D7B7")
52
+ assert_equal(playlist.entry[0].group.content[0].url, "http://www.youtube.com/v/eenf1boHATs")
53
+
54
+ ## get playlist - single element in playlist
55
+ playlist = Youtube::Playlist.find("45C563323B344971")
56
+ assert_equal("http://www.youtube.com/v/i2xMvKOLzYM",playlist.entry[0].group.content[0].url)
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: active_youtube
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2008-02-03 00:00:00 +05:30
8
+ summary: Active Youtube allows consuming YouTube API in your Ruby/Rails project using ActiveResource.
9
+ require_paths:
10
+ - lib
11
+ email: quarkruby@gmail.com
12
+ homepage: http://www.quarkruby.com/
13
+ rubyforge_project:
14
+ description: Active Youtube allows consuming YouTube API in your Ruby/Rails project using ActiveResource.
15
+ autorequire: activeresource
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - QuarkRuby
31
+ files:
32
+ - lib/active_youtube.rb
33
+ - lib/patch.rb
34
+ - lib/activeyoutube.rb
35
+ - lib/youtube.rb
36
+ - examples.rb
37
+ test_files:
38
+ - test/youtube_test.rb
39
+ rdoc_options: []
40
+
41
+ extra_rdoc_files: []
42
+
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ requirements: []
48
+
49
+ dependencies:
50
+ - !ruby/object:Gem::Dependency
51
+ name: activeresource
52
+ version_requirement:
53
+ version_requirements: !ruby/object:Gem::Version::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: 2.0.2
58
+ version: