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

Methods inherited from Video

#inspect

Constructor Details

#initialize(video_id) ⇒ Youtube

Returns a new instance of Youtube.



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

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

Class Method Details

.clientObject



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

def client
  @client
end

.connectionObject



44
45
46
# File 'lib/videostore.rb', line 44

def connection
  @connection
end

.establish_connectionObject



37
38
39
40
41
42
43
# File 'lib/videostore.rb', line 37

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



62
63
64
# File 'lib/videostore.rb', line 62

def client
  self.class.client
end

#connectionObject



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

def connection
  self.class.connection
end

#infoObject



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/videostore.rb', line 92

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/videostore.rb', line 70

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)


66
67
68
# File 'lib/videostore.rb', line 66

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

#uriObject



88
89
90
# File 'lib/videostore.rb', line 88

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

#vimeo?Boolean

Returns:

  • (Boolean)


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

def vimeo? ; false ; end

#youtube?Boolean

Returns:

  • (Boolean)


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

def youtube? ; true ; end