Class: Youtube::Playlist

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

Overview

Represents an individual playlist. A single Playlist object can be instantiated with a playlist id. The id can be found in the url and a few other places: gdata.youtube.com/feeds/api/playlists/AAE42E282C4AD007?v=2

Example

playlist = Youtube::Playlist.new "AAE42E282C4AD007"
playlist.title # "Mestre Jogo de Dentro & Seus Alunos"
playlist.id # "AAE42E282C4AD007" 
playlist.author # "stephensam" 
playlist.url #"\http://gdata.youtube.com/feeds/api/playlists/AAE42E282C4AD007" 
videos = playlist.videos(3) # returns 3 videos

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Playlist

Instantiate individual instances of this class with a YouTube id.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/downthetube/play_list.rb', line 30

def initialize entry

  define_attrs

  if ((entry) && (entry.class == REXML::Element))
    @xml = entry
    @id = /\w{16}/.match(@xml.elements['id'].text).to_s
    populate_attrs

  elsif((entry.class==String)&&(entry =~ /\w{16}/))
    @id = entry.to_s
  else
    raise "The Playlist class did not understand the parameter it was initialized with."
  end
    @url = "http://gdata.youtube.com/feeds/api/playlists/#{@id}"
end

Class Method Details

.nameObject

:nodoc:



49
50
51
# File 'lib/downthetube/play_list.rb', line 49

def self.name # :nodoc:
  "Playlist"
end

Instance Method Details

#url(type = :api) ⇒ Object

Returns the gdata api url of the playlist. If you want the human friendly url pass in :human as an argument.



56
57
58
# File 'lib/downthetube/play_list.rb', line 56

def url type = :api
  type == :human ? @human_url : @url
end

#videos(limit = nil) ⇒ Object

This is the main raison d’être of the Playlist class: to retrieve a set of videos. If you want/need to you can pass a integer like 5 to limit the number of videos returned.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/downthetube/play_list.rb', line 65

def videos limit=nil
  
  case 
  when limit.nil?     
    get_videos unless @videos
  when limit
    if limit == @limit
      get_videos(limit) unless @videos
    else
      get_videos(limit)
      @limit = limit
    end
  else
    puts "Your case has gone badly wrong"
  end
  @videos
end