Class: Yt::Models::Annotation

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

Overview

Provides methods to interact with YouTube annotations.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Annotation

Note:

YouTube API V3 does not provide access to video annotations, therefore the XML endpoint is used to retrieve them and its response is passed to the Annotation initializer.

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



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

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.



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

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.



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

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”.



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

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.



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

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.



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

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.



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

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”.



36
37
38
# File 'lib/yt/models/annotation.rb', line 36

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

Parameters:

  • seconds (Numeric)

    the number of seconds

Returns:

  • (Boolean)

    whether the annotation starts after the number of seconds indicated.



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

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

Parameters:

  • seconds (Numeric)

    the number of seconds

Returns:

  • (Boolean)

    whether the annotation starts before the number of seconds indicated.



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

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