Sha256: ef3cbeadf2c31614debc9116067a0613dd09c9d3200108aa46366c29b7265e9c

Contents?: true

Size: 1.56 KB

Versions: 1

Compression:

Stored size: 1.56 KB

Contents

require 'cgi'
require 'rest_client'

module Slate
  class Render
    def initialize(options={})
      @target = options[:target]
      @from   = options[:from]
      @until  = options[:until]
      @functions = []
    end

    def url(format=:png)
      options = url_options.merge("format" => format.to_s)
      "#{Configuration.instance.endpoint}/render?#{params(options)}"
    end

    def download(format=:png)
      RestClient.get url(format)
    end

    def add_function(*function)
      if function.size > 1
        arguments = function[1..-1]
        @functions << [function.first.to_sym, arguments]
      else
        @functions << function.first.to_sym
      end

      target
    end

    def target
      target = @target
      @functions.each do |function|
        if function.is_a? Symbol
          target = %Q{#{function}(#{target})}
        else
          args = arguments(function.last).join(",")
          target = %Q{#{function.first}(#{target},#{args})}
        end
      end

      target
    end

    private

    def arguments(args=[])
      args.map do |arg|
        if arg.is_a?(Numeric)
          arg.to_s
        elsif arg.is_a?(Slate::Render)
          arg.send(:target)
        else
          %Q{"#{arg}"}
        end
      end
    end

    def url_options
      options = {
        "target" => target
      }
      options["from"]  = @from if @from
      options["until"] = @until if @until

      options
    end

    def params(options={})
      options.map do |key,value|
        "#{CGI.escape(key)}=#{CGI.escape(value)}"
      end.join("&")
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
slate-0.0.1 lib/slate/render.rb