class Cloverly

Constants

VERSION

Attributes

api_key[R]

Public Class Methods

account() click to toggle source
# File lib/cloverly.rb, line 16
def account
  Cloverly::Account.parse(default, default.get('/2019-03-beta/account'))
end
default() click to toggle source
# File lib/cloverly.rb, line 12
def default
  @@default
end
default=(cloverly) click to toggle source
# File lib/cloverly.rb, line 8
def default=(cloverly)
  @@default = cloverly
end
estimate_offset(type, args = {}) click to toggle source
# File lib/cloverly.rb, line 20
def estimate_offset(type, args = {})
  Cloverly::Estimate.parse(default, default.post("/2019-03-beta/estimates/#{type}", args))
end
new(api_key) click to toggle source
# File lib/cloverly.rb, line 29
def initialize(api_key)
  @api_key = api_key
end
offset(type, args = {}) click to toggle source
# File lib/cloverly.rb, line 24
def offset(type, args = {})
  Cloverly::Purchase.parse(default, default.post("/2019-03-beta/purchases/#{type}", args))
end

Public Instance Methods

account() click to toggle source
# File lib/cloverly.rb, line 33
def account
  Cloverly::Account.parse(self, get('/2019-03-beta/account'))
end
delete(path) click to toggle source
# File lib/cloverly.rb, line 78
def delete(path)
  response = conn.delete do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{api_key}"
  end

  json_response = JSON.parse(response.body)

  if json_response.is_a?(Array) || json_response["error"].nil?
    json_response
  else
    raise Cloverly::Error.new(json_response["error"])
  end
end
estimate_offset(type, args = {}) click to toggle source
# File lib/cloverly.rb, line 37
def estimate_offset(type, args = {})
  Cloverly::Estimate.parse(self, post("/2019-03-beta/estimates/#{type}", args))
end
get(path) click to toggle source
# File lib/cloverly.rb, line 62
def get(path)
  response = conn.get do |req|
    req.url path
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{api_key}"
  end

  json_response = JSON.parse(response.body)

  if json_response.is_a?(Array) || json_response["error"].nil?
    json_response
  else
    raise Cloverly::Error.new(json_response["error"])
  end
end
offset(type, args = {}) click to toggle source
# File lib/cloverly.rb, line 41
def offset(type, args = {})
  Cloverly::Purchase.parse(self, post("/2019-03-beta/purchases/#{type}", args))
end
post(path, data) click to toggle source
# File lib/cloverly.rb, line 45
def post(path, data)
  response = conn.post do |req|
    req.url path
    req.body = data.to_json
    req.headers['Content-Type'] = 'application/json'
    req.headers['Authorization'] = "Bearer #{api_key}"
  end

  json_response = JSON.parse(response.body)

  if json_response["error"].nil?
    json_response
  else
    raise Cloverly::Error.new(json_response["error"])
  end
end

Private Instance Methods

conn() click to toggle source
# File lib/cloverly.rb, line 95
def conn
  @conn ||= Faraday.new(:url => ENV['CLOVERLY_API_URL'] || 'https://api.cloverly.com')
end