Class: Lumiere::YouTubePlaylist

Inherits:
Provider
  • Object
show all
Defined in:
lib/provider/youtubeplaylist/youtubeplaylist.rb

Constant Summary collapse

USEABLE =
['www.youtube.com', 'youtube.com', 'youtu.be']
RESULTS_PER_REQUEST =
25

Constants inherited from Provider

Provider::PROVIDERS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Provider

#==, #accessible?, delegate, #duration, #upload_date, #video_id

Constructor Details

#initialize(url, opts = {}) ⇒ YouTubePlaylist

Returns a new instance of YouTubePlaylist.



13
14
15
16
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 13

def initialize(url, opts={})
  @url = url
  @start_index = opts[:start_index] || 1
end

Instance Attribute Details

#urlObject

Returns the value of attribute url.



3
4
5
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 3

def url
  @url
end

Class Method Details

.useable?(url) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
11
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 8

def self.useable?(url)
  uri = URISchemeless.parse(url)
  USEABLE.include?(uri.host) && (uri.path == '/playlist' || uri.path == '/view_play_list')
end

Instance Method Details

#api_urlObject



26
27
28
29
30
31
32
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 26

def api_url
  url = "http://gdata.youtube.com/feeds/api/playlists/#{playlist_id}"
  url << "?max-results=#{RESULTS_PER_REQUEST}"
  url << "&start-index=#{@start_index}"
  url << "&v=2&alt=json"
  url
end

#descriptionObject



81
82
83
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 81

def description
  fetch.description
end

#embed_codeObject



38
39
40
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 38

def embed_code
  "<iframe src=\"#{embed_url}\" frameborder=\"0\" allowfullscreen></iframe>"
end

#embed_urlObject



34
35
36
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 34

def embed_url
  "//youtube.com/embed/?list=#{playlist_id}"
end

#page_countObject



61
62
63
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 61

def page_count
  Playlist.page_count(total_results, RESULTS_PER_REQUEST)
end

#playlist_idObject



22
23
24
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 22

def playlist_id
  @playlist_id ||= calculate_playlist_id
end

#providerObject



18
19
20
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 18

def provider
  "YouTube"
end

#thumbnail_largeObject



73
74
75
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 73

def thumbnail_large
  fetch.thumbnails[2].url
end

#thumbnail_mediumObject



69
70
71
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 69

def thumbnail_medium
  fetch.thumbnails[1].url
end

#thumbnail_smallObject



65
66
67
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 65

def thumbnail_small
  fetch.thumbnails[0].url
end

#titleObject



77
78
79
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 77

def title
  fetch.title
end

#total_resultsObject



85
86
87
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 85

def total_results
  fetch.total_results
end

#unpack_intoObject



89
90
91
92
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 89

def unpack_into
  struct = OpenStruct.new
  struct.extend(YouTubePlaylistRepresenter)
end

#videosObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 42

def videos
  if @videos && @videos.size == total_results
    return @videos
  end

  @videos = fetch.videos
  #take into account the first request that calling total_results will trigger through fetch!
  #todo refactor this
  remaining_pages = page_count - 1
  remaining_pages.times do
    @start_index =+ @videos.size + 1
    @videos += fetch!.videos
  end

  @videos.map do |video|
    YouTube.new_from_video_id(video.video_id, video)
  end
end