class YoutubeVideo::Video

Main class to setup a Video

Attributes

category_id[R]
channel_id[R]
comment_count[R]
description[R]
dislike_count[R]
duration[R]
id[R]
like_count[R]
thumbnail_url[R]
title[R]
view_count[R]

Public Class Methods

find(video_id:) click to toggle source
# 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
new(data:) click to toggle source
# 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

Public Instance Methods

channel_description() click to toggle source
# File lib/YPBT/video.rb, line 43
def channel_description
  load_channel_info unless @is_channel
  @channel_description
end
channel_image_url() click to toggle source
# File lib/YPBT/video.rb, line 38
def channel_image_url
  load_channel_info unless @is_channel
  @channel_image_url
end
channel_title() click to toggle source
# File lib/YPBT/video.rb, line 33
def channel_title
  load_channel_info unless @is_channel
  @channel_title
end
comments() click to toggle source
# 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_url() click to toggle source
# 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

Private Instance Methods

load_channel_info() click to toggle source
# File lib/YPBT/video.rb, line 65
def load_channel_info
  channel_data = YtApi.channel_info @channel_id
  @channel_title = channel_data['title'] if channel_data
  @channel_image_url = channel_data['image_url'] if channel_data
  @channel_description = channel_data['description'] if channel_data
  @is_channel = true
end