Class: Lumiere::YouTubePlaylist

Inherits:
Provider
  • Object
show all
Includes:
EmbedCode
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 included from EmbedCode

#embed_code

Methods inherited from Provider

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

Constructor Details

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

Returns a new instance of YouTubePlaylist.



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

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)


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

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



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

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

#default_attributesObject



39
40
41
42
43
44
45
46
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 39

def default_attributes
  {
    iframe_attributes: {
      frameborder: 0,
      allowfullscreen: true
    }
  }
end

#descriptionObject



87
88
89
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 87

def description
  fetch.description
end

#embed_urlObject



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

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

#page_countObject



67
68
69
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 67

def page_count
  Playlist.page_count(total_results, RESULTS_PER_REQUEST)
end

#playlist_idObject



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

def playlist_id
  @playlist_id ||= calculate_playlist_id
end

#providerObject



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

def provider
  "YouTube"
end

#thumbnail_largeObject



79
80
81
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 79

def thumbnail_large
  fetch.thumbnails[2].url
end

#thumbnail_mediumObject



75
76
77
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 75

def thumbnail_medium
  fetch.thumbnails[1].url
end

#thumbnail_smallObject



71
72
73
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 71

def thumbnail_small
  fetch.thumbnails[0].url
end

#titleObject



83
84
85
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 83

def title
  fetch.title
end

#total_resultsObject



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

def total_results
  fetch.total_results
end

#unpack_intoObject



95
96
97
98
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 95

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

#videosObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/provider/youtubeplaylist/youtubeplaylist.rb', line 48

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