Class: Archangel::Liquid::Tags::NoembedTag

Inherits:
ApplicationTag
  • Object
show all
Defined in:
lib/archangel/liquid/tags/noembed_tag.rb

Overview

Noembed custom tag for Liquid

For more information about Noembed, see noembed.com/

Example

{% noembed "https://www.youtube.com/watch?v=-X2atEH7nCg" %}
{% noembed "https://vimeo.com/70707344" %}
{% noembed "https://vimeo.com/70707344" remote: true %}
{% noembed "https://vimeo.com/70707344" maxwidth:600 %}
{% noembed "https://vimeo.com/70707344" maxheight:400 %}

Constant Summary

Constants inherited from ApplicationTag

ApplicationTag::ASSET_ATTRIBUTES_SYNTAX, ApplicationTag::ASSET_SYNTAX, ApplicationTag::KEY_VALUE_ATTRIBUTES_SYNTAX, ApplicationTag::SLUG_ATTRIBUTES_SYNTAX, ApplicationTag::SLUG_SYNTAX, ApplicationTag::URL_ATTRIBUTES_SYNTAX

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ NoembedTag

Noembed for Liquid


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/archangel/liquid/tags/noembed_tag.rb', line 29

def initialize(tag_name, markup, options)
  super

  match = URL_ATTRIBUTES_SYNTAX.match(markup)

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

  @url = ::Liquid::Variable.new(match[:url], options).name
  @attributes = { remote: false }

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

Instance Method Details

#render(_context) ⇒ String

Render the Widget


52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/archangel/liquid/tags/noembed_tag.rb', line 52

def render(_context)
  return if url.blank?

  noembed_id = "noembed-#{Digest::MD5.hexdigest(url)}"

  content = if %w[1 true yes y].include?(attributes[:remote].to_s)
              build_js_request(noembed_id)
            else
              build_ruby_request
            end

  (:div, raw(content), id: noembed_id, class: "noembed")
end