Class: YouTube::Client

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

Overview

Main client class managing all interaction with the YouTube server. Server communication is handled via method_missing() emulating an RPC-like call and performing all of the work to send out the HTTP request and retrieve the XML response. Inspired by the Flickr interface by Scott Raymond <redgreenblu.com/flickr/>.

Constant Summary collapse

DEFAULT_HOST =

the default hostname at which the YouTube API is hosted

'http://www.youtube.com'
DEFAULT_API_PATH =

the default api path to the YouTube API

'/api2_rest'

Instance Method Summary collapse

Constructor Details

#initialize(dev_id = nil, host = DEFAULT_HOST, api_path = DEFAULT_API_PATH) ⇒ Client

Returns a new instance of Client.



56
57
58
59
60
61
62
# File 'lib/youtube.rb', line 56

def initialize(dev_id = nil, host = DEFAULT_HOST, api_path = DEFAULT_API_PATH)
  raise "developer id required" unless dev_id

  @host = host
  @api_path = api_path
  @dev_id = dev_id
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *params) ⇒ Object (private)

All API methods are implemented with this method. This method is like a remote method call, it encapsulates the request/response cycle to the remote host. It extracts the remote method API name based on the ruby method name.



186
187
188
# File 'lib/youtube.rb', line 186

def method_missing(method_id, *params)
  _request(method_id.to_s.sub('_', '.'), *params)
end

Instance Method Details

#favorite_videos(username) ⇒ Object

Returns a list of YouTube::Video objects detailing the favorite videos of the supplied username.



73
74
75
76
# File 'lib/youtube.rb', line 73

def favorite_videos(username)
  response = users_list_favorite_videos(:user => username)
  _parse_video_response(response)
end

Returns a list of YouTube::Video objects detailing the current global set of featured videos on YouTube.



167
168
169
170
# File 'lib/youtube.rb', line 167

def featured_videos
  response = videos_list_featured
  _parse_video_response(response)
end

#friends(username) ⇒ Object

Returns a list of YouTube::Friend objects detailing the friends of the supplied username.



80
81
82
83
84
# File 'lib/youtube.rb', line 80

def friends(username)
  response = users_list_friends(:user => username)
  friends = response['friend_list']['friend']
  friends.is_a?(Array) ? friends.compact.map { |friend| Friend.new(friend) } : nil        
end

#profile(username) ⇒ Object

Returns a YouTube::Profile object detailing the profile information regarding the supplied username.



66
67
68
69
# File 'lib/youtube.rb', line 66

def profile(username)
  response = users_get_profile(:user => username)
  Profile.new response['user_profile']
end

#video_details(video_id) ⇒ Object

Returns a YouTube::VideoDetails object detailing additional information on the supplied video id, obtained from a YouTube::Video object from a previous client call.

Raises:

  • (ArgumentError)


175
176
177
178
179
# File 'lib/youtube.rb', line 175

def video_details(video_id)
  raise ArgumentError.new("invalid video id parameter, must be string") unless video_id.is_a?(String)
  response = videos_get_details(:video_id => video_id)
  VideoDetails.new(response['video_details'])
end

#videos_by_category_and_tag(category, tag, page = 1, per_page = 20) ⇒ Object

Returns a list of YouTube::Video objects detailing the videos matching the supplied category and tag.

Available categories: YouTube::Category::FILMS_ANIMATION YouTube::Category::AUTOS_VEHICLES YouTube::Category::COMEDY YouTube::Category::ENTERTAINMENT YouTube::Category::MUSIC YouTube::Category::NEWS_POLITICS YouTube::Category::PEOPLE_BLOGS YouTube::Category::PETS_ANIMALS YouTube::Category::HOWTO_DIY YouTube::Category::SPORTS YouTube::Category::TRAVEL_PLACES YouTube::Category::GADGETS_GAMES

Optional parameters are: page = the “page” of results to retrieve (e.g. 1, 2, 3) per_page = the number of results per page (default: 20, max 100).



150
151
152
# File 'lib/youtube.rb', line 150

def videos_by_category_and_tag(category, tag, page = 1, per_page = 20)
  videos_by_category_id_and_tag(category, tag, page, per_page)
end

#videos_by_category_id_and_tag(id, tag, page = 1, per_page = 20) ⇒ Object

Returns a list of YouTube::Video objects detailing the videos matching the supplied category id and tag.

Optional parameters are: page = the “page” of results to retrieve (e.g. 1, 2, 3) per_page = the number of results per page (default: 20, max 100).



125
126
127
128
# File 'lib/youtube.rb', line 125

def videos_by_category_id_and_tag(id, tag, page = 1, per_page = 20)
  response = videos_list_by_category_and_tag(:category_id => id, :tag => tag, :page => page, :per_page => per_page)
  _parse_video_response(response)
end

#videos_by_playlist(id, page = 1, per_page = 20) ⇒ Object

Returns a list of YouTube::Video objects with the specified playlist id.

Optional parameters are: page = the “page” of results to retrieve (e.g. 1, 2, 3) per_page = the number of results per page (default: 20, max 100).



114
115
116
117
# File 'lib/youtube.rb', line 114

def videos_by_playlist(id, page = 1, per_page = 20)
  response = videos_list_by_playlist(:id => id, :page => page, :per_page => per_page)
  _parse_video_response(response)
end

Returns a list of YouTube::Video objects that match the specified tag.

Optional parameters are: page = the “page” of results to retrieve (e.g. 1, 2, 3) per_page = the number of results per page (default: 20, max 100).



103
104
105
106
# File 'lib/youtube.rb', line 103

def videos_by_related(tag, page = 1, per_page = 20)
  response = videos_list_by_related(:tag => tag, :page => page, :per_page => per_page)
  _parse_video_response(response)
end

#videos_by_tag(tag, page = 1, per_page = 20) ⇒ Object

Returns a list of YouTube::Video objects detailing the videos matching the supplied tag.

Optional parameters are: page = the “page” of results to retrieve (e.g. 1, 2, 3) per_page = the number of results per page (default: 20, max 100).



92
93
94
95
# File 'lib/youtube.rb', line 92

def videos_by_tag(tag, page = 1, per_page = 20)
  response = videos_list_by_tag(:tag => tag, :page => page, :per_page => per_page)
  _parse_video_response(response)
end

#videos_by_user(username, page = 1, per_page = 20) ⇒ Object

Returns a list of YouTube::Video objects detailing the videos uploaded by the specified username.

Optional parameters are: page = the “page” of results to retrieve (e.g. 1, 2, 3) per_page = the number of results per page (default: 20, max 100).



160
161
162
163
# File 'lib/youtube.rb', line 160

def videos_by_user(username, page = 1, per_page = 20)
 response = videos_list_by_user(:user => username, :page => page, :per_page => per_page)
  _parse_video_response(response)
end