Sha256: 4f77da39bb5ea4c75438b70ca33cea540338f283490e3ee0d38cacbb141b916e

Contents?: true

Size: 1.39 KB

Versions: 4

Compression:

Stored size: 1.39 KB

Contents

module Locomotive  
  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
        
        Syntax = /(#{::Liquid::VariableSignature}+)\s*from\s*(#{::Liquid::QuotedString}+)/

        def initialize(tag_name, markup, tokens)
          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

          super      
        end

        def render(context)
          context.stack do
            context.scopes.last[@target.to_s] = Locomotive::Httparty::Webservice.consume(@url, @options.symbolize_keys)
            
            render_all(@nodelist, context)
          end
        end 
        
      end
      
      ::Liquid::Template.register_tag('consume', Consume)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
locomotive_cms-0.0.3.3 lib/locomotive/liquid/tags/consume.rb
locomotive_cms-0.0.3.1 lib/locomotive/liquid/tags/consume.rb
locomotive_cms-0.0.2.9 lib/locomotive/liquid/tags/consume.rb
locomotive_cms-0.0.2.8 lib/locomotive/liquid/tags/consume.rb