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
- class BaseSearch #:nodoc:
0
- "http://gdata.youtube.com/feeds/api/"
0
- def set_instance_variables( variables ) #:nodoc:
0
- variables.each do |key, value|
0
- instance_variable_set("@#{name}", value) if respond_to?(name)
0
- def build_query_params(params) #:nodoc:
0
- # nothing to do if there are no params
0
- return '' if (!params || params.empty?)
0
- # build up the query param string, tacking on every key/value
0
- # pair for which the value is non-nil
0
- params.keys.each do |key|
0
- u << '&' if (item_count > 0)
0
- u << "#{CGI.escape(key.to_s)}=#{CGI.escape(value.to_s)}"
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
- class UserSearch < BaseSearch #:nodoc:
0
- def initialize(params, options={})
0
- return @url << "#{options[:user]}/favorites" if params == :favorites
0
- @url << "#{params[:user]}/uploads" if params[:user]
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
- TYPES = [ :most_viewed, :top_rated, :recently_featured, :watch_on_mobile ]
0
- def initialize(type, options={})
0
- if TYPES.include?(type)
0
- set_instance_variables(options)
0
- @url = base_url + type.to_s << build_query_params(to_youtube_params)
0
- raise "Invalid type, must be one of: #{ TYPES.map { |t| t.to_s }.join(", ") }"
0
- super << "standardfeeds/"
0
- def to_youtube_params #:nodoc:
0
- 'max-results' => @max_results,
0
- 'orderby' => @order_by,
0
- 'start-index' => @offset,
0
+ module Request #:nodoc:
0
class VideoSearch < BaseSearch #:nodoc:
0
# From here: http://code.google.com/apis/youtube/reference.html#yt_format
0
@@ -112,24 +16,15 @@ class YouTubeG
0
def initialize(params={})
0
- # XXX I think we want to delete the line below
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
- @response_format = nil
0
- # build up the url corresponding to this request
0
+ @max_results, @order_by,
0
+ @response_format, @video_format,
0
- #
http://gdata.youtube.com/feeds/videos/T7YazwP8GtY0
+ #
Return a single video (base_url + /T7YazwP8GtY)0
return @url << "/" << params[:video_id] if params[:video_id]
0
@url << "/-/" if (params[:categories] || params[:tags])
0
@@ -163,36 +58,35 @@ class YouTubeG
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 << 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
- categories.map { |c| c.to_s.capitalize }.join("/") << '/'
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 << 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
- tags.map { |t| CGI.escape(t.to_s) }.join("/") << '/'
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 << 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
+ categories.map { |c| c.to_s.capitalize }.join("/") << '/'
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 << 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
+ tags.map { |t| CGI.escape(t.to_s) }.join("/") << '/'
Comments
No one has commented yet.