class Quanto::Client

Constants

API_VERSION
QUANTO_URL

Public Class Methods

new(consumer_key, consumer_secret, options = {}) click to toggle source
# File lib/quanto/client.rb, line 10
def initialize(consumer_key, consumer_secret, options = {})
  @consumer =
    OAuth2::Client.new(consumer_key, consumer_secret, site: QUANTO_URL )

  @token = options[:access_token]
end

Public Instance Methods

activate_plugin() click to toggle source

Activate the current plugin. This should only be done once credentials have been obtained. Returns the url for the current plugin.

# File lib/quanto/entries.rb, line 24
def activate_plugin
  attributes = post('/activations')
  attributes["url"]
end
authorized?() click to toggle source

Returns true if the user is authorized to make requests to quanto This means that the application has acquired a valid access token and access token secret.

# File lib/quanto/client.rb, line 19
def authorized?
  !@token.nil?
end
connected?() click to toggle source
# File lib/quanto/client.rb, line 23
def connected?
  !@access_token.nil?
end
metrics() click to toggle source
# File lib/quanto/entries.rb, line 29
def metrics
  get('/plugin/metrics')
end
plugin_url() click to toggle source

Make a GET request for the URL for the authenticated plugin. Returns the plugin URL as a string.

# File lib/quanto/entries.rb, line 16
def plugin_url
  attributes = get('/plugin')
  attributes["url"]
end
record_entry(value, metric_name, opts={}) click to toggle source
# File lib/quanto/entries.rb, line 5
def record_entry(value, metric_name, opts={})
  entry  = {
    metric: metric_name,
    value: value,
    date: opts[:date] || Time.zone.now.to_date.to_s
  }
  post('/entries', entry: entry)
end

Private Instance Methods

access_token() click to toggle source
# File lib/quanto/client.rb, line 28
def access_token
  return nil unless authorized?
  @access_token ||= OAuth2::AccessToken.new(@consumer, @token)
end
get(path, options={}) click to toggle source
# File lib/quanto/client.rb, line 39
def get(path, options={})
  url = "/api/v#{API_VERSION}/#{path}"
  response = access_token.get(url, options).body
  JSON.parse(response)
end
post(path, options={}) click to toggle source
# File lib/quanto/client.rb, line 33
def post(path, options={})
  url = "/api/v#{API_VERSION}/#{path}"
  response = access_token.post(url, body: options).body
  JSON.parse(response)
end