Class: YoutubeVideo::Video

Inherits:
Object
  • Object
show all
Defined in:
lib/YPBT/video.rb

Overview

Main class to setup a Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:) ⇒ Video

Returns a new instance of Video.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/YPBT/video.rb', line 12

def initialize(data:)
  @id = data['id']
  @title = data['snippet']['title']
  @channel_id = data['snippet']['channelId']
  @description = data['snippet']['description']
  @category_id = data['snippet']['categoryId']
  @thumbnail_url = data['snippet']['thumbnails']['medium']['url']
  @dislike_count = data['statistics']['dislikeCount'].to_i
  @like_count = data['statistics']['likeCount'].to_i
  @view_count = data['statistics']['viewCount'].to_i
  @duration = data['contentDetails']['duration']
  @is_channel = false
end

Instance Attribute Details

#category_idObject (readonly)

Returns the value of attribute category_id.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def category_id
  @category_id
end

#channel_idObject (readonly)

Returns the value of attribute channel_id.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def channel_id
  @channel_id
end

#comment_countObject (readonly)

Returns the value of attribute comment_count.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def comment_count
  @comment_count
end

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def description
  @description
end

#dislike_countObject (readonly)

Returns the value of attribute dislike_count.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def dislike_count
  @dislike_count
end

#durationObject (readonly)

Returns the value of attribute duration.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def duration
  @duration
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def id
  @id
end

#like_countObject (readonly)

Returns the value of attribute like_count.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def like_count
  @like_count
end

#thumbnail_urlObject (readonly)

Returns the value of attribute thumbnail_url.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def thumbnail_url
  @thumbnail_url
end

#titleObject (readonly)

Returns the value of attribute title.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def title
  @title
end

#view_countObject (readonly)

Returns the value of attribute view_count.



8
9
10
# File 'lib/YPBT/video.rb', line 8

def view_count
  @view_count
end

Class Method Details

.find(video_id:) ⇒ Object



53
54
55
56
# File 'lib/YPBT/video.rb', line 53

def self.find(video_id:)
  video_data = YtApi.video_info(video_id)
  new(data: video_data) if video_data
end


58
59
60
61
# File 'lib/YPBT/video.rb', line 58

def self.find_popular(max_results: 25)
  videos_data = YtApi.popular_videos_info(max_results)
  videos_data.map { |data| new(data: data) } unless videos_data.empty?
end

Instance Method Details

#channel_descriptionObject



43
44
45
46
# File 'lib/YPBT/video.rb', line 43

def channel_description
  load_channel_info unless @is_channel
  @channel_description
end

#channel_image_urlObject



38
39
40
41
# File 'lib/YPBT/video.rb', line 38

def channel_image_url
  load_channel_info unless @is_channel
  @channel_image_url
end

#channel_titleObject



33
34
35
36
# File 'lib/YPBT/video.rb', line 33

def channel_title
  load_channel_info unless @is_channel
  @channel_title
end

#commentsObject



26
27
28
29
30
31
# File 'lib/YPBT/video.rb', line 26

def comments
  # contain only the comments which have time tag.

  return @comments if @comments
  raw_comments = YtApi.time_tags_info(@id)
  @comments = raw_comments.map { |comment| Comment.new(data: comment) }
end

#embed_urlObject



48
49
50
51
# File 'lib/YPBT/video.rb', line 48

def embed_url
  return @embed_url if @embed_url
  @embed_url = "https://www.youtube.com/embed/#{@id}"
end