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.

Parameters:

  • options (Hash) (defaults to: {})

    the options to initialize an 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

Returns whether the text box surrounding the annotation is completely in the top y% of the video frame.

Parameters:

  • y (Integer)

    Vertical position in the Youtube video (0 to 100)

Returns:

  • (Boolean)

    whether the text box surrounding the annotation is completely in the top y% of the video frame.



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

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

#below?(y) ⇒ Boolean

Returns whether the text box surrounding the annotation is completely in the bottom y% of the video frame.

Parameters:

  • y (Integer)

    Vertical position in the Youtube video (0 to 100)

Returns:

  • (Boolean)

    whether the text box surrounding the annotation is completely in the bottom y% of the video frame.



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

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

#has_invideo_programming?Boolean

Returns whether the annotation is an “InVideo Programming”.

Returns:

  • (Boolean)

    whether the annotation is an “InVideo Programming”.



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

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

Returns whether the annotation includes a link to a playlist, or to a video embedded in a playlist.

Returns:

  • (Boolean)

    whether the annotation includes a link to a playlist, or to a video embedded in a playlist.



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

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

Returns whether the annotation includes a link that will open in the current browser window.

Returns:

  • (Boolean)

    whether the annotation includes a link that will open in the current browser window.



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

def has_link_to_same_window?
  link_target == 'current'
end

Returns whether the annotation includes a link to subscribe.

Returns:

  • (Boolean)

    whether the annotation includes a link to subscribe.



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

Returns whether the annotation includes a link to a video, either directly in the text, or as an “Invideo featured video”.

Returns:

  • (Boolean)

    whether the annotation includes a link to a video, either directly in the text, or as an “Invideo featured video”.



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.

Parameters:

  • seconds (Numeric)

    the number of seconds

Returns:

  • (Boolean)

    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.

Parameters:

  • seconds (Numeric)

    the number of seconds

Returns:

  • (Boolean)

    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

Returns the textual content of the annotation.

Returns:

  • (String)

    the textual content of the annotation.



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

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