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

Inherits:
Liquid::Block
  • Object
show all
Defined in:
lib/locomotive/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}+)(.*)?/

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, markup, tokens, context) ⇒ Consume

Returns a new instance of Consume.



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

def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    @target = $1

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

  @local_cache_key = self.hash

  super
end

Instance Method Details

#render(context) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/locomotive/liquid/tags/consume.rb', line 34

def render(context)
  self.set_api_options(context)

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

  render_all_and_cache_it(context)
end