Sha256: 3917e940cf33f65f3985872cf24e233cdf5851ca2b8a4ef65bfa8439ecf6cf78

Contents?: true

Size: 1.08 KB

Versions: 11

Compression:

Stored size: 1.08 KB

Contents

require 'yt/annotations/base'

module Yt
  module Annotations
    # A Card annotation is different from a Note annotation in the way data is
    # represented in the XML under a new 'data' key with JSON-formatted content.
    class Card < Base
      # @param [Hash] data the Hash representation of the XML data returned by
      #   YouTube for each card of a video.
      def initialize(data = {})
        json = JSON.parse data['data']
        @text = text_in json
        @starts_at = json['start_ms'] / 1000.0
        @ends_at = ends_at_in json
        @link = to_link data, json
      end

    private

      def text_in(json)
        json['teaser_text']
      end

      def ends_at_in(json)
        (json['start_ms'] + json['teaser_duration_ms']) / 1000.0
      end

      def to_link(data, json)
        link_type = case json['card_type']
          when 'collaborator' then :channel
          when 'playlist' then :playlist
          when 'video' then :video
          when 'simple' then :website
        end

        {url: json['url'], new_window: true, type: link_type}
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

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