jamm 0.0.1 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +6 -57
  3. data/.rubocop.yml +18 -25
  4. data/Gemfile +4 -0
  5. data/Gemfile.lock +99 -0
  6. data/README.md +4 -4
  7. data/Rakefile +2 -0
  8. data/jamm.gemspec +16 -3
  9. data/lib/jamm/api/api/customer_api.rb +265 -0
  10. data/lib/jamm/api/api/healthcheck_api.rb +76 -0
  11. data/lib/jamm/api/api/payment_api.rb +203 -0
  12. data/lib/jamm/api/api_client.rb +385 -0
  13. data/lib/jamm/api/api_error.rb +58 -0
  14. data/lib/jamm/api/configuration.rb +293 -0
  15. data/lib/jamm/api/models/customer_service_update_customer_body.rb +247 -0
  16. data/lib/jamm/api/models/protobuf_any.rb +209 -0
  17. data/lib/jamm/api/models/rpc_status.rb +218 -0
  18. data/lib/jamm/api/models/v1_buyer.rb +274 -0
  19. data/lib/jamm/api/models/v1_charge.rb +233 -0
  20. data/lib/jamm/api/models/v1_contract.rb +225 -0
  21. data/lib/jamm/api/models/v1_create_contract_with_charge_request.rb +217 -0
  22. data/lib/jamm/api/models/v1_create_contract_with_charge_response.rb +222 -0
  23. data/lib/jamm/api/models/v1_create_contract_without_charge_request.rb +212 -0
  24. data/lib/jamm/api/models/v1_create_contract_without_charge_response.rb +217 -0
  25. data/lib/jamm/api/models/v1_create_customer_request.rb +207 -0
  26. data/lib/jamm/api/models/v1_create_customer_response.rb +207 -0
  27. data/lib/jamm/api/models/v1_customer.rb +223 -0
  28. data/lib/jamm/api/models/v1_delete_customer_response.rb +207 -0
  29. data/lib/jamm/api/models/v1_get_contract_response.rb +212 -0
  30. data/lib/jamm/api/models/v1_get_customer_response.rb +207 -0
  31. data/lib/jamm/api/models/v1_initial_charge.rb +225 -0
  32. data/lib/jamm/api/models/v1_merchant.rb +212 -0
  33. data/lib/jamm/api/models/v1_merchant_customer.rb +213 -0
  34. data/lib/jamm/api/models/v1_payment_link.rb +221 -0
  35. data/lib/jamm/api/models/v1_ping_response.rb +207 -0
  36. data/lib/jamm/api/models/v1_update_customer_response.rb +207 -0
  37. data/lib/jamm/api/models/v1_url.rb +216 -0
  38. data/lib/jamm/api/version.rb +15 -0
  39. data/lib/jamm/api.rb +65 -0
  40. data/lib/jamm/client.rb +14 -0
  41. data/lib/jamm/customer.rb +28 -0
  42. data/lib/jamm/errors.rb +4 -14
  43. data/lib/jamm/oauth.rb +5 -5
  44. data/lib/jamm/payment.rb +19 -11
  45. data/lib/jamm/version.rb +3 -1
  46. data/lib/jamm.rb +37 -117
  47. metadata +62 -37
  48. data/.github/workflows/build.yml +0 -23
  49. data/.github/workflows/ruby-publish.yml +0 -24
  50. data/CONTRIBUTORS +0 -1
  51. data/images/jamm_logo.png +0 -0
  52. data/lib/jamm/api_operations/create.rb +0 -16
  53. data/lib/jamm/api_operations/get.rb +0 -16
  54. data/lib/jamm/api_operations/list.rb +0 -16
  55. data/lib/jamm/api_operations/update.rb +0 -16
  56. data/lib/jamm/api_resource.rb +0 -5
  57. data/lib/jamm/charge.rb +0 -10
  58. data/lib/jamm/jamm_object.rb +0 -249
  59. data/lib/jamm/request.rb +0 -13
  60. data/lib/jamm/time_util.rb +0 -41
  61. data/lib/jamm/token.rb +0 -10
  62. data/lib/jamm/util.rb +0 -76
  63. data/test/jamm/charge_test.rb +0 -56
  64. data/test/jamm/oauth_test.rb +0 -47
  65. data/test/jamm/payment_test.rb +0 -17
  66. data/test/jamm/time_util_test.rb +0 -18
  67. data/test/jamm/token_test.rb +0 -52
  68. data/test/test_data.rb +0 -118
  69. data/test/test_helper.rb +0 -59
