Class: Graphite::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/graphite_builder.rb

Overview

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

Defined Under Namespace

Classes: NoTargetsDefined, UnknownFunctionSignature

Instance Method Summary collapse

Constructor Details

#initialize(args = {}, &block) ⇒ Builder

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



20
21
22
23
24
25
26
# File 'lib/graphite_builder.rb', line 20

def initialize(args={}, &block)
  @args = args
  @targets = []
  if block
    self.instance_eval(&block)
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
# 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

Instance Method Details

#asPercent(is, of) ⇒ Object



40
41
42
# File 'lib/graphite_builder.rb', line 40

def asPercent(is, of)
  array_argument_wrapper('asPercent', is, of)
end

#format(value) ⇒ Object



32
33
34
# File 'lib/graphite_builder.rb', line 32

def format(value)
  @args[:format] = value
end

#renderObject



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object



44
45
46
# File 'lib/graphite_builder.rb', line 44

def secondYAxis(arg)
  single_argument_wrapper('secondYAxis',arg)
end

#stacked(arg) ⇒ Object



48
49
50
# File 'lib/graphite_builder.rb', line 48

def stacked(arg)
  single_argument_wrapper('stacked',arg)
end

#sumSeries(*args) ⇒ Object



36
37
38
# File 'lib/graphite_builder.rb', line 36

def sumSeries(*args)
  array_argument_wrapper('sumSeries', args)
end

#target(value) ⇒ Object



28
29
30
# File 'lib/graphite_builder.rb', line 28

def target(value)
  @targets << value
end