Class: VideoStore::Youtube

Inherits:
Video
  • Object
show all
Defined in:
lib/videostore.rb

Constant Summary collapse

DEVELOPER_KEY =
::YOUTUBE_DEVELOPER_KEY
YOUTUBE_API_SERVICE_NAME =
"youtube"
YOUTUBE_API_VERSION =
"v3"

Constants inherited from Video

Video::ATTRIBUTES

Instance Attribute Summary

Attributes inherited from Video

#channel_name, #comment_count, #duration, #likes, #thumbnail_uri, #title, #video_id, #views

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(video_id) ⇒ Youtube

Returns a new instance of Youtube.



46
47
48
49
50
# File 'lib/videostore.rb', line 46

def initialize(video_id)
  @video_id = video_id
  @response_hash = Hash.new
  populate_attributes
end

Class Method Details

.clientObject



40
41
42
# File 'lib/videostore.rb', line 40

def client
  @client
end

.connectionObject



37
38
39
# File 'lib/videostore.rb', line 37

def connection
  @connection
end

.establish_connectionObject



30
31
32
33
34
35
36
# File 'lib/videostore.rb', line 30

def establish_connection
  @client = Google::APIClient.new(key: DEVELOPER_KEY,
                                  application_name: YOUTUBE_API_SERVICE_NAME,
                                  application_version: YOUTUBE_API_VERSION, 
                                  authorization: nil)
  @connection = @client.discovered_api(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION)
end

Instance Method Details

#clientObject



55
56
57
# File 'lib/videostore.rb', line 55

def client
  self.class.client
end

#connectionObject



52
53
54
# File 'lib/videostore.rb', line 52

def connection
  self.class.connection
end

#infoObject



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/videostore.rb', line 85

def info
  response = {}
  if present?
    ATTRIBUTES.each do |attribute|
      response.merge!({attribute => send(attribute)})
    end
  else
    response.merge!({:video_id => @video_id})
    response.merge!({:video_not_found => 'video_not_found'})
  end 
  response
end

#populate_attributes(options = {}) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/videostore.rb', line 63

def populate_attributes(options = {})
  if present?
    content_details = JSON.parse(fetch_data('contentDetails').response.body)['items'].first['contentDetails']
    @duration = content_details["duration"].youtube_duration_to_seconds

    statistics = JSON.parse(fetch_data('statistics').response.body)['items'].first['statistics']
    @views = statistics["viewCount"].to_i
    @likes = statistics["likeCount"].to_i
    @comment_count = statistics["commentCount"].to_i

    snippet = JSON.parse(fetch_data('snippet').response.body)['items'].first['snippet']
    @title = snippet["title"]
    @thumbnail_uri = snippet["thumbnails"]["default"]["url"]
    @channel_name = snippet["channelTitle"]
  end
  info
end

#present?(options = {}) ⇒ Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/videostore.rb', line 59

def present?(options = {})
  JSON.parse(fetch_data('id').response.body)["pageInfo"]["totalResults"] == 1
end

#uriObject



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

def uri
  "http://www.youtube.com/watch?v=#{@video_id}" if present?
end

#vimeo?Boolean

Returns:

  • (Boolean)


99
# File 'lib/videostore.rb', line 99

def vimeo? ; false ; end

#youtube?Boolean

Returns:

  • (Boolean)


98
# File 'lib/videostore.rb', line 98

def youtube? ; true ; end