class Graphite::Builder

Encapsulates a small DSL for enabling the cut-n-paste of data from the graphite UI and interpolating/ modifying them for inclusion in a server side template

Public Class Methods

new(args={}, &block) click to toggle source

args are optional arguments for constructing the url opts are data to retrieve

# File lib/graphite_builder.rb, line 20
def initialize(args={}, &block)
  @args = args
  @targets = []
  if block
    self.instance_eval(&block)
  end
end

Public Instance Methods

asPercent(is, of) click to toggle source
# File lib/graphite_builder.rb, line 40
def asPercent(is, of)
  array_argument_wrapper('asPercent', is, of)
end
format(value) click to toggle source
# File lib/graphite_builder.rb, line 32
def format(value)
  @args[:format] = value
end
method_missing(meth,*args,&block) click to toggle source
# File lib/graphite_builder.rb, line 52
def method_missing(meth,*args,&block)
  if args.length == 2
    if meth == :legend
      meth = :alias
    end
    quoted_array_argument_wrapper(meth, args)
  elsif args.length == 1
    @args[meth.to_sym] = args.first
  else
    raise ::Graphite::Builder::UnknownFunctionSignature.new("#{meth}(#{args.join(',')})")
  end
end
render() click to toggle source
# File lib/graphite_builder.rb, line 65
def render
  raise ::Graphite::Builder::NoTargetsDefined.new if @targets.empty?
  '<img ' <<
    (@args.has_key?(:height) ? "height=\"#{@args[:height]}\" " : '') <<
      (@args.has_key?(:width) ? "width=\"#{@args[:width]}\" " : '') <<
        'src="' <<
          @args.delete(:base_url) << 
            '?' <<
              (@args.map do |k ,v|
                if v.is_a?(Array)
                  v.map { |item| url_param(k, item) }.join("&")
                else
                  url_param(k, v)
                end
              end <<
                @targets.map {|target| "target=#{target}" }).join('&') <<
                  '"/>'
end
secondYAxis(arg) click to toggle source
# File lib/graphite_builder.rb, line 44
def secondYAxis(arg)
  single_argument_wrapper('secondYAxis',arg)
end
stacked(arg) click to toggle source
# File lib/graphite_builder.rb, line 48
def stacked(arg)
  single_argument_wrapper('stacked',arg)
end
sumSeries(*args) click to toggle source
# File lib/graphite_builder.rb, line 36
def sumSeries(*args)
  array_argument_wrapper('sumSeries', args)
end
target(value) click to toggle source
# File lib/graphite_builder.rb, line 28
def target(value)
  @targets << value
end

Private Instance Methods

array_argument_wrapper(meth, *args) click to toggle source
# File lib/graphite_builder.rb, line 90
def array_argument_wrapper(meth, *args)
  "#{meth}(#{args.join(",")})"
end
quoted_array_argument_wrapper(meth, *args) click to toggle source
# File lib/graphite_builder.rb, line 94
def quoted_array_argument_wrapper(meth, *args)
  args = args[0] if args[0].is_a? Array
  "#{meth}(#{args.shift},#{args.map { |a| "'#{CGI::escape(a.to_s)}'"}.join(',')})"
end
single_argument_wrapper(meth, arg) click to toggle source
# File lib/graphite_builder.rb, line 86
def single_argument_wrapper(meth, arg)
  "#{meth}(#{arg})"
end
url_param(param, value) click to toggle source
# File lib/graphite_builder.rb, line 99
def url_param(param, value)
  "#{URI.escape(param.to_s)}=#{CGI::escape(value.to_s)}"
end