Sha256: 149d36f7eb3f074bdb45bd5af048f97feea88443cc6c881aafea88fec8ee81ba

Contents?: true

Size: 1.71 KB

Versions: 4

Compression:

Stored size: 1.71 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', 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
            @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

        def render(context)
          render_all_and_cache_it(context)
        end

        protected

        def render_all_and_cache_it(context)
          Rails.cache.fetch(@cache_key, :expires_in => @expires_in) do
            context.stack do
              context.scopes.last[@target.to_s] = Locomotive::Httparty::Webservice.consume(@url, @options.symbolize_keys)

              render_all(@nodelist, context)
            end
          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.4.beta3 lib/locomotive/liquid/tags/consume.rb
locomotive_cms-0.0.4.beta2 lib/locomotive/liquid/tags/consume.rb
locomotive_cms-0.0.4.beta1 lib/locomotive/liquid/tags/consume.rb
locomotive_cms-0.0.4 lib/locomotive/liquid/tags/consume.rb