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.

@see www.youtube.com/yt/playbook/annotations.html

Public Class Methods

new(options = {}) click to toggle source

@param [Hash] options the options to initialize an Annotation. @option options [String] :data The XML representation of an annotation

# File lib/yt/models/annotation.rb, line 10
def initialize(options = {})
  @data = options[:data]
end

Public Instance Methods

above?(y) click to toggle source

@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)

# File lib/yt/models/annotation.rb, line 17
def above?(y)
  top && top < y
end
below?(y) click to toggle source

@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)

# File lib/yt/models/annotation.rb, line 24
def below?(y)
  bottom && bottom > y
end
has_invideo_programming?() click to toggle source

@return [Boolean] whether the annotation is an “InVideo Programming”.

# File lib/yt/models/annotation.rb, line 52
def has_invideo_programming?
  type == 'promotion' || type == 'branding'
end
starts_after?(seconds) click to toggle source

@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
# File lib/yt/models/annotation.rb, line 61
def starts_after?(seconds)
  timestamps.first > seconds if timestamps.any?
end
starts_before?(seconds) click to toggle source

@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
# File lib/yt/models/annotation.rb, line 70
def starts_before?(seconds)
  timestamps.first < seconds if timestamps.any?
end
text() click to toggle source

@return [String] the textual content of the annotation.

# File lib/yt/models/annotation.rb, line 75
def text
  @text ||= @data['TEXT'] || ''
end

Private Instance Methods

bottom() click to toggle 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
positions() click to toggle source
# File lib/yt/models/annotation.rb, line 121
def positions
  @positions ||= Array.wrap region['rectRegion'] || region['anchoredRegion']
end
region() click to toggle source
# File lib/yt/models/annotation.rb, line 125
def region
  @region ||= segment.fetch 'movingRegion', {}
end
timestamp_of(position) click to toggle 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
timestamps() click to toggle 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
top() click to toggle source
# File lib/yt/models/annotation.rb, line 97
def top
  @top ||= positions.map{|pos| pos['y'].to_f}.max
end
url() click to toggle source
# File lib/yt/models/annotation.rb, line 91
def url
  @url ||= action.fetch 'url', {}
end