Sha256: 8b92b780c7af5f1954b63b9b6cb0e5df7a9e6b764ae65221b8acd4de62be1881

Contents?: true

Size: 1.31 KB

Versions: 11

Compression:

Stored size: 1.31 KB

Contents

require 'yt/annotations/base'

module Yt
  module Annotations
    # A Note annotation is just a basic annotation with text, start/end time
    # and an optional link. It's shaped like a Post-It with a curved corner.
    class Note < Base
      # @param [Hash] data the Hash representation of the XML data returned by
      #   YouTube for each annotation of a video.
      def initialize(data = {})
        @text = data['TEXT']
        @starts_at = to_seconds regions_of(data)[0]['t']
        @ends_at = to_seconds regions_of(data)[1]['t']
        @link = to_link data.fetch('action', {})['url']
      end

    private

      def regions_of(data)
        data['segment']['movingRegion']['rectRegion']
      end

      def to_seconds(timestamp)
        timestamp.split(':').map(&:to_f).inject(0) {|sum, n| 60 * sum + n}
      end

      def to_link(url)
        return unless url
        new_window = url['target'] != 'current'
        type = case url['link_class']
          when '1' then :video
          when '2' then :playlist
          when '3' then :channel
          when '4' then :profile
          when '5' then :subscribe
          when '6' then :website
          when '8' then :crowdfunding
          when '12' then :website
        end
        {url: url['value'], new_window: new_window, type: type}
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
yt-annotations-1.4.2 lib/yt/annotations/note.rb
yt-annotations-1.4.1 lib/yt/annotations/note.rb
yt-annotations-1.4.0 lib/yt/annotations/note.rb
yt-annotations-1.3.2 lib/yt/annotations/note.rb
yt-annotations-1.3.1 lib/yt/annotations/note.rb
yt-annotations-1.3.0 lib/yt/annotations/note.rb
yt-annotations-1.2.3 lib/yt/annotations/note.rb
yt-annotations-1.2.2 lib/yt/annotations/note.rb
yt-annotations-1.2.1 lib/yt/annotations/note.rb
yt-annotations-1.2.0 lib/yt/annotations/note.rb
yt-annotations-1.1.0 lib/yt/annotations/note.rb