gatecoin-api 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: edcb574c9993ccf4d40b588a63f70ad185f94ca7
4
+ data.tar.gz: 42c41e587a1db4990a7e7d9ab44800dfe0861245
5
+ SHA512:
6
+ metadata.gz: 1b1e43c6ed68bad493cac622568f85dc038e55c291b9f696c5af7c1b050cbf36b59d44a604128b4ff640d912676a9f147ddd9d2ddebe54ecefba3f449ada1554
7
+ data.tar.gz: df4126b68c3a9693565b456e36f247b85ba26f59decab9b500c7d09d7b7bc6b61a886e8e558b12c9eac9adadad08ef67336a7462b9628057c7fa50038aab4799
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.2
4
+ before_install: gem install bundler -v 1.10.6
5
+ install: bin/setup
6
+ script: rake
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gatecoin-api.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 AlexanderPavlenko
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,40 @@
1
+ # GatecoinAPI
2
+
3
+ This gem is a wrapper for [Gatecoin](https://www.gatecoin.com/) API.
4
+
5
+ [![Build Status](https://travis-ci.org/MyceliumGear/gatecoin-api.svg)](https://travis-ci.org/MyceliumGear/gatecoin-api)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'gatecoin-api'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install gatecoin-api
22
+
23
+ ## Usage
24
+
25
+ require 'gatecoin-api'
26
+ client = GatecoinAPI::Client.new(
27
+ public_key: 'public_key',
28
+ private_key: 'api_key',
29
+ url: GatecoinAPI::PRODUCTION_URL,
30
+ )
31
+
32
+ ## Development
33
+
34
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
35
+
36
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
37
+
38
+ ## Contributing
39
+
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MyceliumGear/gatecoin-api.
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'gatecoin-api'
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require 'irb'
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gatecoin-api/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'gatecoin-api'
8
+ spec.version = GatecoinAPI::VERSION
9
+ spec.authors = ['AlexanderPavlenko']
10
+ spec.email = ['alerticus@gmail.com']
11
+
12
+ spec.summary = %q{Wrapper for Gatecoin API}
13
+ spec.description = %q{Wrapper for Gatecoin API}
14
+ spec.homepage = 'https://github.com/MyceliumGear/gatecoin-api'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'multi_json', '~> 1.11'
23
+ spec.add_dependency 'faraday', '~> 0.9.2'
24
+
25
+ spec.add_development_dependency 'bundler', '~> 1.10'
26
+ spec.add_development_dependency 'rake', '~> 10.0'
27
+ spec.add_development_dependency 'rspec'
28
+ spec.add_development_dependency 'vcr'
29
+ spec.add_development_dependency 'webmock'
30
+ end
@@ -0,0 +1,11 @@
1
+ require 'gatecoin-api/version'
2
+ require 'gatecoin-api/client'
3
+
4
+ module GatecoinAPI
5
+ TEST_URL = 'https://staging.gatecoin.com'
6
+ PRODUCTION_URL = 'https://gatecoin.com'
7
+
8
+ class << self
9
+ attr_accessor :logger, :logger_options # https://github.com/lostisland/faraday/blob/master/lib/faraday/response/logger.rb
10
+ end
11
+ end
@@ -0,0 +1,257 @@
1
+ require 'base64'
2
+ require 'openssl'
3
+ require 'multi_json'
4
+ require 'faraday'
5
+
6
+ # https://staging.gatecoin.com/api/swagger-ui/index.html
7
+ module GatecoinAPI
8
+ class Client
9
+
10
+ InvalidCredentials = Class.new(ArgumentError)
11
+
12
+ class ApiError < StandardError
13
+
14
+ attr_reader :raw
15
+
16
+ def initialize(message, code, raw)
17
+ @raw = raw
18
+ super "#{message} [#{code}]"
19
+ end
20
+ end
21
+
22
+ attr_accessor :public_key, :private_key, :url
23
+
24
+ def initialize(public_key:, private_key:, url: GatecoinAPI::TEST_URL)
25
+ @public_key = public_key
26
+ @private_key = private_key
27
+ @url = url
28
+ end
29
+
30
+ def register_user(email:, password:, is_corporate_account: false, language: 'en', referral_code: nil)
31
+ params = {
32
+ Email: email,
33
+ Password: password,
34
+ IsCorporateAccount: is_corporate_account,
35
+ language: language,
36
+ }
37
+ params[:ReferralCode] = referral_code if referral_code
38
+
39
+ response = connection(sign: true).post('/api/RegisterUser') do |req|
40
+ req.body = MultiJson.dump(params)
41
+ end
42
+
43
+ parse_response(response)
44
+ end
45
+
46
+ def login(username:, password:, validation_code: nil)
47
+ params = {
48
+ UserName: username,
49
+ Password: password,
50
+ }
51
+ params[:ValidationCode] = validation_code if validation_code
52
+
53
+ response = connection(sign: true).post('/api/Auth/Login') do |req|
54
+ req.body = MultiJson.dump(params)
55
+ end
56
+ result = parse_response(response)
57
+ client = self.class.new(public_key: result['publicKey'], private_key: result['apiKey'], url: url)
58
+
59
+ [client, result]
60
+ end
61
+
62
+ def post_document_id(number:, country:, content:, mime_type: 'image/jpeg')
63
+ params = {
64
+ DocumentNumber: number,
65
+ IssuingCountry: country,
66
+ Content: Base64.strict_encode64(content),
67
+ MimeType: mime_type,
68
+ }
69
+
70
+ response = connection(sign: true).post('/api/Account/DocumentID') do |req|
71
+ req.body = MultiJson.dump(params)
72
+ end
73
+
74
+ parse_response(response)
75
+ end
76
+
77
+ def post_document_address(content:, mime_type: 'image/jpeg')
78
+ params = {
79
+ Content: Base64.strict_encode64(content),
80
+ MimeType: mime_type,
81
+ }
82
+
83
+ response = connection(sign: true).post('/api/Account/DocumentAddress') do |req|
84
+ req.body = MultiJson.dump(params)
85
+ end
86
+
87
+ parse_response(response)
88
+ end
89
+
90
+ def documents_status
91
+ result = {}
92
+
93
+ response = connection(sign: true).get('/api/Account/DocumentID')
94
+ parsed = parse_response(response)
95
+ result['DocumentID'] = parsed['status']
96
+
97
+ response = connection(sign: true).get('/api/Account/DocumentAddress')
98
+ parsed = parse_response(response)
99
+ result['DocumentAddress'] = parsed['status']
100
+
101
+ result
102
+ end
103
+
104
+ # fails unless documents are verified
105
+ def link_bank_account(bank_name:, label:, account_number:, currency:, holder_name:, city:, country_code:, password:,
106
+ swift_code: nil, bank_code: nil, branch_name: nil, bank_address: nil, bank_phone: nil, validation_code: nil)
107
+ params = {
108
+ BankName: bank_name,
109
+ Label: label,
110
+ AccountNumber: account_number,
111
+ Currency: currency,
112
+ HolderName: holder_name,
113
+ City: city,
114
+ CountryCode: country_code,
115
+ Password: password,
116
+ }
117
+ params[:SwiftCode] = swift_code if swift_code
118
+ params[:BankCode] = bank_code if bank_code
119
+ params[:BranchName] = branch_name if branch_name
120
+ params[:Address] = bank_address if bank_address
121
+ params[:Phone] = bank_phone if bank_phone
122
+ params[:ValidationCode] = validation_code if validation_code
123
+
124
+ response = connection(sign: true).post('/api/Bank/UserAccounts') do |req|
125
+ req.body = MultiJson.dump(params)
126
+ end
127
+
128
+ parse_response(response)
129
+ end
130
+
131
+ def bank_accounts
132
+ response = connection(sign: true).get('/api/Bank/UserAccounts')
133
+
134
+ parse_response(response)
135
+ end
136
+
137
+ def create_quote(currency_to:, amount:, is_amount_in_currency_from: false, reference: nil, label: nil)
138
+ params = {
139
+ CurrencyTo: currency_to,
140
+ Amount: amount,
141
+ IsAmountInCurrencyFrom: is_amount_in_currency_from,
142
+ }
143
+ params[:Reference] = reference if reference
144
+ params[:Label] = label if label
145
+
146
+ response = connection(sign: true).post('/api/Merchant/Payment/Quote') do |req|
147
+ req.body = MultiJson.dump(params)
148
+ end
149
+
150
+ parse_response(response)
151
+ end
152
+
153
+ def update_gateway(expiry_second: nil, webhook: nil)
154
+ params = {}
155
+ params[:Webhook] = webhook if webhook
156
+ params[:ExpirySecond] = expiry_second if expiry_second
157
+
158
+ response = connection(sign: true).put('/api/Merchant/Gateway') do |req|
159
+ req.body = MultiJson.dump(params)
160
+ end
161
+
162
+ parse_response(response)
163
+ end
164
+
165
+ def gateways
166
+ response = connection(sign: true).get('/api/Merchant/Gateway')
167
+
168
+ parse_response(response)
169
+ end
170
+
171
+ def payments
172
+ response = connection(sign: true).get('/api/Merchant/Payment')
173
+
174
+ parse_response(response)
175
+ end
176
+
177
+
178
+ def parse_response(response)
179
+ result = MultiJson.load(response.body)
180
+ if (status = result['responseStatus']) && (error_code = status['errorCode'])
181
+ fail ApiError.new(status['message'], error_code, status)
182
+ end
183
+ result
184
+ end
185
+
186
+ def connection(sign: false)
187
+ Faraday.new(connection_options) do |faraday|
188
+ if sign
189
+ raise InvalidCredentials unless @public_key && @private_key
190
+ faraday.use SigningMiddleware, @public_key, @private_key
191
+ end
192
+ faraday.response(:logger, GatecoinAPI.logger, GatecoinAPI.logger_options || {}) if GatecoinAPI.logger
193
+ faraday.adapter :net_http
194
+ end
195
+ end
196
+
197
+ private def connection_options
198
+ {
199
+ url: @url,
200
+ ssl: {
201
+ ca_path: ENV['SSL_CERT_DIR'] || '/etc/ssl/certs',
202
+ },
203
+ headers: {
204
+ content_type: 'application/json',
205
+ },
206
+ }
207
+ end
208
+
209
+ class CaseSensitiveString < String
210
+ def downcase
211
+ self
212
+ end
213
+
214
+ def upcase
215
+ self
216
+ end
217
+
218
+ def capitalize
219
+ self
220
+ end
221
+ end
222
+
223
+ class SigningMiddleware < Faraday::Middleware
224
+
225
+ API_PUBLIC_KEY = CaseSensitiveString.new('API_PUBLIC_KEY').freeze
226
+ API_REQUEST_DATE = CaseSensitiveString.new('API_REQUEST_DATE').freeze
227
+ API_REQUEST_SIGNATURE = CaseSensitiveString.new('API_REQUEST_SIGNATURE').freeze
228
+
229
+ def initialize(app, public_key, private_key)
230
+ @app = app
231
+ @public_key = public_key
232
+ @private_key = private_key
233
+ end
234
+
235
+ def call(env)
236
+ env[:request_headers][API_PUBLIC_KEY] = @public_key
237
+ env[:request_headers][API_REQUEST_DATE] = Time.now.to_f.round(3).to_s
238
+ env[:request_headers][API_REQUEST_SIGNATURE] = signature(env)
239
+ @app.call(env)
240
+ end
241
+
242
+ # Base64Encode(
243
+ # HMAC-SHA256(
244
+ # (“POST" + "https://staging.gatecoin.com/api/RegisterUser” +
245
+ # "application/json" + "1447700841.477091").downcase,
246
+ # @private_key
247
+ # )
248
+ # )
249
+ def signature(env)
250
+ http_method = env[:method].to_s.upcase
251
+ content_type = http_method == 'GET' ? '' : env[:request_headers][:content_type]
252
+ message = "#{http_method}#{env[:url]}#{content_type}#{env[:request_headers][API_REQUEST_DATE]}".downcase
253
+ Base64.strict_encode64 OpenSSL::HMAC.digest('sha256', @private_key, message)
254
+ end
255
+ end
256
+ end
257
+ end
@@ -0,0 +1,3 @@
1
+ module GatecoinAPI
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gatecoin-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - AlexanderPavlenko
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-12-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: multi_json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.9.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.9.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: vcr
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Wrapper for Gatecoin API
112
+ email:
113
+ - alerticus@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - ".gitignore"
119
+ - ".rspec"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/console
126
+ - bin/setup
127
+ - gatecoin-api.gemspec
128
+ - lib/gatecoin-api.rb
129
+ - lib/gatecoin-api/client.rb
130
+ - lib/gatecoin-api/version.rb
131
+ homepage: https://github.com/MyceliumGear/gatecoin-api
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.8
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Wrapper for Gatecoin API
155
+ test_files: []