@@ -0,0 +1,216 @@
1
+ # frozen_string_literal: true
2
+
3
+ # #api/v1/common.proto
4
+ #
5
+ # No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6
+ #
7
+ # The version of the OpenAPI document: version not set
8
+ #
9
+ # Generated by: https://openapi-generator.tech
10
+ # Generator version: 7.9.0
11
+ #
12
+
13
+ require 'date'
14
+ require 'time'
15
+
16
+ module Api
17
+ class URL
18
+ # URL to redirect the customer from Jamm to shop after payment is successful. 決済が成功した場合に、顧客をJammからショップへリダイレクトするURLです。 @gotags: validate:\"required,http_url\"
19
+ attr_accessor :success_url
20
+
21
+ # URL to redirect the customer from Jamm to shop after payment is failed. 決済が失敗した場合に、顧客をJammからショップへリダイレクトするURLです。 @gotags: validate:\"required,http_url\"
22
+ attr_accessor :failure_url
23
+
24
+ # Attribute mapping from ruby-style variable name to JSON key.
25
+ def self.attribute_map
26
+ {
27
+ :success_url => :successUrl,
28
+ :failure_url => :failureUrl
29
+ }
30
+ end
31
+
32
+ # Returns all the JSON keys this model knows about
33
+ def self.acceptable_attributes
34
+ attribute_map.values
35
+ end
36
+
37
+ # Attribute type mapping.
38
+ def self.openapi_types
39
+ {
40
+ :success_url => :String,
41
+ :failure_url => :String
42
+ }
43
+ end
44
+
45
+ # List of attributes with nullable: true
46
+ def self.openapi_nullable
47
+ Set.new([])
48
+ end
49
+
50
+ # Initializes the object
51
+ # @param [Hash] attributes Model attributes in the form of hash
52
+ def initialize(attributes = {})
53
+ raise ArgumentError, 'The input argument (attributes) must be a hash in `Api::URL` initialize method' unless attributes.is_a?(Hash)
54
+
55
+ # check to see if the attribute exists and convert string to symbol for hash key
56
+ attributes = attributes.each_with_object({}) do |(k, v), h|
57
+ raise ArgumentError, "`#{k}` is not a valid attribute in `Api::URL`. Please check the name to make sure it's valid. List of attributes: " + self.class.attribute_map.keys.inspect unless self.class.attribute_map.key?(k.to_sym)
58
+
59
+ h[k.to_sym] = v
60
+ end
61
+
62
+ self.success_url = attributes[:success_url] if attributes.key?(:success_url)
63
+
64
+ return unless attributes.key?(:failure_url)
65
+
66
+ self.failure_url = attributes[:failure_url]
67
+ end
68
+
69
+ # Show invalid properties with the reasons. Usually used together with valid?
70
+ # @return Array for valid properties with the reasons
71
+ def list_invalid_properties
72
+ warn '[DEPRECATED] the `list_invalid_properties` method is obsolete'
73
+ []
74
+ end
75
+
76
+ # Check to see if the all the properties in the model are valid
77
+ # @return true if the model is valid
78
+ def valid?
79
+ warn '[DEPRECATED] the `valid?` method is obsolete'
80
+ true
81
+ end
82
+
83
+ # Checks equality by comparing each attribute.
84
+ # @param [Object] Object to be compared
85
+ def ==(other)
86
+ return true if equal?(other)
87
+
88
+ self.class == other.class &&
89
+ success_url == other.success_url &&
90
+ failure_url == other.failure_url
91
+ end
92
+
93
+ # @see the `==` method
94
+ # @param [Object] Object to be compared
95
+ def eql?(other)
96
+ self == other
97
+ end
98
+
99
+ # Calculates hash code according to all attributes.
100
+ # @return [Integer] Hash code
101
+ def hash
102
+ [success_url, failure_url].hash
103
+ end
104
+
105
+ # Builds the object from hash
106
+ # @param [Hash] attributes Model attributes in the form of hash
107
+ # @return [Object] Returns the model itself
108
+ def self.build_from_hash(attributes)
109
+ return nil unless attributes.is_a?(Hash)
110
+
111
+ attributes = attributes.transform_keys(&:to_sym)
112
+ transformed_hash = {}
113
+ openapi_types.each_pair do |key, type|
114
+ if attributes.key?(attribute_map[key]) && attributes[attribute_map[key]].nil?
115
+ transformed_hash[key.to_s] = nil
116
+ elsif type =~ /\AArray<(.*)>/i
117
+ # check to ensure the input is an array given that the attribute
118
+ # is documented as an array but the input is not
119
+ transformed_hash[key.to_s] = attributes[attribute_map[key]].map { |v| _deserialize(::Regexp.last_match(1), v) } if attributes[attribute_map[key]].is_a?(Array)
120
+ elsif !attributes[attribute_map[key]].nil?
121
+ transformed_hash[key.to_s] = _deserialize(type, attributes[attribute_map[key]])
122
+ end
123
+ end
124
+ new(transformed_hash)
125
+ end
126
+
127
+ # Deserializes the data based on type
128
+ # @param string type Data type
129
+ # @param string value Value to be deserialized
130
+ # @return [Object] Deserialized data
131
+ def self._deserialize(type, value)
132
+ case type.to_sym
133
+ when :Time
134
+ Time.parse(value)
135
+ when :Date
136
+ Date.parse(value)
137
+ when :String
138
+ value.to_s
139
+ when :Integer
140
+ value.to_i
141
+ when :Float
142
+ value.to_f
143
+ when :Boolean
144
+ if value.to_s =~ /\A(true|t|yes|y|1)\z/i
145
+ true
146
+ else
147
+ false
148
+ end
149
+ when :Object
150
+ # generic object (usually a Hash), return directly
151
+ value
152
+ when /\AArray<(?<inner_type>.+)>\z/
153
+ inner_type = Regexp.last_match[:inner_type]
154
+ value.map { |v| _deserialize(inner_type, v) }
155
+ when /\AHash<(?<k_type>.+?), (?<v_type>.+)>\z/
156
+ k_type = Regexp.last_match[:k_type]
157
+ v_type = Regexp.last_match[:v_type]
158
+ {}.tap do |hash|
159
+ value.each do |k, v|
160
+ hash[_deserialize(k_type, k)] = _deserialize(v_type, v)
161
+ end
162
+ end
163
+ else # model
164
+ # models (e.g. Pet) or oneOf
165
+ klass = Api.const_get(type)
166
+ klass.respond_to?(:openapi_any_of) || klass.respond_to?(:openapi_one_of) ? klass.build(value) : klass.build_from_hash(value)
167
+ end
168
+ end
169
+
170
+ # Returns the string representation of the object
171
+ # @return [String] String presentation of the object
172
+ def to_s
173
+ to_hash.to_s
174
+ end
175
+
176
+ # to_body is an alias to to_hash (backward compatibility)
177
+ # @return [Hash] Returns the object in the form of hash
178
+ def to_body
179
+ to_hash
180
+ end
181
+
182
+ # Returns the object in the form of hash
183
+ # @return [Hash] Returns the object in the form of hash
184
+ def to_hash
185
+ hash = {}
186
+ self.class.attribute_map.each_pair do |attr, param|
187
+ value = send(attr)
188
+ if value.nil?
189
+ is_nullable = self.class.openapi_nullable.include?(attr)
190
+ next if !is_nullable || (is_nullable && !instance_variable_defined?(:"@#{attr}"))
191
+ end
192
+
193
+ hash[param] = _to_hash(value)
194
+ end
195
+ hash
196
+ end
197
+
198
+ # Outputs non-array value in the form of hash
199
+ # For object, use to_hash. Otherwise, just return the value
200
+ # @param [Object] value Any valid value
201
+ # @return [Hash] Returns the value in the form of hash
202
+ def _to_hash(value)
203
+ if value.is_a?(Array)
204
+ value.compact.map { |v| _to_hash(v) }
205
+ elsif value.is_a?(Hash)
206
+ {}.tap do |hash|
207
+ value.each { |k, v| hash[k] = _to_hash(v) }
208
+ end
209
+ elsif value.respond_to? :to_hash
210
+ value.to_hash
211
+ else
212
+ value
213
+ end
214
+ end
215
+ end
216
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ # #api/v1/common.proto
4
+ #
5
+ # No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6
+ #
7
+ # The version of the OpenAPI document: version not set
8
+ #
9
+ # Generated by: https://openapi-generator.tech
10
+ # Generator version: 7.9.0
11
+ #
12
+
13
+ module Api
14
+ VERSION = '1.0.0'
15
+ end
data/lib/jamm/api.rb ADDED
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # #api/v1/common.proto
4
+ #
5
+ # No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
6
+ #
7
+ # The version of the OpenAPI document: version not set
8
+ #
9
+ # Generated by: https://openapi-generator.tech
10
+ # Generator version: 7.9.0
11
+ #
12
+
13
+ # Common files
14
+ require 'jamm/api/api_client'
15
+ require 'jamm/api/api_error'
16
+ require 'jamm/api/version'
17
+ require 'jamm/api/configuration'
18
+
19
+ # Models
20
+ require 'jamm/api/models/customer_service_update_customer_body'
21
+ require 'jamm/api/models/protobuf_any'
22
+ require 'jamm/api/models/rpc_status'
23
+ require 'jamm/api/models/v1_buyer'
24
+ require 'jamm/api/models/v1_charge'
25
+ require 'jamm/api/models/v1_contract'
26
+ require 'jamm/api/models/v1_create_contract_with_charge_request'
27
+ require 'jamm/api/models/v1_create_contract_with_charge_response'
28
+ require 'jamm/api/models/v1_create_contract_without_charge_request'
29
+ require 'jamm/api/models/v1_create_contract_without_charge_response'
30
+ require 'jamm/api/models/v1_create_customer_request'
31
+ require 'jamm/api/models/v1_create_customer_response'
32
+ require 'jamm/api/models/v1_customer'
33
+ require 'jamm/api/models/v1_delete_customer_response'
34
+ require 'jamm/api/models/v1_get_contract_response'
35
+ require 'jamm/api/models/v1_get_customer_response'
36
+ require 'jamm/api/models/v1_initial_charge'
37
+ require 'jamm/api/models/v1_merchant'
38
+ require 'jamm/api/models/v1_merchant_customer'
39
+ require 'jamm/api/models/v1_payment_link'
40
+ require 'jamm/api/models/v1_ping_response'
41
+ require 'jamm/api/models/v1_url'
42
+ require 'jamm/api/models/v1_update_customer_response'
43
+
44
+ # APIs
45
+ require 'jamm/api/api/customer_api'
46
+ require 'jamm/api/api/healthcheck_api'
47
+ require 'jamm/api/api/payment_api'
48
+
49
+ module Api
50
+ class << self
51
+ # Customize default settings for the SDK using block.
52
+ # Api.configure do |config|
53
+ # config.username = "xxx"
54
+ # config.password = "xxx"
55
+ # end
56
+ # If no block given, return the default Configuration object.
57
+ def configure
58
+ if block_given?
59
+ yield(Configuration.default)
60
+ else
61
+ Configuration.default
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,14 @@
1
+ module Jamm
2
+ module Client
3
+ def self.handler
4
+ base = Jamm::ApiClient.new
5
+
6
+ # Configure
7
+ base.config.host = Jamm.openapi.config.host
8
+ base.config.scheme = Jamm.openapi.config.scheme
9
+ base.default_headers['Authorization'] = "Bearer #{Jamm::OAuth.token}"
10
+
11
+ base
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest-client'
4
+ require 'json'
5
+ require 'base64'
6
+ require 'jamm/errors'
7
+
8
+ module Jamm
9
+ module Customer
10
+ def self.get(customer:)
11
+ Jamm::CustomerApi.new(Jamm::Client.handler).get customer
12
+ rescue Jamm::ApiError => e
13
+ puts e
14
+ end
15
+
16
+ def self.update(**params)
17
+ Jamm::CustomerApi.new(Jamm::Client.handler).update(params[:id], params)
18
+ rescue Jamm::ApiError => e
19
+ puts e
20
+ end
21
+
22
+ def self.delete(customer:)
23
+ Jamm::CustomerApi.new(Jamm::Client.handler).delete customer
24
+ rescue Jamm::ApiError => e
25
+ puts e
26
+ end
27
+ end
28
+ end
data/lib/jamm/errors.rb CHANGED
@@ -1,8 +1,12 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jamm
2
4
  class JammError < StandardError
