Class: Youtube

Inherits:
Object
  • Object
show all
Defined in:
lib/service/youtube.rb

Overview

Retrieve channel’s video from YouTube

Constant Summary collapse

CHANNEL_ENDPOINT =
'https://www.googleapis.com/youtube/v3/channels'.freeze
PLAYLIST_ENDPOINT =
'https://www.googleapis.com/youtube/v3/playlistItems'.freeze
YOUTUBE_URL =
'https://www.youtube.com/watch'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(api_key) ⇒ Youtube

Returns a new instance of Youtube.



8
9
10
11
# File 'lib/service/youtube.rb', line 8

def initialize(api_key)
  @api_key = api_key
  @agent = Mechanize.new
end

Instance Method Details

#last_content(channel_id) ⇒ Object



28
29
30
# File 'lib/service/youtube.rb', line 28

def last_content(channel_id)
  videos(channel_id).first
end

#random_content(channel_id) ⇒ Object



32
33
34
# File 'lib/service/youtube.rb', line 32

def random_content(channel_id)
  videos(channel_id).sample
end

#video_from_playlist(playlist_id) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/service/youtube.rb', line 17

def video_from_playlist(playlist_id)
  params = {
    playlistId: playlist_id,
    key: @api_key,
    part: 'snippet',
    maxResults: 50
  }
  response = parse_response(@agent.get(PLAYLIST_ENDPOINT, params))
  parse_videos(response)
end

#videos(channel_id) ⇒ Object



13
14
15
# File 'lib/service/youtube.rb', line 13

def videos(channel_id)
  playlists(channel_id).map { |playlist| video_from_playlist(playlist) }.flatten
end