Sha256: 8f0cfe9f4966cac9638b0553157b383860c8a7f1e0cc665bf9d6cc15d00ad0af

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

module Locomotive
  module Builder
    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', expires_in: 3000 %}
        #   {% 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, context)
            if markup =~ Syntax
              @target = $1
              @url = $2.gsub(/['"]/, '')
              @options = {}
              markup.scan(::Liquid::TagAttributes) do |key, value|
                @options[key] = value if key != 'http'
              end
              @options.delete('expires_in')
            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
              _response = nil

              begin
                _response = Locomotive::Builder::Httparty::Webservice.consume(@url, @options.symbolize_keys)
              rescue Exception => e
                _response = { 'error' => e.message.to_s }.to_liquid
              end

              context.scopes.last[@target.to_s] = _response

              render_all(@nodelist, context)
            end
          end

        end

        ::Liquid::Template.register_tag('consume', Consume)
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
locomotivecms_builder-1.0.0.alpha8 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha7 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha6 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha5 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha4 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha3 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha2 lib/locomotive/builder/liquid/tags/consume.rb
locomotivecms_builder-1.0.0.alpha1 lib/locomotive/builder/liquid/tags/consume.rb