Class: YouTubeG::Client

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

Instance Method Summary collapse

Methods included from Logging

#logger

Constructor Details

#initialize(legacy_debug_flag = nil) ⇒ Client

Previously this was a logger instance but we now do it globally



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

def initialize(legacy_debug_flag = nil)
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}]."
  parser = YouTubeG::Parser::VideosFeedParser.new(request.url)
  parser.parse
end