mfkessai-ruby 1.0.0.beta1

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
+ SHA256:
3
+ metadata.gz: 67112c72373759e261b00b94f3c496e03b0ced828c2967ec19b6683e447f2566
4
+ data.tar.gz: 3dbdb547117293ac159653907289bb7480d8c2c40689c0b5f05c53d2f42e9c4c
5
+ SHA512:
6
+ metadata.gz: 537cb562321a03bccfabfe962643a270307dab56913dc64e4ff2020c28fb2879f24c3c19e7f6e3d88fe103c60c1780b6b26f4b4147ac151d6f187f7cd12cf4df
7
+ data.tar.gz: 8eff324befa18b38a9287798d0a3d32e3fbe2f450dd82c395a326324db3a0ab7b95ecd84f536cf0eb4c1d9ecab7f7d9fae6b7e46bff0d3491eca1873cdd78f72
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.gitlab-ci.yml ADDED
@@ -0,0 +1,65 @@
1
+ stages:
2
+ - test
3
+
4
+ ruby2.3:rspec:
5
+ image: ruby:2.3-alpine
6
+
7
+ script:
8
+ - setup
9
+ - execute_rspec
10
+
11
+ ruby2.4:rspec:
12
+ image: ruby:2.4-alpine
13
+
14
+ script:
15
+ - setup
16
+ - execute_rspec
17
+
18
+ ruby2.5:rspec:
19
+ image: ruby:2.5-alpine
20
+
21
+ script:
22
+ - setup
23
+ - execute_rspec
24
+
25
+ ruby2.5.1:rspec:
26
+ image: ruby:2.5.1-alpine
27
+
28
+ script:
29
+ - setup
30
+ - execute_rspec
31
+
32
+ allow_failure: true
33
+
34
+ dependency_scanning:
35
+ image: docker:stable
36
+ variables:
37
+ DOCKER_DRIVER: overlay2
38
+ allow_failure: true
39
+ services:
40
+ - docker:stable-dind
41
+ script:
42
+ - export SP_VERSION=$(echo "$CI_SERVER_VERSION" | sed 's/^\([0-9]*\)\.\([0-9]*\).*/\1-\2-stable/')
43
+ - docker run
44
+ --env DEP_SCAN_DISABLE_REMOTE_CHECKS="${DEP_SCAN_DISABLE_REMOTE_CHECKS:-false}"
45
+ --volume "$PWD:/code"
46
+ --volume /var/run/docker.sock:/var/run/docker.sock
47
+ "registry.gitlab.com/gitlab-org/security-products/dependency-scanning:$SP_VERSION" /code
48
+ artifacts:
49
+ paths: [gl-dependency-scanning-report.json]
50
+
51
+ .auto_devops: &auto_devops |
52
+ function execute_rspec() {
53
+ bundle exec rspec
54
+ }
55
+
56
+ function setup() {
57
+ apk --update --upgrade add $BUILD_PACKAGES
58
+ apk add git
59
+ gem install bundler --no-ri --no-rdoc
60
+ bundle check --path=${BUNDLE_CACHE} || bundle install --path=${BUNDLE_CACHE} --jobs=2 --retry=3
61
+ bundle clean
62
+ }
63
+
64
+ before_script:
65
+ - *auto_devops
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/CHANGELOG.md ADDED
@@ -0,0 +1 @@
1
+ # Changelog
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mfkessai-ruby.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,39 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ mfkessai-ruby (1.0.0.beta1)
5
+ faraday (~> 0.10)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ diff-lcs (1.3)
11
+ faraday (0.15.2)
12
+ multipart-post (>= 1.2, < 3)
13
+ multipart-post (2.0.0)
14
+ rake (10.5.0)
15
+ rspec (3.7.0)
16
+ rspec-core (~> 3.7.0)
17
+ rspec-expectations (~> 3.7.0)
18
+ rspec-mocks (~> 3.7.0)
19
+ rspec-core (3.7.1)
20
+ rspec-support (~> 3.7.0)
21
+ rspec-expectations (3.7.0)
22
+ diff-lcs (>= 1.2.0, < 2.0)
23
+ rspec-support (~> 3.7.0)
24
+ rspec-mocks (3.7.0)
25
+ diff-lcs (>= 1.2.0, < 2.0)
26
+ rspec-support (~> 3.7.0)
27
+ rspec-support (3.7.1)
28
+
29
+ PLATFORMS
30
+ ruby
31
+
32
+ DEPENDENCIES
33
+ bundler (~> 1.16)
34
+ mfkessai-ruby!
35
+ rake (~> 10.0)
36
+ rspec (~> 3.0)
37
+
38
+ BUNDLED WITH
39
+ 1.16.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) 2018 TORU FUKUI (https://gitlab.com/torufukui)
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,340 @@
1
+ ### **!こちらのgemはまだbeta版です**
2
+
3
+ # Mfkessai for Ruby
4
+ mfkessaiのapiを使用するための便利なアクセスをRubyで提供します
5
+
6
+ こちらのgemは非公式なライブラリです
7
+
8
+ 対応API
9
+
10
+ - customer
11
+ - destination
12
+ - examination
13
+ - ping
14
+ - transaction
15
+
16
+ [詳しいAPIを参照する](https://mfkessai.co.jp/api_doc/endpoint.html)
17
+
18
+ ## Installation
19
+
20
+ ```
21
+ $ gem install mfkessai-ruby -v 1.0.0.beta1 --pre
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ ライブラリは、利用可能なアカウントの秘密鍵と本番環境かテスト環境かのURLの設定をする必要があります。
27
+
28
+ Mfkessai.api_key, Mfkessai.api_urlをその値に設定します。
29
+
30
+ ```
31
+ require "mfkessai"
32
+ Mfkessai.api_url = "https://sandbox"
33
+ Mfkessai.api_key = "mfkessai_test_..."
34
+
35
+ # list customers
36
+ Mfkessai::Customer.list
37
+
38
+ # retrieve single customer
39
+ Mfkessai::Customer.retrieve("customer_test_123")
40
+ ```
41
+
42
+ ## API一覧
43
+
44
+ ## customer
45
+
46
+ - Mfkessai::Customer.list
47
+
48
+ ```
49
+ require "mfkessai"
50
+ Mfkessai.api_url = "https://sandbox"
51
+ Mfkessai.api_key = "mfkessai_test_..."
52
+
53
+ # 顧客リストを取得
54
+ Mfkessai::Customer.list
55
+ ```
56
+
57
+ - Mfkessai::Customer.retrieve
58
+
59
+ ```
60
+ require "mfkessai"
61
+ Mfkessai.api_url = "https://sandbox"
62
+ Mfkessai.api_key = "mfkessai_test_..."
63
+
64
+ # 顧客IDを指定して取得
65
+ Mfkessai::Customer.retrieve("customer_test_123")
66
+ ```
67
+
68
+ ## destination
69
+
70
+ - Mfkessai::Destination.create
71
+
72
+ ```
73
+ require "mfkessai"
74
+ Mfkessai.api_url = "https://sandbox"
75
+ Mfkessai.api_key = "mfkessai_test_..."
76
+
77
+ # 請求先を登録
78
+ params = {
79
+ "address1": "世田谷区1-2-3",
80
+ "address2": "ビル3F",
81
+ "cc_emails": [
82
+ "another.test1@example.jp",
83
+ "another.test2@example.jp"
84
+ ],
85
+ "customer": {
86
+ "office_name": "テスト商事株式会社",
87
+ "user_defined_id": "customer123456"
88
+ },
89
+ "customer_id": "ABCD-EFGH",
90
+ "department": "経理部",
91
+ "email": "test@example.jp",
92
+ "name": "請求先氏名",
93
+ "prefecture": "東京都",
94
+ "tel": "03-1234-5678",
95
+ "title": "",
96
+ "zip_code": "111-1111"
97
+ }
98
+ Mfkessai::Destination.create(request_body: params)
99
+ ```
100
+
101
+ ## examination
102
+ - Mfkessai::Destination.list
103
+
104
+ ```
105
+ require "mfkessai"
106
+ Mfkessai.api_url = "https://sandbox"
107
+ Mfkessai.api_key = "mfkessai_test_..."
108
+
109
+ # 審査リストを取得
110
+ Mfkessai::Destination.list
111
+ ```
112
+
113
+ - Mfkessai::Destination.create
114
+
115
+ ```
116
+ require "mfkessai"
117
+ Mfkessai.api_url = "https://sandbox"
118
+ Mfkessai.api_key = "mfkessai_test_..."
119
+
120
+ # 審査のための取引情報を登録
121
+ params = {
122
+ "amount": 2160,
123
+ "destination_id": "XXXX-YYYY",
124
+ "due_date": "2018-05-31T00:00:00+09:00",
125
+ "examination_details": [
126
+ {
127
+ "amount": 2000,
128
+ "description": "商品名A",
129
+ "quantity": 2,
130
+ "unit_price": 1000
131
+ },
132
+ {
133
+ "amount": 160,
134
+ "description": "消費税",
135
+ "quantity": 1,
136
+ "unit_price": 160
137
+ }
138
+ ]
139
+ }
140
+
141
+ Mfkessai::Destination.create(request_body: params)
142
+ ```
143
+
144
+ - Mfkessai::Destination.retrieve
145
+
146
+ ```
147
+ require "mfkessai"
148
+ Mfkessai.api_url = "https://sandbox"
149
+ Mfkessai.api_key = "mfkessai_test_..."
150
+
151
+ # IDを指定して審査情報を取得
152
+ Mfkessai::Destination.retrieve('destination_test_123')
153
+ ```
154
+
155
+ - Mfkessai::Destination.create_transaction
156
+
157
+ ```
158
+ require "mfkessai"
159
+ Mfkessai.api_url = "https://sandbox"
160
+ Mfkessai.api_key = "mfkessai_test_..."
161
+
162
+ # 審査時に登録した情報をもとに取引を登録
163
+
164
+ params = {
165
+ "date": "2018-04-16T00:00:00+09:00",
166
+ "email_flag": true,
167
+ "issue_date": "2018-05-16T00:00:00+09:00",
168
+ "posting_flag": false,
169
+ "user_defined_id": "transaction00000001"
170
+ }
171
+
172
+ Mfkessai::Destination.create_transaction(id: 'test_123', request_body: params)
173
+ ```
174
+
175
+ ## ping
176
+
177
+ - Mfkessai::Ping.send
178
+
179
+ ```
180
+ require "mfkessai"
181
+ Mfkessai.api_url = "https://sandbox"
182
+ Mfkessai.api_key = "mfkessai_test_..."
183
+
184
+ # pingを飛ばす
185
+ Mfkessai::Ping.send
186
+ ```
187
+
188
+ ## transaction
189
+
190
+ - Mfkessai::Transaction.list
191
+
192
+ ```
193
+ require "mfkessai"
194
+ Mfkessai.api_url = "https://sandbox"
195
+ Mfkessai.api_key = "mfkessai_test_..."
196
+
197
+ # 取引リストを取得
198
+
199
+ Mfkessai::Transaction.list
200
+ ```
201
+
202
+ - Mfkessai::Transaction.create
203
+
204
+ ```
205
+ require "mfkessai"
206
+ Mfkessai.api_url = "https://sandbox"
207
+ Mfkessai.api_key = "mfkessai_test_..."
208
+
209
+ # 取引を登録
210
+ params = {
211
+ "amount": 2160,
212
+ "date": "2018-04-16T00:00:00+09:00",
213
+ "destination": {
214
+ "address1": "千代田区1-2-3",
215
+ "address2": "サンプルビル3F",
216
+ "cc_emails": [
217
+ "another.test1@example.jp",
218
+ "another.test2@example.jp"
219
+ ],
220
+ "customer": {
221
+ "office_name": "サンプル商事株式会社",
222
+ "user_defined_id": "customer123456"
223
+ },
224
+ "customer_id": "ABCD-EFGH",
225
+ "department": "経理部",
226
+ "email": "kesai.tanto@example.jp",
227
+ "name": "請求先氏名",
228
+ "prefecture": "東京都",
229
+ "tel": "03-1234-5678",
230
+ "title": "",
231
+ "zip_code": "111-1111"
232
+ },
233
+ "destination_id": "XXXX-YYYY",
234
+ "due_date": "2018-05-31T00:00:00+09:00",
235
+ "email_flag": true,
236
+ "issue_date": "2018-05-16T00:00:00+09:00",
237
+ "posting_flag": false,
238
+ "transaction_details": [
239
+ {
240
+ "amount": 2000,
241
+ "description": "商品名A",
242
+ "quantity": 2,
243
+ "unit_price": 1000
244
+ },
245
+ {
246
+ "amount": 160,
247
+ "description": "消費税",
248
+ "quantity": 1,
249
+ "unit_price": 160
250
+ }
251
+ ],
252
+ "user_defined_id": "transaction00000001"
253
+ }
254
+
255
+ Mfkessai::Transaction.create(request_body: params)
256
+ ```
257
+
258
+ - Mfkessai::Transaction.retrieve
259
+
260
+ ```
261
+ require "mfkessai"
262
+ Mfkessai.api_url = "https://sandbox"
263
+ Mfkessai.api_key = "mfkessai_test_..."
264
+
265
+ # 取引IDを指定して取得
266
+ Mfkessai::Transaction.retrieve("transation_test_123")
267
+ ```
268
+
269
+ - Mfkessai::Transaction.cancel
270
+
271
+ ```
272
+ require "mfkessai"
273
+ Mfkessai.api_url = "https://sandbox"
274
+ Mfkessai.api_key = "mfkessai_test_..."
275
+
276
+ # 取引をキャンセル
277
+
278
+ Mfkessai::Transaction.cancel("transation_test_123")
279
+ ```
280
+
281
+ ## Handling errors
282
+
283
+ ```
284
+ begin
285
+ # Use Mfkessai's library to make requests...
286
+ rescue Mfkessai::BadRequestError => e
287
+ puts "Status is: #{e.http_status}"
288
+ puts "Message is: #{e.message}"
289
+ puts "http_body is: #{e.http_body}"
290
+ puts "json_body is: #{e.json_body}"
291
+ rescue Mfkessai::AuthenticationError => e
292
+ puts "Status is: #{e.http_status}"
293
+ puts "Message is: #{e.message}"
294
+ puts "http_body is: #{e.http_body}"
295
+ puts "json_body is: #{e.json_body}"
296
+ rescue Mfkessai::ForbiddenError => e
297
+ puts "Status is: #{e.http_status}"
298
+ puts "Message is: #{e.message}"
299
+ puts "http_body is: #{e.http_body}"
300
+ puts "json_body is: #{e.json_body}"
301
+ rescue Mfkessai::NotFoundError => e
302
+ puts "Status is: #{e.http_status}"
303
+ puts "Message is: #{e.message}"
304
+ puts "http_body is: #{e.http_body}"
305
+ puts "json_body is: #{e.json_body}"
306
+ rescue Mfkessai::APIConnectionError => e
307
+ puts "Status is: #{e.http_status}"
308
+ puts "Message is: #{e.message}"
309
+ puts "http_body is: #{e.http_body}"
310
+ puts "json_body is: #{e.json_body}"
311
+ rescue Mfkessai::ServerError => e
312
+ puts "Status is: #{e.http_status}"
313
+ puts "Message is: #{e.message}"
314
+ puts "http_body is: #{e.http_body}"
315
+ puts "json_body is: #{e.json_body}"
316
+ rescue => e
317
+ # Something else happened, completely unrelated to Mfkessai
318
+ end
319
+
320
+ ```
321
+
322
+ ## Development
323
+
324
+ Run all tests:
325
+
326
+ ```
327
+ bundle exec rspec spec
328
+ ```
329
+
330
+ ## Contributing
331
+
332
+ 1. Fork it
333
+ 1. Create your feature branch (git checkout -b my-new-feature)
334
+ 1. Commit your changes (git commit -am 'Add some feature')
335
+ 1. Push to the branch (git push origin my-new-feature)
336
+ 1. Create new Pull Request
337
+
338
+ ## License
339
+
340
+ MIT
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mfkessai/ruby"
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(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,3 @@
1
+ #!/bin/bash -xe
2
+
3
+ bundle exec rspec
@@ -0,0 +1,6 @@
1
+ #!/bin/bash -xe
2
+
3
+ apk --update --upgrade add $BUILD_PACKAGES
4
+ gem install bundler --no-ri --no-rdoc
5
+ bundle check --path=${BUNDLE_CACHE} || bundle install --path=${BUNDLE_CACHE} --jobs=2 --retry=3
6
+ bundle clean
@@ -0,0 +1,207 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ module Client
4
+ def request(url:, request_type:, request_body: nil)
5
+ api_url = Mfkessai.api_url
6
+ api_key = Mfkessai.api_key
7
+
8
+ check_api_key!(api_key)
9
+
10
+ url = "#{api_url}#{url}"
11
+ uri = URI.parse(url)
12
+
13
+ conn = Faraday.new(:ssl => {:verify => false}) do |c|
14
+ c.use Faraday::Request::Multipart
15
+ c.use Faraday::Request::UrlEncoded
16
+ c.use Faraday::Response::RaiseError
17
+ c.adapter Faraday.default_adapter
18
+ end
19
+
20
+ payload = request_body.nil? ? request_body : request_body.to_json
21
+
22
+ http_resp = conn.run_request(request_type, uri, payload, request_headers) do |req|
23
+ req.options.open_timeout = Mfkessai.open_timeout
24
+ req.options.timeout = Mfkessai.read_timeout
25
+ end
26
+ from_faraday_response(http_resp)
27
+ rescue JSON::ParserError => e
28
+ raise general_api_error(status: http_resp.status, body: http_resp.body)
29
+ rescue Faraday::ClientError => e
30
+ if e.response
31
+ handle_error_response(error_response: e.response)
32
+ else
33
+ handle_network_error(e)
34
+ end
35
+ rescue AuthenticationError => e
36
+ raise AuthenticationError.new(http_status: 401, message: e.message)
37
+ rescue StandardError => e
38
+ raise ServerError.new(http_status: 500)
39
+ end
40
+
41
+ def from_faraday_response(http_resp)
42
+ response = Struct.new(:data, :http_body, :http_headers, :http_status)
43
+ resp = response.new
44
+
45
+ # response bodyがokの場合、v1/pingからのリクエストなので処理を分ける
46
+ if http_resp.body == 'ok'
47
+ resp.http_body = http_resp.body
48
+ resp.http_headers = http_resp.headers
49
+ resp.http_status = http_resp.status
50
+ else
51
+ resp.data = JSON.parse(http_resp.body, symbolize_names: true)
52
+ resp.http_body = http_resp.body
53
+ resp.http_headers = http_resp.headers
54
+ resp.http_status = http_resp.status
55
+ end
56
+ resp
57
+ end
58
+
59
+ def request_headers
60
+ headers = {
61
+ "User-Agent" => "Mfkessai/v1 RubyBindings/#{Mfkessai::VERSION}",
62
+ "apiKey" => Mfkessai.api_key,
63
+ "Content-Type" => "application/json",
64
+ }
65
+
66
+ headers["Mfkessai-Version"] = Mfkessai.api_version if Mfkessai.api_version
67
+
68
+ begin
69
+ headers.update(
70
+ "X-Mfkessai-Client-User-Agent" => JSON.generate(user_agent)
71
+ )
72
+ rescue StandardError => e
73
+ headers.update(
74
+ "X-Mfkessai-Client-Raw-User-Agent" => user_agent.inspect,
75
+ :error => "#{e} (#{e.class})"
76
+ )
77
+ end
78
+ headers
79
+ end
80
+
81
+ def user_agent
82
+ @uname ||= uname
83
+ lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
84
+
85
+ {
86
+ :bindings_version => Mfkessai::VERSION,
87
+ :lang => 'ruby',
88
+ :lang_version => lang_version,
89
+ :platform => RUBY_PLATFORM,
90
+ :engine => defined?(RUBY_ENGINE) ? RUBY_ENGINE : '',
91
+ :uname => @uname,
92
+ :hostname => Socket.gethostname
93
+ }
94
+ end
95
+
96
+ def uname
97
+ if File.exist?('/proc/version')
98
+ File.read('/proc/version').strip
99
+ else
100
+ case RbConfig::CONFIG['host_os']
101
+ when /linux|darwin|bsd|sunos|solaris|cygwin/i
102
+ _uname_uname
103
+ when /mswin|mingw/i
104
+ _uname_ver
105
+ else
106
+ "unknown platform"
107
+ end
108
+ end
109
+ end
110
+
111
+ def _uname_uname
112
+ (`uname -a 2>/dev/null` || '').strip
113
+ rescue Errno::ENOMEM # couldn't create subprocess
114
+ "uname lookup failed"
115
+ end
116
+
117
+ def _uname_ver
118
+ (`ver` || '').strip
119
+ rescue Errno::ENOMEM # couldn't create subprocess
120
+ "uname lookup failed"
121
+ end
122
+
123
+ def handle_error_response(error_response:)
124
+ case error_response[:status]
125
+ when 400
126
+ data = JSON.parse(error_response[:body], symbolize_names: true)
127
+ raise BadRequestError.new(message: data[:detail],
128
+ http_status: error_response[:status],
129
+ http_body: error_response[:body],
130
+ json_body: data)
131
+ when 401
132
+ data = JSON.parse(error_response[:body], symbolize_names: true)
133
+ raise AuthenticationError.new(message: data[:detail],
134
+ http_status: error_response[:status],
135
+ http_body: error_response[:body],
136
+ json_body: data)
137
+ when 403
138
+ data = JSON.parse(error_response[:body], symbolize_names: true)
139
+ raise ForbiddenError.new(message: data[:detail],
140
+ http_status: error_response[:status],
141
+ http_body: error_response[:body],
142
+ json_body: data)
143
+ when 404
144
+ data = JSON.parse(error_response[:body], symbolize_names: true)
145
+ raise NotFoundError.new(message: data[:detail],
146
+ http_status: error_response[:status],
147
+ http_body: error_response[:body],
148
+ json_body: data)
149
+ when 500
150
+ data = JSON.parse(error_response[:body], symbolize_names: true)
151
+ raise ServerError.new(message: data[:detail],
152
+ http_status: error_response[:status],
153
+ http_body: error_response[:body],
154
+ json_body: data)
155
+ when 503
156
+ data = JSON.parse(error_response[:body], symbolize_names: true)
157
+ raise ServerError.new(message: data[:detail],
158
+ http_status: error_response[:status],
159
+ http_body: error_response[:body],
160
+ json_body: data)
161
+ else
162
+ raise ServerError.new(http_status: 500)
163
+ end
164
+ end
165
+
166
+ def handle_network_error(e)
167
+ case e
168
+ when Faraday::ConnectionFailed
169
+ message = "Unexpected error communicating when trying to connect to Mfkessai. " \
170
+ "You may be seeing this message because your DNS is not working. "
171
+ when Faraday::SSLError
172
+ message = "Could not establish a secure connection to Mfkessai, you may " \
173
+ "need to upgrade your OpenSSL version. To check, try running "
174
+ when Faraday::TimeoutError
175
+ message = "Could not connect to Mfkessai (#{Mfkessai.api_key}). " \
176
+ "Please check your internet connection and try again. "
177
+ else
178
+ message = "Unexpected error communicating with Mfkessai. "
179
+ end
180
+
181
+ message += " Request was retried"
182
+
183
+ raise APIConnectionError.new(message: message + "\n\n(Network error: #{e.message})")
184
+ end
185
+
186
+ def general_api_error(status:, body:)
187
+ error_message = "Invalid response object from API: #{body.inspect} \n HTTP response code was #{status})"
188
+ APIConnectionError.new(message: error_message,
189
+ http_status: status,
190
+ http_body: body)
191
+ end
192
+
193
+ def check_api_key!(api_key)
194
+ unless api_key
195
+ error_message = "No API key provided. " \
196
+ 'Set your API key using "Mfkessai.api_key = <API-KEY>". ' \
197
+ "You can generate API keys from the Mfkessai web interface. "
198
+ raise AuthenticationError.new(message: error_message)
199
+ end
200
+
201
+ return unless api_key =~ /\s/
202
+ error_message = "Your API key is invalid, as it contains " \
203
+ "whitespace. (HINT: You can double-check your API key from the "
204
+ raise AuthenticationError.new(message: error_message)
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ class Customer
4
+ extend Mfkessai::Client
5
+
6
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/customer/customer_index
7
+ def self.list
8
+ request(url: '/v1/customers',
9
+ request_type: :get)
10
+ end
11
+
12
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/customer/customer_show
13
+ def self.retrieve(id)
14
+ request(url: "/v1/customers/#{id}",
15
+ request_type: :get)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ class Destination
4
+ extend Mfkessai::Client
5
+
6
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/destination/destination_create
7
+ def self.create(request_body:)
8
+ request(url: '/v1/destinations',
9
+ request_type: :post,
10
+ request_body: request_body)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ class MfkessaiError < StandardError
4
+ attr_reader :message
5
+ attr_reader :http_status
6
+ attr_reader :http_body
7
+ attr_reader :json_body
8
+
9
+ def initialize(message: nil, http_status: nil, http_body: nil, json_body: nil)
10
+ @message = message
11
+ @http_status = http_status
12
+ @http_body = http_body
13
+ @json_body = json_body
14
+ end
15
+
16
+ def to_s
17
+ status_string = @http_status.nil? ? "" : "(Status #{@http_status}) "
18
+ "#{status_string}#{@message}"
19
+ end
20
+ end
21
+
22
+ # 400 Bad Request
23
+ class BadRequestError < MfkessaiError
24
+ end
25
+
26
+ # 401 Unauthorized
27
+ class AuthenticationError < MfkessaiError
28
+ end
29
+
30
+ # 403 Forbidden
31
+ class ForbiddenError < MfkessaiError
32
+ end
33
+
34
+ # 404 NotFound
35
+ class NotFoundError < MfkessaiError
36
+ end
37
+
38
+ # 500, 503 Internal Server Error
39
+ class ServerError < MfkessaiError
40
+ end
41
+
42
+ # APIConnectionError is raised in the event that the SDK can't connect to
43
+ # Mfkessai's servers.
44
+ class APIConnectionError < MfkessaiError
45
+ end
46
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ class Examination
4
+ extend Mfkessai::Client
5
+
6
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/examination/examination_index
7
+ def self.list
8
+ request(url: '/v1/examinations',
9
+ request_type: :get)
10
+ end
11
+
12
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/examination/examination_create
13
+ def self.create(request_body:)
14
+ request(url: '/v1/examinations',
15
+ request_type: :post,
16
+ request_body: request_body)
17
+ end
18
+
19
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/examination/examination_show
20
+ def self.retrieve(id)
21
+ request(url: "/v1/examinations/#{id}",
22
+ request_type: :get)
23
+ end
24
+
25
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/examination/examination_create_transaction
26
+ def self.create_transaction(id:, request_body:)
27
+ request(url: "/v1/examinations/#{id}/transaction",
28
+ request_type: :post,
29
+ request_body: request_body)
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ class Ping
4
+ extend Mfkessai::Client
5
+
6
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/ping/ping_show
7
+ def self.send
8
+ request(url: '/v1/ping', request_type: :get)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ class Transaction
4
+ extend Mfkessai::Client
5
+
6
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/transaction/transaction_index
7
+ def self.list
8
+ request(url: '/v1/transactions',
9
+ request_type: :get)
10
+ end
11
+
12
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/transaction/transaction_create
13
+ def self.create(request_body:)
14
+ request(url: '/v1/transactions',
15
+ request_type: :post,
16
+ request_body: request_body)
17
+ end
18
+
19
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/transaction/transaction_show
20
+ def self.retrieve(id)
21
+ request(url: "/v1/transactions/#{id}",
22
+ request_type: :get)
23
+ end
24
+
25
+ # ref: https://mfkessai.co.jp/api_doc/endpoint.html#/transaction/transaction_cancel
26
+ def self.cancel(id)
27
+ request(url: "/v1/transactions/#{id}/cancel",
28
+ request_type: :post)
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module Mfkessai
3
+ VERSION = "1.0.0.beta1"
4
+ end
data/lib/mfkessai.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ require 'net/http'
3
+ require 'openssl'
4
+ require 'json'
5
+ require 'faraday'
6
+
7
+ require 'mfkessai/version'
8
+ require 'mfkessai/client'
9
+ require 'mfkessai/customer'
10
+ require 'mfkessai/destination'
11
+ require 'mfkessai/examination'
12
+ require 'mfkessai/ping'
13
+ require 'mfkessai/transaction'
14
+ require 'mfkessai/errors'
15
+
16
+ module Mfkessai
17
+ @api_version = 'v1'
18
+ @open_timeout = 30
19
+ @read_timeout = 80
20
+
21
+ class << self
22
+ attr_accessor :api_key, :api_url, :api_version,
23
+ :open_timeout, :read_timeout
24
+ end
25
+
26
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "mfkessai/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mfkessai-ruby"
8
+ spec.version = Mfkessai::VERSION
9
+ spec.authors = ["torufukui"]
10
+ spec.email = ["fukui.development@gmail.com"]
11
+
12
+ spec.summary = 'Ruby bindings for the Mfkessai API'
13
+ spec.description = 'Ruby bindings for the Mfkessai API'
14
+ spec.homepage = "https://gitlab.com/torufukui/mfkessai-ruby"
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ # if spec.respond_to?(:metadata)
20
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
21
+ # else
22
+ # raise "RubyGems 2.0 or newer is required to protect against " \
23
+ # "public gem pushes."
24
+ # end
25
+
26
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
27
+ f.match(%r{^(test|spec|features)/})
28
+ end
29
+ spec.bindir = "exe"
30
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
31
+ spec.require_paths = ["lib"]
32
+
33
+ spec.add_dependency("faraday", "~> 0.10")
34
+
35
+ spec.add_development_dependency "bundler", "~> 1.16"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ end
metadata ADDED
@@ -0,0 +1,123 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mfkessai-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0.beta1
5
+ platform: ruby
6
+ authors:
7
+ - torufukui
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.10'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.10'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: Ruby bindings for the Mfkessai API
70
+ email:
71
+ - fukui.development@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".gitlab-ci.yml"
78
+ - ".rspec"
79
+ - CHANGELOG.md
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - LICENSE
83
+ - README.md
84
+ - Rakefile
85
+ - bin/console
86
+ - bin/setup
87
+ - gitlab-ci/rspec.sh
88
+ - gitlab-ci/setup.sh
89
+ - lib/mfkessai.rb
90
+ - lib/mfkessai/client.rb
91
+ - lib/mfkessai/customer.rb
92
+ - lib/mfkessai/destination.rb
93
+ - lib/mfkessai/errors.rb
94
+ - lib/mfkessai/examination.rb
95
+ - lib/mfkessai/ping.rb
96
+ - lib/mfkessai/transaction.rb
97
+ - lib/mfkessai/version.rb
98
+ - mfkessai-ruby.gemspec
99
+ homepage: https://gitlab.com/torufukui/mfkessai-ruby
100
+ licenses:
101
+ - MIT
102
+ metadata: {}
103
+ post_install_message:
104
+ rdoc_options: []
105
+ require_paths:
106
+ - lib
107
+ required_ruby_version: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - ">="
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ required_rubygems_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">"
115
+ - !ruby/object:Gem::Version
116
+ version: 1.3.1
117
+ requirements: []
118
+ rubyforge_project:
119
+ rubygems_version: 2.7.6
120
+ signing_key:
121
+ specification_version: 4
122
+ summary: Ruby bindings for the Mfkessai API
123
+ test_files: []