public
Rubygem
Description: An object-oriented Ruby wrapper for the YouTube GData API
Homepage: http://groups.google.com/group/ruby-youtube-library
Clone URL: git://github.com/tmm1/youtube-g.git
moved request classes into seperate files, and some minor refactoring
dinsley (author)
Mon Jul 07 12:03:16 -0700 2008
commit  48363eff4738fec2f11d8bdd1dc2591c9440c1fd
tree    4fecb3156e2f21b5a68ae4bcfac69cfd42e7acaa
parent  5f24804f3f9fdb29f2e3d2667bf8251f1102bfd1
...
17
18
19
 
 
 
20
21
22
...
17
18
19
20
21
22
23
24
25
0
@@ -17,6 +17,9 @@ lib/youtube_g/model/user.rb
0
 lib/youtube_g/model/video.rb
0
 lib/youtube_g/parser.rb
0
 lib/youtube_g/record.rb
0
+lib/youtube_g/request/base_search.rb
0
+lib/youtube_g/request/standard_search.rb
0
+lib/youtube_g/request/user_search.rb
0
 lib/youtube_g/request/video_search.rb
0
 lib/youtube_g/request/video_upload.rb
0
 lib/youtube_g/response/video_search.rb
...
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
 
99
100
101
...
112
113
114
115
116
117
118
 
119
120
121
122
123
124
125
126
127
128
129
 
 
 
 
130
131
132
 
133
134
135
...
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
 
 
 
 
 
 
 
 
 
 
 
 
 
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
197
198
...
1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
3
4
5
...
16
17
18
 
 
 
 
19
20
 
 
 
 
 
 
 
 
 
 
21
22
23
24
25
26
 
27
28
29
30
...
58
59
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
0
@@ -1,101 +1,5 @@
0
 class YouTubeG
0
-
0
- # The goal of the classes in this module is to build the request URLs for each type of search
0
- module Request #:nodoc:
0
-
0
- class BaseSearch #:nodoc:
0
- attr_reader :url
0
-
0
- private
0
-
0
- def base_url #:nodoc:
0
- "http://gdata.youtube.com/feeds/api/"
0
- end
0
-
0
- def set_instance_variables( variables ) #:nodoc:
0
- variables.each do |key, value|
0
- name = key.to_s
0
- instance_variable_set("@#{name}", value) if respond_to?(name)
0
- end
0
- end
0
-
0
- def build_query_params(params) #:nodoc:
0
- # nothing to do if there are no params
0
- return '' if (!params || params.empty?)
0
-
0
- # build up the query param string, tacking on every key/value
0
- # pair for which the value is non-nil
0
- u = '?'
0
- item_count = 0
0
- params.keys.each do |key|
0
- value = params[key]
0
- next if value.nil?
0
-
0
- u << '&' if (item_count > 0)
0
- u << "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
0
- item_count += 1
0
- end
0
-
0
- # if we found no non-nil values, we've got no params so just
0
- # return an empty string
0
- (item_count == 0) ? '' : u
0
- end
0
- end
0
-
0
- class UserSearch < BaseSearch #:nodoc:
0
-
0
- def initialize(params, options={})
0
- @url = base_url
0
- return @url << "#{options[:user]}/favorites" if params == :favorites
0
- @url << "#{params[:user]}/uploads" if params[:user]
0
- end
0
-
0
- private
0
-
0
- def base_url #:nodoc:
0
- super << "users/"
0
- end
0
- end
0
-
0
- class StandardSearch < BaseSearch #:nodoc:
0
- attr_reader :max_results # max_results
0
- attr_reader :order_by # orderby, ([relevance], viewCount, published, rating)
0
- attr_reader :offset # start-index
0
- attr_reader :time # time
0
-
0
- TYPES = [ :most_viewed, :top_rated, :recently_featured, :watch_on_mobile ]
0
-
0
- def initialize(type, options={})
0
- if TYPES.include?(type)
0
- @max_results = nil
0
- @order_by = nil
0
- @offset = nil
0
- @time = nil
0
-
0
- set_instance_variables(options)
0
- @url = base_url + type.to_s << build_query_params(to_youtube_params)
0
- else
0
- raise "Invalid type, must be one of: #{ TYPES.map { |t| t.to_s }.join(", ") }"
0
- end
0
- end
0
-
0
- private
0
-
0
- def base_url #:nodoc:
0
- super << "standardfeeds/"
0
- end
0
-
0
- def to_youtube_params #:nodoc:
0
- {
0
- 'max-results' => @max_results,
0
- 'orderby' => @order_by,
0
- 'start-index' => @offset,
0
- 'time' => @time
0
- }
0
- end
0
-
0
- end
0
-
0
+ module Request #:nodoc:
0
     class VideoSearch < BaseSearch #:nodoc:
0
       # From here: http://code.google.com/apis/youtube/reference.html#yt_format
0
       ONLY_EMBEDDABLE = 5