3
5
  attr_reader :message, :http_body, :http_headers, :http_status, :json_body
4
6
 
5
7
  def initialize(message = nil, http_status: nil, http_headers: nil, http_body: nil, json_body: nil)
8
+ super(message)
9
+
6
10
  @message = message
7
11
  @http_status = http_status
8
12
  @http_headers = http_headers
@@ -19,18 +23,4 @@ module Jamm
19
23
  # OAuthError is raised when a request to AWS cognito failed
20
24
  class OAuthError < JammError
21
25
  end
22
-
23
- # ApiConnectionError is raised when a request to Jamm api failed
24
- class ApiConnectionError < JammError
25
- end
26
-
27
- # ApiError is raised when Jamm api failed with an unexpected reason
28
- class ApiError < JammError
29
- end
30
-
31
- class InvalidRequestError < JammError
32
- end
33
-
34
- class AuthenticationError < JammError
35
- end
36
26
  end
data/lib/jamm/oauth.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rest-client'
2
4
  require 'json'
3
5
  require 'base64'
@@ -5,16 +7,14 @@ require 'jamm/errors'
5
7
 
6
8
  module Jamm
7
9
  module OAuth
8
- def self.oauth_token
9
- client_id = Jamm.client_id
10
- client_secret = Jamm.client_secret
11
- if client_id.nil? || client_secret.nil?
10
+ def self.token
11
+ if Jamm.client_id.nil? || Jamm.client_secret.nil?
12
12
  raise OAuthError, 'No client_id or client_secret is set. ' \
