Module: CoinGate
- Defined in:
- lib/coingate.rb,
lib/coingate/version.rb,
lib/coingate/api_error.rb,
lib/coingate/error_handler.rb
Defined Under Namespace
Modules: Merchant Classes: APIError, BadCredentials, BadEnvironment, BadRequest, CredentialsMissing, InternalServerError, NotFound, OrderIsNotValid, OrderNotFound, PageNotFound, RecordNotFound, Unauthorized, UnprocessableEntity
Constant Summary collapse
- VERSION =
'1.0.1'
Class Attribute Summary collapse
-
.api_key ⇒ Object
Returns the value of attribute api_key.
-
.api_secret ⇒ Object
Returns the value of attribute api_secret.
-
.app_id ⇒ Object
Returns the value of attribute app_id.
-
.environment ⇒ Object
Returns the value of attribute environment.
Class Method Summary collapse
- .api_request(url, request_method = :post, params = {}, authentication = {}) ⇒ Object
- .config {|_self| ... } ⇒ Object
- .format_error(error) ⇒ Object
- .raise_error(http_code, error = {}) ⇒ Object
Class Attribute Details
.api_key ⇒ Object
Returns the value of attribute api_key.
15 16 17 |
# File 'lib/coingate.rb', line 15 def api_key @api_key end |
.api_secret ⇒ Object
Returns the value of attribute api_secret.
15 16 17 |
# File 'lib/coingate.rb', line 15 def api_secret @api_secret end |
.app_id ⇒ Object
Returns the value of attribute app_id.
15 16 17 |
# File 'lib/coingate.rb', line 15 def app_id @app_id end |
.environment ⇒ Object
Returns the value of attribute environment.
15 16 17 |
# File 'lib/coingate.rb', line 15 def environment @environment end |
Class Method Details
.api_request(url, request_method = :post, params = {}, authentication = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/coingate.rb', line 21 def api_request(url, request_method = :post, params = {}, authentication = {}) app_id = authentication[:app_id] || self.app_id api_key = authentication[:api_key] || self.api_key api_secret = authentication[:api_secret] || self.api_secret environment = authentication[:environment] || self.environment || 'live' # Check if credentials was passed if app_id.nil? || api_key.nil? || api_secret.nil? CoinGate.raise_error(400, {'reason' => 'CredentialsMissing'}) 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/v1' else 'https://api.coingate.com/v1' end) + url nonce = (Time.now.to_f * 1e6).to_i = nonce.to_s + app_id.to_s + api_key signature = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), api_secret, ) headers = { 'Access-Nonce' => nonce, 'Access-Key' => api_key, 'Access-Signature' => signature } 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| ... } ⇒ Object
17 18 19 |
# File 'lib/coingate.rb', line 17 def config yield self end |
.format_error(error) ⇒ Object
2 3 4 |
# File 'lib/coingate/error_handler.rb', line 2 def self.format_error(error) "#{error['reason']} #{error['message']}" end |
.raise_error(http_code, error = {}) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# 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 'CredentialsMissing' then CredentialsMissing when 'BadEnvironment' then BadEnvironment else BadRequest end when 401 then case reason when 'BadCredentials' then BadCredentials else 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 500 then InternalServerError else APIError end), format_error(error) end |