Class: Chartbeat

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

Constant Summary collapse

YESTERDAY =
Time.now.to_i - 86400
BASE_URI =
'chartbeat.com/api'
METHODS =
[:pages, :pathsummary, :recent, :summize, :quickstats, :toppages, :histogram, :summary]
DASHAPI_METHODS =
[:alerts, :snapshots, :stats, :data_series, :day_data_series]
DEFAULT_ARG_VALS =
{:path => '/', :keys => 'n', :types => 'n', :since => YESTERDAY, 
:timestamp => YESTERDAY, :days => 1, :minutes => 20, :type => 'path', 
:breaks => 'n', :limit => 10}
DEPRECATED_METHODS =
[:summize]

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Chartbeat

c = Chartbeat.new :apikey => ‘yourkey’, :host => ‘yourdomain.com’

Raises:

  • (RuntimeError)


15
16
17
18
19
# File 'lib/chartbeat.rb', line 15

def initialize(opts = {})
  raise RuntimeError, "You must provide an API key and host" unless opts[:apikey] && opts[:host]
  @apikey = opts[:apikey]
  @host = opts[:host]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/chartbeat.rb', line 21

def method_missing(m, *args)
  super unless METHODS.include?(m) || DASHAPI_METHODS.include?(m)

  if DEPRECATED_METHODS.include?(m)
    warn "#{m} is a deprecated method. Use with caution!"
  end

  query = *args
  query_to_perform = {:apikey => @apikey, :host => @host}
  DEFAULT_ARG_VALS.each do |k,v|
    if query && query[k]
      v = query[k]
    end
    query_to_perform[k] = v
  end
  prefix = DASHAPI_METHODS.include?(m) ? '/dashapi' : ''
  data = Crack::JSON.parse(RestClient.get("http://" + BASE_URI + prefix + '/' + m.to_s + '/', :params => query_to_perform))
end