0
@@ -112,24 +16,15 @@ class YouTubeG
0
       attr_reader :author
0
       
0
       def initialize(params={})
0
- # XXX I think we want to delete the line below
0
- return if params.nil?
0
-
0
- # initialize our various member data to avoid warnings and so we'll
0
+ # Initialize our various member data to avoid warnings and so we'll
0
         # automatically fall back to the youtube api defaults
0
- @max_results = nil
0
- @order_by = nil
0
- @offset = nil
0
- @query = nil
0
- @response_format = nil
0
- @video_format = nil
0
- @racy = nil
0
- @author = nil
0
-
0
- # build up the url corresponding to this request
0
+ @max_results, @order_by,
0
+ @offset, @query,
0
+ @response_format, @video_format,
0
+ @racy, @author = nil
0
         @url = base_url
0
         
0
- # http://gdata.youtube.com/feeds/videos/T7YazwP8GtY
0
+ # Return a single video (base_url + /T7YazwP8GtY)
0
         return @url << "/" << params[:video_id] if params[:video_id]
0
         
0
         @url << "/-/" if (params[:categories] || params[:tags])
0
@@ -163,36 +58,35 @@ class YouTubeG
0
           'author' => @author
0
         }
0
       end
0
-
0
- private
0
- # Convert category symbols into strings and build the URL. GData requires categories to be capitalized.
0
- # Categories defined like: categories => { :include => [:news], :exclude => [:sports], :either => [..] }
0
- # or like: categories => [:news, :sports]
0
- def categories_to_params(categories) #:nodoc:
0
- if categories.respond_to?(:keys) and categories.respond_to?(:[])
0
- s = ""
0
- s << categories[:either].map { |c| c.to_s.capitalize }.join("%7C") << '/' if categories[:either]
0
- s << categories[:include].map { |c| c.to_s.capitalize }.join("/") << '/' if categories[:include]
0
- s << ("-" << categories[:exclude].map { |c| c.to_s.capitalize }.join("/-")) << '/' if categories[:exclude]
0
- s
0
- else
0
- categories.map { |c| c.to_s.capitalize }.join("/") << '/'
0
- end
0
- end
0
-
0
- # Tags defined like: tags => { :include => [:football], :exclude => [:soccer], :either => [:polo, :tennis] }
0
- # or tags => [:football, :soccer]
0
- def tags_to_params(tags) #:nodoc:
0
- if tags.respond_to?(:keys) and tags.respond_to?(:[])
0
- s = ""
0
- s << tags[:either].map { |t| CGI.escape(t.to_s) }.join("%7C") << '/' if tags[:either]
0
- s << tags[:include].map { |t| CGI.escape(t.to_s) }.join("/") << '/' if tags[:include]
0
- s << ("-" << tags[:exclude].map { |t| CGI.escape(t.to_s) }.join("/-")) << '/' if tags[:exclude]
0
- s
0
- else
0
- tags.map { |t| CGI.escape(t.to_s) }.join("/") << '/'
0
- end
0
+
0
+ # Convert category symbols into strings and build the URL. GData requires categories to be capitalized.
0
+ # Categories defined like: categories => { :include => [:news], :exclude => [:sports], :either => [..] }
0
+ # or like: categories => [:news, :sports]
0
+ def categories_to_params(categories) #:nodoc:
0
+ if categories.respond_to?(:keys) and categories.respond_to?(:[])
0
+ s = ""
0
+ s << categories[:either].map { |c| c.to_s.capitalize }.join("%7C") << '/' if categories[:either]
0
+ s << categories[:include].map { |c| c.to_s.capitalize }.join("/") << '/' if categories[:include]
0
+ s << ("-" << categories[:exclude].map { |c| c.to_s.capitalize }.join("/-")) << '/' if categories[:exclude]
0
+ s
0
+ else
0
+ categories.map { |c| c.to_s.capitalize }.join("/") << '/'
0
         end
0
+ end
0
+
0
+ # Tags defined like: tags => { :include => [:football], :exclude => [:soccer], :either => [:polo, :tennis] }
0
+ # or tags => [:football, :soccer]
0
+ def tags_to_params(tags) #:nodoc:
0
+ if tags.respond_to?(:keys) and tags.respond_to?(:[])
0
+ s = ""
0
+ s << tags[:either].map { |t| CGI.escape(t.to_s) }.join("%7C") << '/' if tags[:either]
0
+ s << tags[:include].map { |t| CGI.escape(t.to_s) }.join("/") << '/' if tags[:include]
0
+ s << ("-" << tags[:exclude].map { |t| CGI.escape(t.to_s) }.join("/-")) << '/' if tags[:exclude]
0
+ s
0
+ else
0
+ tags.map { |t| CGI.escape(t.to_s) }.join("/") << '/'
0
+ end
0
+ end
0
         
0
     end
0
   end

Comments

    No one has commented yet.