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})/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of ConnectedContentTag.



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

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

Instance Attribute Details

#argumentsObject (readonly)

Returns the value of attribute arguments.



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

def arguments
  @arguments
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



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

def tag_name
  @tag_name
end

#urlObject (readonly)

Returns the value of attribute url.



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

def url
  @url
end

Instance Method Details

#metadataObject



44
45
46
47
48
49
50
51
52
# File 'lib/txbr/liquid/connected_content_tag.rb', line 44

def 
  uri = URI.parse(url)

  .new(
    CGI.parse(uri.query)
      .each_with_object({}) { |(k, v), ret| ret[k] = v.first }
      .merge('prefix' => prefix)
  )
end

#prefixObject



34
35
36
37
# File 'lib/txbr/liquid/connected_content_tag.rb', line 34

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

#render(context) ⇒ Object



39
40
41
42
# File 'lib/txbr/liquid/connected_content_tag.rb', line 39

def render(context)
  @url = ::Liquid::Template.parse(url).render(context)
  ''
end