class Yt::Models::Annotation
Provides methods to interact with YouTube annotations. @note YouTube API V3 does not provide access to video annotations,
therefore a legacy XML endpoint is used to retrieve annotations.
Public Class Methods
Source
# File lib/yt/models/annotation.rb, line 10 def initialize(options = {}) @data = options[:data] end
@param [Hash] options the options to initialize an Annotation
. @option options [String] :data The XML representation of an annotation
Public Instance Methods
Source
# File lib/yt/models/annotation.rb, line 17 def above?(y) top && top < y end
@return [Boolean] whether the text box surrounding the annotation is
completely in the top y% of the video frame.
@param [Integer] y Vertical position in the Youtube video (0 to 100)
Source
# File lib/yt/models/annotation.rb, line 24 def below?(y) bottom && bottom > y end
@return [Boolean] whether the text box surrounding the annotation is
completely in the bottom y% of the video frame.
@param [Integer] y Vertical position in the Youtube video (0 to 100)
Source
# File lib/yt/models/annotation.rb, line 52 def has_invideo_programming? type == 'promotion' || type == 'branding' end
@return [Boolean] whether the annotation is an “InVideo Programming”.
Source
# File lib/yt/models/annotation.rb, line 41 def has_link_to_playlist? link_class == '2' || text.include?('&list=') end
@return [Boolean] whether the annotation includes a link to a playlist,
or to a video embedded in a playlist.
Source
# File lib/yt/models/annotation.rb, line 47 def has_link_to_same_window? link_target == 'current' end
@return [Boolean] whether the annotation includes a link that will
open in the current browser window.
Source
# File lib/yt/models/annotation.rb, line 29 def has_link_to_subscribe?(options = {}) # TODO: options for which videos link_class == '5' end
@return [Boolean] whether the annotation includes a link to subscribe.
Source
# File lib/yt/models/annotation.rb, line 35 def has_link_to_video?(options = {}) # TODO: options for which videos link_class == '1' || type == 'promotion' end
@return [Boolean] whether the annotation includes a link to a video,
either directly in the text, or as an "Invideo featured video".
Source
# File lib/yt/models/annotation.rb, line 61 def starts_after?(seconds) timestamps.first > seconds if timestamps.any? end
@param [Numeric] seconds the number of seconds @return [Boolean] whether the annotation starts after the number of
seconds indicated.
@note This is broken for invideo programming, because they do not
have the timestamp in the region, but in the "data" field
Source
# File lib/yt/models/annotation.rb, line 70 def starts_before?(seconds) timestamps.first < seconds if timestamps.any? end
@param [Numeric] seconds the number of seconds @return [Boolean] whether the annotation starts before the number of
seconds indicated.
@note This is broken for invideo programming, because they do not
have the timestamp in the region, but in the "data" field
Source
# File lib/yt/models/annotation.rb, line 75 def text @text ||= @data['TEXT'] || '' end
@return [String] the textual content of the annotation.
Private Instance Methods
Source
# File lib/yt/models/annotation.rb, line 101 def bottom @bottom ||= positions.map{|pos| pos['y'].to_f + pos['h'].to_f}.max end
Source
# File lib/yt/models/annotation.rb, line 83 def link_class @link_class ||= url['link_class'] end
Source
# File lib/yt/models/annotation.rb, line 87 def link_target @link_target ||= url['target'] end
Source
# File lib/yt/models/annotation.rb, line 121 def positions @positions ||= Array.wrap region['rectRegion'] || region['anchoredRegion'] end
Source
# File lib/yt/models/annotation.rb, line 125 def region @region ||= segment.fetch 'movingRegion', {} end
Source
# File lib/yt/models/annotation.rb, line 111 def timestamp_of(position) regex = %r{(?:|(?<hours>\d*):)(?:|(?<min>\d*):)(?<sec>\d*)\.(?<ms>\d*)} position['t'] = '00:00:00.000' if position['t'] == '0' match = position['t'].match regex hours = (match[:hours] || '0').to_i minutes = (match[:min] || '0').to_i seconds = (match[:sec]).to_i (hours * 60 + minutes) * 60 + seconds end
Source
# File lib/yt/models/annotation.rb, line 105 def timestamps @timestamps ||= positions.reject{|pos| pos['t'] == 'never'}.map do |pos| timestamp_of pos end end
Source
# File lib/yt/models/annotation.rb, line 97 def top @top ||= positions.map{|pos| pos['y'].to_f}.max end
Source
# File lib/yt/models/annotation.rb, line 91 def url @url ||= action.fetch 'url', {} end