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.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/locomotive/liquid/tags/consume.rb', line 18

def initialize(tag_name, markup, tokens, context)
  if markup =~ Syntax
    @target = $1
    @url = $2
    @is_var = false
    if @url.match(::Liquid::QuotedString)
      @url.gsub!(/['"]/, '')
    elsif @url.match(::Liquid::VariableSignature)
      @is_var = true
    else
      raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
    end
    @options = {}
    markup.scan(::Liquid::TagAttributes) do |key, value|
      @options[key] = value if key != 'http'
    end
    @expires_in = (@options.delete('expires_in') || 0).to_i
    @cache_key = Digest::SHA1.hexdigest(@target)
  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



43
44
45
46
47
48
# File 'lib/locomotive/liquid/tags/consume.rb', line 43

def render(context)
  if @is_var
    @url = context[@url]
  end
  render_all_and_cache_it(context)
end