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, AuthTokenMissing, BadAuthToken, BadEnvironment, BadRequest, InternalServerError, NotFound, OrderIsNotValid, OrderNotFound, PageNotFound, RateLimitException, RecordNotFound, Unauthorized, UnprocessableEntity

Constant Summary collapse

VERSION =
'2.0.0'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.auth_tokenObject

Returns the value of attribute auth_token.



15
16
17
# File 'lib/coingate.rb', line 15

def auth_token
  @auth_token
end

.environmentObject

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
# 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| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (CoinGate)

    the object that the method was called on



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

Raises:



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
43
44
# 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