Class: Txbr::Liquid::ConnectedContentTag

Inherits:
Liquid::Tag
  • Object
show all
Defined in:
lib/txbr/liquid/connected_content_tag.rb

Constant Summary collapse

IDENTIFIER =
::Liquid::Lexer::IDENTIFIER
PARSE_RE =

This is a regular expression to pull out the variable in which to store the value returned from the call made by the connected_content filter. For example, if connected_content makes a request to foo.com and is told to store the results in a variable called “strings”, the API response will then be accessible via the normal Liquid variable mechanism, i.e. {…}. Say the API at foo.com returned something like “bar”:“baz”, then the template might contain Txbr::Liquid::ConnectedContentTag.{strings{strings.bar}, which would print out “baz”.

/(:#{IDENTIFIER})\s+(#{IDENTIFIER})/
ASSIGNS_RE =
/\{\{(?:(?!\}\}).)*\}\}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, arg, _context = nil) ⇒ ConnectedContentTag

Returns a new instance of ConnectedContentTag.



30
31
32
33
# File 'lib/txbr/liquid/connected_content_tag.rb', line 30

def initialize(tag_name, arg, _context = nil)
  @tag_name = tag_name
  @url, @arguments = parse_arg(arg)
end

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



28
29
30
# File 'lib/txbr/liquid/connected_content_tag.rb', line 28

def arguments
  @arguments
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



28
29
30
# File 'lib/txbr/liquid/connected_content_tag.rb', line 28

def tag_name
  @tag_name
end

#urlObject (readonly)

Returns the value of attribute url.



28
29
30
# File 'lib/txbr/liquid/connected_content_tag.rb', line 28

def url
  @url
end

Instance Method Details

#metadataObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/txbr/liquid/connected_content_tag.rb', line 48

def 
  @metadata ||= begin
    query_hash = CGI.parse(uri.query)

    Metadata.new(
      query_hash
        .each_with_object({}) { |(k, v), ret| ret[k] = v.first }
        .merge('prefix' => prefix)
    )
  end
end

#prefixObject



43
44
45
46
# File 'lib/txbr/liquid/connected_content_tag.rb', line 43

def prefix
  # we want to blow up if "save" isn't present
  arguments.fetch('save').first
end

#render(context) ⇒ Object

this method is called inside Txbr::ContentTag#metadata



36
37
38
39
40
41
# File 'lib/txbr/liquid/connected_content_tag.rb', line 36

def render(context)
  @metadata_hash =
    Metadata::ASSIGNMENTS.each_with_object({}) do |assignment, ret|
      ret[assignment] = context[assignment]
    end
end