Sha256: cac54e7e813babaa296f061d199ebd2b6dc02c50c7ccfc3a7a52a2759b5b64ab

Contents?: true

Size: 892 Bytes

Versions: 5

Compression:

Stored size: 892 Bytes

Contents

require 'cgi'
require 'rest-client'

module Slate
  class Graph
    attr_accessor :from, :until

    def initialize(options={})
      @from    = options[:from]
      @until   = options[:until]
    end

    def <<(target)
      @target = target
    end

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

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

    private

    def url_options
      options = []
      options << ["target", @target.to_s]
      options << ["from", @from]   if @from
      options << ["until", @until] if @until

      options
    end

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
slate-0.2.1 lib/slate/graph.rb
slate-0.2.0 lib/slate/graph.rb
slate-0.1.0 lib/slate/graph.rb
slate-0.0.5 lib/slate/graph.rb
slate-0.0.4 lib/slate/graph.rb