Class: YouTubeG::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/youtube_g/client.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger = false) ⇒ Client

Returns a new instance of Client.



5
6
7
# File 'lib/youtube_g/client.rb', line 5

def initialize(logger=false)
  @logger = Logger.new(STDOUT) if logger
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



3
4
5
# File 'lib/youtube_g/client.rb', line 3

def logger
  @logger
end

Instance Method Details

#video_by(vid) ⇒ Object

Retrieves a single YouTube video.

Parameters

vid<String>:: The ID or URL of the video that you'd like to retrieve.

Returns

YouTubeG::Model::Video



68
69
70
71
72
# File 'lib/youtube_g/client.rb', line 68

def video_by(vid)
  video_id = vid =~ /^http/ ? vid : "http://gdata.youtube.com/feeds/videos/#{vid}"
  parser = YouTubeG::Parser::VideoFeedParser.new(video_id)
  parser.parse
end

#videos_by(params, options = {}) ⇒ Object

Retrieves an array of standard feed, custom query, or user videos.

Parameters

If fetching videos for a standard feed:

params<Symbol>:: Accepts a symbol of :top_rated, :top_favorites, :most_viewed, 
                 :most_popular, :most_recent, :most_discussed, :most_linked, 
                 :most_responded, :recently_featured, and :watch_on_mobile.

You can find out more specific information about what each standard feed provides
by visiting: http://code.google.com/apis/youtube/reference.html#Standard_feeds                 

options<Hash> (optional)::  Accepts the options of :time, :page (default is 1), 
                            and :per_page (default is 25). :offset and :max_results
                            can also be passed for a custom offset.

If fetching videos by tags, categories, query:

params<Hash>:: Accepts the keys :tags, :categories, :query, :order_by, 
               :author, :racy, :response_format, :video_format, :page (default is 1), 
               and :per_page(default is 25)

options<Hash>:: Not used. (Optional)

If fetching videos for a particular user:

params<Hash>:: Key of :user with a value of the username.
options<Hash>:: Not used. (Optional)

Returns

YouTubeG::Response::VideoSearch



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/youtube_g/client.rb', line 36

def videos_by(params, options={})
  request_params = params.respond_to?(:to_hash) ? params : options
  request_params[:page] = integer_or_default(request_params[:page], 1)
  
  unless request_params[:max_results]
    request_params[:max_results] = integer_or_default(request_params[:per_page], 25)
  end
  
  unless request_params[:offset]
    request_params[:offset] = calculate_offset(request_params[:page], request_params[:max_results] )
  end
  
  if params.respond_to?(:to_hash) and not params[:user]
    request = YouTubeG::Request::VideoSearch.new(request_params)
  elsif (params.respond_to?(:to_hash) && params[:user]) || (params == :favorites)
    request = YouTubeG::Request::UserSearch.new(params, request_params)
  else
    request = YouTubeG::Request::StandardSearch.new(params, request_params)
  end
  
  logger.debug "Submitting request [url=#{request.url}]." if logger
  parser = YouTubeG::Parser::VideosFeedParser.new(request.url)
  parser.parse
end