13
13
  'Set your merchant client id and client secret using' \
14
14
  'Jamm.client_id=<your client id> and Jamm=<your client secret>'
15
15
  end
16
16
 
17
- fetch_oauth_token(client_id, client_secret)
17
+ fetch_oauth_token(Jamm.client_id, Jamm.client_secret)
18
18
  end
19
19
 
20
20
  def self.fetch_oauth_token(client_id, client_secret)
data/lib/jamm/payment.rb CHANGED
@@ -1,18 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rest-client'
4
+ require 'json'
5
+ require 'base64'
6
+ require 'jamm/errors'
7
+
1
8
  module Jamm
2
- class Payment < APIResource
3
- def self.create(params = {}, headers = {})
4
- params[:redirect][:expired_at] = TimeUtil.relative_time_to_iso(params[:redirect][:expired_in]) if params[:redirect] && params[:redirect][:expired_in]
9
+ module Payment
10
+ def self.contract_with_charge(**params)
11
+ request = Jamm::CreateContractWithChargeRequest.new(params)
5
12
 
6
- request_jamm_api(
7
- method: :post,
8
- path: resource_url,
9
- params: params,
10
- headers: headers
11
- )
13
+ begin
14
+ Jamm::PaymentApi.new(Jamm::Client.handler).create_contract_with_charge(request)
15
+ rescue Jamm::ApiError => e
16
+ puts e
17
+ end
12
18
  end
