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

Inherits:
Liquid::Block
  • Object
show all
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::QuotedString}|#{::Liquid::VariableSignature}+)(.*)?/o

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, options) ⇒ Consume

Returns a new instance of Consume.



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 20

def initialize(tag_name, markup, options)
  if markup =~ Syntax
    @name = $1.to_s

    self.prepare_url($2)
    @default_api_options = parse_options_from_string($3)
  else
    raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
  end

  super
end

Instance Method Details

#render(context) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/locomotive/steam/liquid/tags/consume.rb', line 33

def render(context)
  self.set_api_options(context)

  if instance_variable_defined?(:@variable_name)
    @url = context[@variable_name]
  end

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