Sha256: f02b394f9e65c8fdd19cdb12c573a0c961e7985ce474c2bb242d430e4cb3f72a
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 KB
Contents
require 'cgi' require 'rest-client' module Slate class Graph attr_accessor :from, :until # Public: Creates a new graph instance. # # client - A configured Slate::Client instance. # options - Options for creating the graph (default: {}): # :from - The String to start the data in the graph (optional). # :until - The String to end the data in the graph (optional). # # Examples # # Slate::Graph.new(client) # # Slate::Graph.new(client, { from: "-1d" }) # # Slate::Graph.new(client, { until: "-1h" }) def initialize(client, options={}) @client = client @from = options[:from] @until = options[:until] @targets = options[:targets] || [] end def <<(target) @targets << target end # Public: Generate a URL to the image of the graph. # # format - The format of the graph to return, as a Symbol (default: :png). # # Examples # # url # # => "http://example.com/render?format=png" # # url(:svg) # # => "http://example.com/render?format=svg" # # Returns the URL String. def url(format=:png) options = url_options.push(["format", format.to_s]) "#{@client.endpoint}/render?#{params(options)}" end def download(format=:png) RestClient.get url(format) end private def url_options options = [] options += @targets.map { |t| ["target", t.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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
slate-1.0.3 | lib/slate/graph.rb |