class Coinmarketcal::Client

Constants

HOST

Attributes

access_token[R]
client_id[R]
client_secret[R]

Public Class Methods

new(attrs = {}) click to toggle source
# File lib/coinmarketcal/client.rb, line 12
def initialize(attrs = {})
  @client_id = attrs[:client_id]
  @client_secret = attrs[:client_secret]
  @access_token = get_access_token
end

Public Instance Methods

get(path, params = {}, headers = {}) click to toggle source
# File lib/coinmarketcal/client.rb, line 18
def get(path, params = {}, headers = {})
  response = connection.get do |req|
    url = "#{HOST}/#{path}"
    req.params.merge!(params)
    req.url(url)
    req.params[:access_token] = access_token
  end

  return_response(response)
end

Private Instance Methods

connection() click to toggle source
# File lib/coinmarketcal/client.rb, line 31
def connection
  @connection ||= Faraday.new(:url => HOST) do |faraday|
    faraday.request :json
    faraday.response :json
    faraday.adapter Faraday.default_adapter
  end
end
get_access_token() click to toggle source
# File lib/coinmarketcal/client.rb, line 51
def get_access_token
  response = connection.get do |req|
    url = "#{HOST}/oauth/v2/token"
    req.url(url)

    req.params[:client_id] = client_id
    req.params[:client_secret] = client_secret
    req.params[:grant_type] = 'client_credentials'
  end
  return_response(response)['access_token']
end
return_response(response) click to toggle source
# File lib/coinmarketcal/client.rb, line 39
def return_response(response)
  if response.status.to_i == 200
    response.body
  elsif response.status.to_i == 400
    error_message = response.body["error_description"] || response.body["message"]
    raise Coinmarketcal::FilterError, error_message
  elsif response.status.to_i == 401
    error_message = response.body["error_description"] || response.body["message"]
    raise Coinmarketcal::TokenExpiredError, error_message
  end
end