Class: Metric::Receive
- Inherits:
-
Object
- Object
- Metric::Receive
- Defined in:
- lib/metric/receive.rb
Overview
Fetch data via the metric.io API
Class Method Summary collapse
-
.compose(metric, range) ⇒ String
Generate the url with query strings.
-
.connection ⇒ Faraday::Connection
Returns and memoizes a Faraday connection.
-
.generate_token(metric, range) ⇒ String
Generate a hash of the site’s secret_key and metric identifier.
-
.receive(metric, range) ⇒ Hash
Perform the actual request and parse JSON.
Class Method Details
.compose(metric, range) ⇒ String
Generate the url with query strings
23 24 25 26 27 28 29 30 31 |
# File 'lib/metric/receive.rb', line 23 def self.compose(metric, range) token = generate_token(metric, range) parameters = {"metric" => metric, "range" => range, "token" => token} api_key = Metric.configuration.api_key url = Metric.configuration.protocol + "://" + Metric.configuration.host url << "/v1/sites/#{api_key}/statistics?" url << Metric::Util.build_query_string(parameters) url end |
.connection ⇒ Faraday::Connection
Returns and memoizes a Faraday connection
36 37 38 39 40 |
# File 'lib/metric/receive.rb', line 36 def self.connection @connection ||= Faraday.new do |builder| builder.adapter :net_http end end |
.generate_token(metric, range) ⇒ String
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, range) Digest::MD5.hexdigest(Metric.configuration.secret_key + metric + range) end |
.receive(metric, range) ⇒ Hash
Perform the actual request and parse JSON
47 48 49 50 51 |
# File 'lib/metric/receive.rb', line 47 def self.receive(metric, range) url = compose(metric, range) response = connection.get(url) MultiJson.decode(response.body)[range] end |