Class: GatecoinAPI::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/gatecoin-api/client.rb

Defined Under Namespace

Classes: ApiError, CaseSensitiveString, SigningMiddleware

Constant Summary collapse

InvalidCredentials =
Class.new(ArgumentError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(public_key:, private_key:, url: GatecoinAPI::TEST_URL) ⇒ Client

Returns a new instance of Client.



24
25
26
27
28
# File 'lib/gatecoin-api/client.rb', line 24

def initialize(public_key:, private_key:, url: GatecoinAPI::TEST_URL)
  @public_key  = public_key
  @private_key = private_key
  @url         = url
end

Instance Attribute Details

#private_keyObject

Returns the value of attribute private_key.



22
23
24
# File 'lib/gatecoin-api/client.rb', line 22

def private_key
  @private_key
end

#public_keyObject

Returns the value of attribute public_key.



22
23
24
# File 'lib/gatecoin-api/client.rb', line 22

def public_key
  @public_key
end

#urlObject

Returns the value of attribute url.



22
23
24
# File 'lib/gatecoin-api/client.rb', line 22

def url
  @url
end

Instance Method Details

#bank_accountsObject



131
132
133
134
135
# File 'lib/gatecoin-api/client.rb', line 131

def bank_accounts
  response = connection(sign: true).get('/api/Bank/UserAccounts')

  parse_response(response)
end

#connection(sign: false) ⇒ Object



186
187
188
189
190
191
192
193
194
195
# File 'lib/gatecoin-api/client.rb', line 186

def connection(sign: false)
  Faraday.new(connection_options) do |faraday|
    if sign
      raise InvalidCredentials unless @public_key && @private_key
      faraday.use SigningMiddleware, @public_key, @private_key
    end
    faraday.response(:logger, GatecoinAPI.logger, GatecoinAPI.logger_options || {}) if GatecoinAPI.logger
    faraday.adapter :net_http
  end
end

#create_quote(currency_to:, amount:, is_amount_in_currency_from: false, reference: nil, label: nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/gatecoin-api/client.rb', line 137

def create_quote(currency_to:, amount:, is_amount_in_currency_from: false, reference: nil, label: nil)
  params             = {
    CurrencyTo:             currency_to,
    Amount:                 amount,
    IsAmountInCurrencyFrom: is_amount_in_currency_from,
  }
  params[:Reference] = reference if reference
  params[:Label]     = label if label

  response = connection(sign: true).post('/api/Merchant/Payment/Quote') do |req|
    req.body = MultiJson.dump(params)
  end

  parse_response(response)
end

#documents_statusObject



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/gatecoin-api/client.rb', line 90

def documents_status
  result = {}

  response             = connection(sign: true).get('/api/Account/DocumentID')
  parsed               = parse_response(response)
  result['DocumentID'] = parsed['status']

  response                  = connection(sign: true).get('/api/Account/DocumentAddress')
  parsed                    = parse_response(response)
  result['DocumentAddress'] = parsed['status']

  result
end

#gatewaysObject



165
166
167
168
169
# File 'lib/gatecoin-api/client.rb', line 165

def gateways
  response = connection(sign: true).get('/api/Merchant/Gateway')

  parse_response(response)
end

fails unless documents are verified



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/gatecoin-api/client.rb', line 105

def (bank_name:, label:, account_number:, currency:, holder_name:, city:, country_code:, password:,
                      swift_code: nil, bank_code: nil, branch_name: nil, bank_address: nil, bank_phone: nil, validation_code: nil)
  params                  = {
    BankName:      bank_name,
    Label:         label,
    AccountNumber: ,
    Currency:      currency,
    HolderName:    holder_name,
    City:          city,
    CountryCode:   country_code,
    Password:      password,
  }
  params[:SwiftCode]      = swift_code if swift_code
  params[:BankCode]       = bank_code if bank_code
  params[:BranchName]     = branch_name if branch_name
  params[:Address]        = bank_address if bank_address
  params[:Phone]          = bank_phone if bank_phone
  params[:ValidationCode] = validation_code if validation_code

  response = connection(sign: true).post('/api/Bank/UserAccounts') do |req|
    req.body = MultiJson.dump(params)
  end

  parse_response(response)
end

#login(username:, password:, validation_code: nil) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/gatecoin-api/client.rb', line 46

def (username:, password:, validation_code: nil)
  params                  = {
    UserName: username,
    Password: password,
  }
  params[:ValidationCode] = validation_code if validation_code

  response = connection(sign: true).post('/api/Auth/Login') do |req|
    req.body = MultiJson.dump(params)
  end
  result   = parse_response(response)
  client   = self.class.new(public_key: result['publicKey'], private_key: result['apiKey'], url: url)

  [client, result]
end

#parse_response(response) ⇒ Object



178
179
180
181
182
183
184
# File 'lib/gatecoin-api/client.rb', line 178

def parse_response(response)
  result = MultiJson.load(response.body)
  if (status = result['responseStatus']) && (error_code = status['errorCode'])
    fail ApiError.new(status['message'], error_code, status)
  end
  result
end

#paymentsObject



171
172
173
174
175
# File 'lib/gatecoin-api/client.rb', line 171

def payments
  response = connection(sign: true).get('/api/Merchant/Payment')

  parse_response(response)
end

#post_document_address(content:, mime_type: 'image/jpeg') ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/gatecoin-api/client.rb', line 77

def post_document_address(content:, mime_type: 'image/jpeg')
  params = {
    Content:  Base64.strict_encode64(content),
    MimeType: mime_type,
  }

  response = connection(sign: true).post('/api/Account/DocumentAddress') do |req|
    req.body = MultiJson.dump(params)
  end

  parse_response(response)
end

#post_document_id(number:, country:, content:, mime_type: 'image/jpeg') ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gatecoin-api/client.rb', line 62

def post_document_id(number:, country:, content:, mime_type: 'image/jpeg')
  params = {
    DocumentNumber: number,
    IssuingCountry: country,
    Content:        Base64.strict_encode64(content),
    MimeType:       mime_type,
  }

  response = connection(sign: true).post('/api/Account/DocumentID') do |req|
    req.body = MultiJson.dump(params)
  end

  parse_response(response)
end

#register_user(email:, password:, is_corporate_account: false, language: 'en', referral_code: nil) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gatecoin-api/client.rb', line 30

def register_user(email:, password:, is_corporate_account: false, language: 'en', referral_code: nil)
  params                = {
    Email:              email,
    Password:           password,
    IsCorporateAccount: ,
    language:           language,
  }
  params[:ReferralCode] = referral_code if referral_code

  response = connection(sign: true).post('/api/RegisterUser') do |req|
    req.body = MultiJson.dump(params)
  end

  parse_response(response)
end

#update_gateway(expiry_second: nil, webhook: nil) ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
# File 'lib/gatecoin-api/client.rb', line 153

def update_gateway(expiry_second: nil, webhook: nil)
  params                = {}
  params[:Webhook]      = webhook if webhook
  params[:ExpirySecond] = expiry_second if expiry_second

  response = connection(sign: true).put('/api/Merchant/Gateway') do |req|
    req.body = MultiJson.dump(params)
  end

  parse_response(response)
end