13
19
 
14
- def self.resource_url
15
- '/payments'
20
+ def self.get_contract(contract:)
21
+ Jamm::PaymentApi.new(Jamm::Client.handler).get_contract(contract)
22
+ rescue Jamm::ApiError => e
23
+ puts e
16
24
  end
17
25
  end
18
26
  end
data/lib/jamm/version.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Jamm
2
- VERSION = '0.0.1'
4
+ VERSION = '1.0.2'
3
5
  end
data/lib/jamm.rb CHANGED
@@ -1,146 +1,66 @@
1
- # FIXME: remove this line once manual testing completed
2
- $LOAD_PATH.unshift(File.dirname(__FILE__))
1
+ # frozen_string_literal: true
2
+
3
3
  require 'rest-client'
4
4
  require 'openssl'
5
5
  require 'json'
6
6
 
7
- require 'jamm/api_operations/create'
8
- require 'jamm/api_operations/get'
9
- require 'jamm/api_operations/list'
10
- require 'jamm/api_operations/update'
11
- require 'jamm/version'
7
+ require 'jamm/api'
8
+ require 'jamm/client'
9
+ require 'jamm/errors'
12
10
  require 'jamm/oauth'
13
- require 'jamm/request'
14
- require 'jamm/api_resource'
15
- require 'jamm/token'
11
+ require 'jamm/version'
12
+
13
+ # OpenAPI wrappers
14
+ require 'jamm/customer'
16
15
  require 'jamm/payment'
