module CoinGate
Constants
- VERSION
Attributes
auth_token[RW]
environment[RW]
Public Class Methods
api_request(url, request_method = :post, params = {}, authentication = {})
click to toggle source
# File lib/coingate.rb, line 21 def api_request(url, request_method = :post, params = {}, authentication = {}) auth_token = authentication[:auth_token] || self.auth_token environment = authentication[:environment] || self.environment || 'live' # Check if auth_token was passed if auth_token.nil? CoinGate.raise_error(400, {'reason' => 'AuthTokenMissing'}) end # Check if right environment passed environments = %w(live sandbox) unless environments.include?(environment) CoinGate.raise_error(400, {'reason' => 'BadEnvironment', 'message' => "Environment does not exist. Available environments: #{environments.join(', ')}"}) end url = ( case environment when 'sandbox' 'https://api-sandbox.coingate.com/v2' else 'https://api.coingate.com/v2' end) + url headers = { Authorization: "Token #{auth_token}" } begin response = case request_method when :get RestClient.get(url, headers) when :post RestClient.post(url, params, headers.merge('Content-Type' => 'application/x-www-form-urlencoded')) end [response.code, JSON.parse(response.to_str)] rescue => e response = begin JSON.parse(e.response) rescue {'reason' => nil, 'message' => e.response} end CoinGate.raise_error(e.http_code, response) end end
config() { |self| ... }
click to toggle source
# File lib/coingate.rb, line 17 def config yield self end
format_error(error)
click to toggle source
# File lib/coingate/error_handler.rb, line 2 def self.format_error(error) "#{error['reason']} #{error['message']}" end
raise_error(http_code, error={})
click to toggle source
# File lib/coingate/error_handler.rb, line 6 def self.raise_error(http_code, error={}) reason = error['reason'] raise (case http_code when 400 case reason when 'AuthTokenMissing' then AuthTokenMissing when 'BadEnvironment' then BadEnvironment else BadRequest end when 401 then case reason when 'BadAuthToken' then BadAuthToken else Unauthorized end when 404 then case reason when 'PageNotFound' then PageNotFound when 'RecordNotFound' then RecordNotFound when 'OrderNotFound' then OrderNotFound else NotFound end when 422 case reason when 'OrderIsNotValid' then OrderIsNotValid else UnprocessableEntity end when 429 then RateLimitException when 500 then InternalServerError else APIError end), format_error(error) end