Class: Wavefront::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/wavefront/client.rb,
lib/wavefront/client/version.rb

Constant Summary collapse

DEFAULT_PERIOD_SECONDS =
600
DEFAULT_PATH =
'/chart/api'
DEFAULT_FORMAT =
:raw
FORMATS =
[ :raw, :ruby, :graphite, :highcharts ]
GRANULARITIES =
%w( s m h d )
VERSION =
"0.5.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(token, host = DEFAULT_HOST, debug = false) ⇒ Client

Returns a new instance of Client.



35
36
37
38
39
# File 'lib/wavefront/client.rb', line 35

def initialize(token, host=DEFAULT_HOST, debug=false)
  @headers = {'X-AUTH-TOKEN' => token}
  @base_uri = URI::HTTPS.build(:host => host, :path => DEFAULT_PATH)
  debug(debug)
end

Instance Attribute Details

#base_uriObject (readonly)

Returns the value of attribute base_uri.



33
34
35
# File 'lib/wavefront/client.rb', line 33

def base_uri
  @base_uri
end

#headersObject (readonly)

Returns the value of attribute headers.



33
34
35
# File 'lib/wavefront/client.rb', line 33

def headers
  @headers
end

Instance Method Details

#query(query, granularity = 'm', options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/wavefront/client.rb', line 41

def query(query, granularity='m', options={})

  options[:end_time] ||= Time.now
  options[:start_time] ||= options[:end_time] - DEFAULT_PERIOD_SECONDS
  options[:response_format] ||= DEFAULT_FORMAT

  [ options[:start_time], options[:end_time] ].each { |o| raise Wavefront::Exception::InvalidTimeFormat unless o.is_a?(Time) }
  raise Wavefront::Exception::InvalidGranularity unless GRANULARITIES.include?(granularity)
  raise Wavefront::Exception::InvalidResponseFormat unless FORMATS.include?(options[:response_format])

  args = {:params =>
          {:q => query, :g => granularity, :n => 'Unknown',
           :s => options[:start_time].to_i, :e => options[:end_time].to_i}}.merge(@headers)
  response = RestClient.get @base_uri.to_s, args

  klass = Object.const_get('Wavefront').const_get('Response').const_get(options[:response_format].to_s.capitalize)
  return klass.new(response)

end