2010-07-23 20:09:54 +00:00
|
|
|
module Locomotive
|
2010-07-06 00:05:47 +00:00
|
|
|
module Liquid
|
|
|
|
module Tags
|
|
|
|
# Consume web services as easy as pie directly in liquid !
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# {% consume blog from 'http://nocoffee.tumblr.com/api/read.json?num=3' username: 'john', password: 'easy', format: 'json' %}
|
|
|
|
# {% for post in blog.posts %}
|
|
|
|
# {{ post.title }}
|
|
|
|
# {% endfor %}
|
|
|
|
# {% endconsume %}
|
|
|
|
#
|
|
|
|
class Consume < ::Liquid::Block
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
Syntax = /(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::QuotedString}+)/
|
|
|
|
|
2010-08-20 00:31:01 +00:00
|
|
|
def initialize(tag_name, markup, tokens, context)
|
2010-07-06 00:05:47 +00:00
|
|
|
if markup =~ Syntax
|
|
|
|
@target = $1
|
|
|
|
@url = $2.gsub(/['"]/, '')
|
|
|
|
@options = {}
|
|
|
|
markup.scan(::Liquid::TagAttributes) do |key, value|
|
|
|
|
@options[key] = value if key != 'http'
|
|
|
|
end
|
|
|
|
else
|
|
|
|
raise ::Liquid::SyntaxError.new("Syntax Error in 'consume' - Valid syntax: consume <var> from \"<url>\" [username: value, password: value]")
|
|
|
|
end
|
|
|
|
|
2010-07-23 20:09:54 +00:00
|
|
|
super
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def render(context)
|
|
|
|
context.stack do
|
|
|
|
context.scopes.last[@target.to_s] = Locomotive::Httparty::Webservice.consume(@url, @options.symbolize_keys)
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
render_all(@nodelist, context)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-06 00:05:47 +00:00
|
|
|
::Liquid::Template.register_tag('consume', Consume)
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|