Class: Youtube::Video

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

Overview

The video class rather unsurprisingly represents an individual YouTube video.

Example

video = playlist.videos.first
video.title # "Semeando 2010 - Mestre Jogo de Dentro" 
video.duration #returns number of seconds: "93" 
video.description # "2010 o VII Encontro Internacional de Capoeira Angola... " 
video.thumbnail(:large) # "\http://i.ytimg.com/vi/4YMMWt2808U/hqdefault.jpg" 
video.id # "4YMMWt2808U" 
video.uploader # "micheldezen" 
video.url # "http://www.youtube.com/watch?v=4YMMWt2808U"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry) ⇒ Video

Returns a new instance of Video.



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/downthetube/video.rb', line 25

def initialize entry
  
  define_attrs

  if entry.class == REXML::Element
    @xml = entry
  elsif((entry.class == String)&&(entry =~/(\w|-){11}/))
    @url = "http://gdata.youtube.com/feeds/api/videos/#{entry}?v=2"
  else
	raise "What was passed into Playlist class was not an REXML object"
  end
end

Class Method Details

.nameObject

:nodoc:



46
47
48
# File 'lib/downthetube/video.rb', line 46

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

Instance Method Details

#thumbnail(size = :small) ⇒ Object

Returns the url of the thumbnail image for the video. If you want the large thumbnail pass :large as a parameter.



54
55
56
57
58
59
60
61
# File 'lib/downthetube/video.rb', line 54

def thumbnail size=:small
  populate_attrs unless @large_thumbnail && @small_thumbnail
 if size == :large
	@large_thumbnail
  else
	@small_thumbnail
  end	
end

#to_hObject

Returns a hash with the video attributes in it.



71
72
73
74
75
76
77
78
# File 'lib/downthetube/video.rb', line 71

def to_h
  populate_attrs unless @xml
    h = {}
  instance_variables.each do |var| 
    h[var.to_s.gsub('@', '').to_sym]= self.instance_variable_get(var.to_sym).to_s unless [:@client, :@xml].include? var
  end
  h
end

#to_xmlObject

Returns that the video was instantiated with.



65
66
67
# File 'lib/downthetube/video.rb', line 65

def to_xml
  @xml.to_s
end

#urlObject

Returns the url of the video



40
41
42
43
# File 'lib/downthetube/video.rb', line 40

def url
  populate_attrs unless @url
  @url
end