Class: Locomotive::Steam::Liquid::Tags::Consume

Inherits:
Liquid::Block
  • Object
show all
Includes:
Locomotive::Steam::Liquid::Tags::Concerns::Attributes
Defined in:
lib/locomotive/steam/liquid/tags/consume.rb

Overview

Consume web services as easy as pie directly in liquid!

Usage:

consume blog from ‘nocoffee.tumblr.com/api/read.json?num=3’ username: ‘john’, password: ‘easy’, format: ‘json’, expires_in: 3000 %

{% for post in blog.posts %}
  {{ post.title }}
{% endfor %}

endconsume %

Constant Summary collapse

Syntax =
/(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::QuotedFragment}+),?(.+)?/o.freeze

Instance Attribute Summary collapse

Attributes included from Locomotive::Steam::Liquid::Tags::Concerns::Attributes

#attributes, #raw_attributes

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ Consume

Returns a new instance of Consume.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 24

def initialize(tag_name, markup, options)
  super

  if markup =~ Syntax
    @variable_name, @url, attributes = $1.to_s, ::Liquid::Expression.parse($2), $3

    parse_attributes(attributes)
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
  end
end

Instance Attribute Details

#expires_inObject (readonly)

Returns the value of attribute expires_in.



22
23
24
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 22

def expires_in
  @expires_in
end

#urlObject (readonly)

Returns the value of attribute url.



22
23
24
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 22

def url
  @url
end

#variable_nameObject (readonly)

Returns the value of attribute variable_name.



22
23
24
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 22

def variable_name
  @variable_name
end

Instance Method Details

#render(context) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 36

def render(context)
  evaluate_attributes(context)

  # attributes will become the options which will be passed to the service.
  # we don't want the expires_in option to be part of it.
  @expires_in = attributes.delete(:expires_in)&.to_i

  # the URL can come from a variable
  @url = context.evaluate(url)

  if url.blank?
    Locomotive::Common::Logger.error "A consume tag can't call an empty URL."
    ''
  else
    render_all_and_cache_it(context) { |_context| super(_context) }
  end
end