class Archangel::Liquid::Tags::YoutubeTag

YouTube custom tag for Liquid

Example

Given the YouTube URL https://www.youtube.com/watch?v=NOGEyBeoBGM
{% youtube "NOGEyBeoBGM" %}
{% youtube "NOGEyBeoBGM" width:800 height:600 %}
{% youtube "NOGEyBeoBGM" id:"my_video" class:"my-video" %}
{% youtube "NOGEyBeoBGM" autoplay:1 %}
{% youtube "NOGEyBeoBGM" captions: %}
{% youtube "NOGEyBeoBGM" loop:1 %}
{% youtube "NOGEyBeoBGM" annotations:1 %}
{% youtube "NOGEyBeoBGM" start:10 %}
{% youtube "NOGEyBeoBGM" end:60 %}
{% youtube "NOGEyBeoBGM" showinfo:0 %}
{% youtube "NOGEyBeoBGM" allowfullscreen: %}
{% youtube "NOGEyBeoBGM" allowtransparency:0 %}
{% youtube "NOGEyBeoBGM" frameborder:4 %}

Attributes

attributes[R]
key[R]

Public Class Methods

new(tag_name, markup, options) click to toggle source

YouTube video embed for Liquid

@param tag_name [String] the Liquid tag name @param markup [String] the passed options @param options [Object] options

Calls superclass method
# File lib/archangel/liquid/tags/youtube_tag.rb, line 33
def initialize(tag_name, markup, options)
  super

  match = SLUG_ATTRIBUTES_SYNTAX.match(markup)

  if match.blank?
    raise ::Liquid::SyntaxError, Archangel.t("errors.syntax.youtube")
  end

  @key = ::Liquid::Variable.new(match[:slug], options).name
  @attributes = {}

  match[:attributes].scan(KEY_VALUE_ATTRIBUTES_SYNTAX) do |key, value|
    @attributes[key.to_sym] = ::Liquid::Expression.parse(value)
  end
end

Public Instance Methods

render(_context) click to toggle source

Render the YouTube video

@param _context [Object] the Liquid context @return [String] the rendered video

# File lib/archangel/liquid/tags/youtube_tag.rb, line 56
def render(_context)
  return if key.blank?

  content_tag(:iframe, "", video_attributes)
end

Protected Instance Methods

video_attributes() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 66
def video_attributes
  {
    src: video_url,
    width: video_width,
    height: video_height
  }.merge(video_fetch_attributes)
end
video_fetch_attributes() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 74
def video_fetch_attributes
  {
    id: nil,
    class: nil,
    style: nil,
    frameborder: 0,
    allowtransparency: "true",
    allowFullScreen: "allowFullScreen"
  }.each_with_object({}) do |(key, value), hash|
    hash[key] = attributes.fetch(key.to_s.downcase.to_sym, value)
  end
end
video_height() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 123
def video_height
  attributes.fetch(:height, 360)
end
video_url() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 87
def video_url
  [
    "https://www.youtube.com/embed/#{key}",
    video_url_params
  ].join("?")
end
video_url_fetch_params() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 106
def video_url_fetch_params
  {
    color: "red",
    autoplay: 0,
    loop: 0,
    showinfo: 1,
    start: nil,
    end: nil
  }.each_with_object({}) do |(key, value), hash|
    hash[key] = attributes.fetch(key.to_sym, value)
  end
end
video_url_params() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 94
def video_url_params
  {
    ecver: 1,
    autohide: 2,
    cc_load_policy: attributes.fetch(:captions, 0),
    iv_load_policy: attributes.fetch(:annotations, 0),
    width: video_width,
    height: video_height
  }.merge(video_url_fetch_params)
    .compact.reject { |_, value| value.blank? }.to_query
end
video_width() click to toggle source
# File lib/archangel/liquid/tags/youtube_tag.rb, line 119
def video_width
  attributes.fetch(:width, 640)
end