Class: ArticleJSON::Export::AMP::CustomElementLibraryResolver

Inherits:
Object
  • Object
show all
Defined in:
lib/article_json/export/amp/custom_element_library_resolver.rb

Overview

AMP uses custom HTML tags for elements like iframes or embedded youtube videos. These elements each require a javascript to be loaded to be properly rendered by the browser. This class resolves the custom tags to a list of javascript libraries which then can be included on the page.

Instance Method Summary collapse

Constructor Details

#initialize(custom_element_tags) ⇒ CustomElementLibraryResolver

Returns a new instance of CustomElementLibraryResolver.

Parameters:

  • custom_element_tags (Array[Symbol])


10
11
12
# File 'lib/article_json/export/amp/custom_element_library_resolver.rb', line 10

def initialize(custom_element_tags)
  @custom_element_tags = custom_element_tags
end

Instance Method Details

#script_tagsArray[String]

Return all custom library script tags required for the given custom element tags

Returns:

  • (Array[String])


26
27
28
29
30
31
32
33
34
# File 'lib/article_json/export/amp/custom_element_library_resolver.rb', line 26

def script_tags
  sources.map do |custom_element_tag, src|
    <<-HTML.gsub(/\s+/, ' ').strip
        <script async
                custom-element="#{custom_element_tag}"
                src="#{src}"></script>
    HTML
  end
end

#sourcesHash[Symbol => String]

List of all custom tags with their library source URI

Returns:

  • (Hash[Symbol => String])


16
17
18
19
20
21
# File 'lib/article_json/export/amp/custom_element_library_resolver.rb', line 16

def sources
  @custom_element_tags.each_with_object({}) do |custom_element, mapping|
    src = custom_element_script_mapping(custom_element)
    mapping[custom_element] = src if src
  end
end