17
- require 'jamm/charge'
18
- require 'jamm/util'
19
- require 'jamm/jamm_object'
20
- require 'jamm/errors'
21
- require 'jamm/time_util'
22
16
 
17
+ # Jamm Ruby SDK
23
18
  module Jamm
24
- @api_base = 'http://api.jamm-pay.jp/api/v0'
25
- @oauth_base = 'https://jamm-merchant-sandbox.auth.ap-northeast-1.amazoncognito.com'
19
+ # Configurable attributes.
20
+ @oauth_base = nil
21
+ @openapi = nil
26
22
  @open_timeout = 30
27
23
  @read_timeout = 90
28
24
  @max_retry = 0
29
- @retry_initial_delay = 0.1
30
- @retry_max_delay = 32
25
+
26
+ # Include OpenAPI generated module into root Jamm namespace.
27
+ # The OpenAPI sub-modules and methods can be accessed via Jamm::{foo}
28
+ include Api
31
29
 
32
30
  class << self
33
- attr_accessor :client_id, :client_secret, :api_base, :oauth_base, :api_version, :connect_base,
34
- :open_timeout, :read_timeout, :max_retry, :retry_initial_delay, :retry_max_delay
31
+ attr_accessor :api, :client_id, :client_secret, :api_base, :env, :oauth_base, :api_version, :connect_base,
32
+ :openapi, :open_timeout, :read_timeout, :max_retry, :retry_initial_delay, :retry_max_delay
35
33
  end
36
34
 
37
- def self.request_jamm_api(method, path, params = {}, headers = {})
38
- access_token = OAuth.oauth_token
39
- api_base_url = @api_base
40
- open_timeout = @open_timeout
41
- read_timeout = @read_timeout
42
- max_retry = @max_retry
43
- retry_initial_delay = @retry_initial_delay
44
- retry_max_delay = @retry_max_delay
45
-
46
- headers = {
47
- authorization: "Bearer #{access_token}",
48
- content_type: 'application/json',
49
- X_JAMM_SDK_VERSION: "Ruby_#{VERSION}"
50
- }.update(headers)
51
- error_on_invalid_params(params)
52
-
53
- url = api_url(path, api_base_url)
54
-
55
- case method.to_s.downcase.to_sym
56
- when :get, :head, :delete
57
- # Make params into GET parameters
58
- url + "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params&.any?
59
- payload = nil
60
- else
61
- payload = params.to_json
62
- end
63
-
64
- retry_count = 1
65
-
66
- begin
67
- response = execute_request(method: method, url: url, payload: payload, headers: headers,
68
- read_timeout: read_timeout, open_timeout: open_timeout)
69
- rescue SocketError
70
- raise ApiConnectionError,
71
- 'An unexpected error happens while communicating to OAuth server. Check your network setting'
72
- rescue RestClient::RequestTimeout
73
- raise ApiConnectionError, "Timed out over #{read_timeout} sec."
74
- rescue RestClient::ExceptionWithResponse => e
75
- if (e.http_code == 429) && (retry_count <= max_retry)
76
- sleep get_retry_delay(retry_count, retry_initial_delay, retry_max_delay)
77
- retry_count += 1
78
- retry
79
- end
35
+ def self.configure(client_id:, client_secret:, env:)
36
+ Jamm.client_id = client_id
37
+ Jamm.client_secret = client_secret
80
38
 
