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.



41
42
43
44
45
46
47
# File 'lib/youtube.rb', line 41

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.



109
110
111
# File 'lib/youtube.rb', line 109

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.



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

def favorite_videos(username)
  response = users_list_favorite_videos(:user => username)
  response['video_list']['video'].compact.map { |video| Video.new(video) }
end

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



90
91
92
93
# File 'lib/youtube.rb', line 90

def featured_videos
  response = videos_list_featured
  response['video_list']['video'].compact.map { |video| Video.new(video) }
end

#friends(username) ⇒ Object

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



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

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

#profile(username) ⇒ Object

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



51
52
53
54
# File 'lib/youtube.rb', line 51

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)


98
99
100
101
102
# File 'lib/youtube.rb', line 98

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_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).



76
77
78
79
# File 'lib/youtube.rb', line 76

def videos_by_tag(tag, page = 1, per_page = 20)
  response = videos_list_by_tag(:tag => tag, :page => page, :per_page => per_page)
  response['video_list']['video'].compact.map { |video| Video.new(video) }
end

#videos_by_user(username) ⇒ Object

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



83
84
85
86
# File 'lib/youtube.rb', line 83

def videos_by_user(username)
  response = videos_list_by_user(:user => username)
  response['video_list']['video'].compact.map { |video| Video.new(video) }
end