Class: Yt::Models::Annotation

Inherits:
Base
  • Object
show all
Defined in:
lib/yt/models/annotation.rb

Overview

Note:

YouTube API V3 does not provide access to video annotations, therefore a legacy XML endpoint is used to retrieve annotations.

Provides methods to interact with YouTube annotations.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Annotation

Returns a new instance of Annotation.

Options Hash (options):

  • :data (String)

    The XML representation of an annotation



10
11
12
# File 'lib/yt/models/annotation.rb', line 10

def initialize(options = {})
  @data = options[:data]
end

Instance Method Details

#above?(y) ⇒ Boolean



17
18
19
# File 'lib/yt/models/annotation.rb', line 17

def above?(y)
  top && top < y
end

#below?(y) ⇒ Boolean



24
25
26
# File 'lib/yt/models/annotation.rb', line 24

def below?(y)
  bottom && bottom > y
end

#has_invideo_programming?Boolean



52
53
54
# File 'lib/yt/models/annotation.rb', line 52

def has_invideo_programming?
  type == 'promotion' || type == 'branding'
end


41
42
43
# File 'lib/yt/models/annotation.rb', line 41

def has_link_to_playlist?
  link_class == '2' || text.include?('&list=')
end


47
48
49
# File 'lib/yt/models/annotation.rb', line 47

def has_link_to_same_window?
  link_target == 'current'
end


29
30
31
# File 'lib/yt/models/annotation.rb', line 29

def has_link_to_subscribe?(options = {}) # TODO: options for which videos
  link_class == '5'
end


35
36
37
# 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

#starts_after?(seconds) ⇒ Boolean

Note:

This is broken for invideo programming, because they do not have the timestamp in the region, but in the “data” field

Returns whether the annotation starts after the number of seconds indicated.



61
62
63
# File 'lib/yt/models/annotation.rb', line 61

def starts_after?(seconds)
  timestamps.first > seconds if timestamps.any?
end

#starts_before?(seconds) ⇒ Boolean

Note:

This is broken for invideo programming, because they do not have the timestamp in the region, but in the “data” field

Returns whether the annotation starts before the number of seconds indicated.



70
71
72
# File 'lib/yt/models/annotation.rb', line 70

def starts_before?(seconds)
  timestamps.first < seconds if timestamps.any?
end

#textString



75
76
77
# File 'lib/yt/models/annotation.rb', line 75

def text
  @text ||= @data['TEXT'] || ''
end