81
- handle_api_error(e.http_code, e.http_body)
82
- rescue RestClient::Exception => e
83
- raise ApiError, "An unexpected error happens while communicating to Jamm server. #{e}"
84
- end
39
+ case env
40
+ when 'prod'
41
+ self.oauth_base = 'https://merchant-identity.jamm-pay.jp'
85
42
 
86
- parse(response)
87
- end
43
+ self.openapi = Jamm::ApiClient.new
44
+ openapi.config.host = 'api.jamm-pay.jp'
88
45
 
89
- def self.handle_api_error(http_code, http_body)
90
- begin
91
- error_obj = JSON.parse(http_body)
92
- error_obj = Util.symbolize_names(error_obj)
93
- error = error_obj[:error] or raise(ApiError) # escape from parsing
94
- rescue JSON::ParserError, ApiError => e
95
- raise ApiError.new "An unexpected error happens while parsing the error response. #{e}", http_status: http_code,
96
- http_body: http_body
97
- end
46
+ # Debugger mode, uncomment when needed
47
+ # when 'develop'
48
+ # self.oauth_base = "https://merchant-identity.#{env}.jamm-pay.jp"
49
+ #
50
+ # self.openapi = Jamm::ApiClient.new
51
+ # openapi.config.host = 'api.jamm.test'
98
52
 
99
- case http_code
100
- when 400, 404
101
- raise InvalidRequestError.new error, http_status: http_code, http_body: http_body, json_body: error_obj
102
- when 401
103
- raise AuthenticationError.new error, http_status: http_code, http_body: http_body, json_body: error_obj
104
53
  else
105
- raise ApiError.new error, http_status: http_code, http_body: http_body, json_body: error_obj
106
- end
107
- end
54
+ self.oauth_base = "https://merchant-identity.#{env}.jamm-pay.jp"
108
55
 
109
- def self.parse(response)
110
- begin
111
- response = JSON.parse(response.body)
112
- rescue JSON::ParserError
113
- raise ApiError.new "Invalid response object from API: #{response.inspect}. HTTP response code: #{response.code})",
114
- http_status: response.code, http_body: response.body
56
+ self.openapi = Jamm::ApiClient.new
57
+ openapi.config.host = "api.#{env}.jamm-pay.jp"
115
58
  end
116
59
 
117
- converted_response = Util.symbolize_names(response)
118
- Util.convert_to_jamm_object(converted_response)
60
+ openapi.config.scheme = 'https'
119
61
  end
120
62
 
121
63
  def self.execute_request(option)
122
64
  RestClient::Request.execute(option)
123
65
  end
124
-
125
- def self.get_retry_delay(retry_count, retry_initial_delay, retry_max_delay)
126
- # Exponential backoff
127
- [retry_max_delay, retry_initial_delay * 2**retry_count].min
128
- end
129
-
130
- def self.uri_encode(params)
131
- Util.flatten_params(params)
132
- .map { |k, v| "#{k}=#{Util.url_encode(v)}" }.join('&')
133
- end
134
-
135
- def self.api_url(url, api_base_url)
136
- api_base_url + url
137
- end
138
-
139
- def self.error_on_invalid_params(params)
140
- return if params.nil? || params.is_a?(Hash)
141
-
142
- raise ArgumentError,
143
- 'request params should be either a Hash or nil ' \
144
- "(was a #{params.class})"
145
- end
146
66
  end