Class: Metric::Receive

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

Overview

Fetch data via the metric.io API

Class Method Summary (collapse)

Class Method Details

+ (String) compose(metric, range)

Generate the url with query strings



23
24
25
26
27
28
29
30
# File 'lib/metric/receive.rb', line 23

def self.compose(metric, range)
  key = "?api_key=" + Metric.configuration.api_key
  url = Metric.configuration.metric_host + '/receive'
  url << key
  url << "&token=" + generate_token(metric)
  url << parse_metric(metric)
  url << "&range=" + range
end

+ (Faraday::Connection) connection

Returns and memoizes a Faraday connection



35
36
37
38
39
# File 'lib/metric/receive.rb', line 35

def self.connection
  @connection ||= Faraday.new do |builder|
    builder.adapter :net_http
  end
end

+ (String) generate_token(metric)

Generate a hash of the site's secret_key and metric identifier



14
15
16
# File 'lib/metric/receive.rb', line 14

def self.generate_token(metric)
  Digest::MD5.hexdigest(Metric.configuration.secret_key + metric)
end

+ (String) parse_metric(metric)

CGI escape the metric name so spaces and characters are allowed



56
57
58
# File 'lib/metric/receive.rb', line 56

def self.parse_metric(metric)
  "&metric=#{CGI.escape(metric)}"
end

+ (Hash) receive(metric, range)

Perform the actual request and parse JSON



46
47
48
49
50
# File 'lib/metric/receive.rb', line 46

def self.receive(metric, range)
  url = compose(metric, range)
  response = connection.get(url)
  MultiJson.decode(response.body)
end