transbank-sdk 1.1.0 → 1.2.0
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.
- checksums.yaml +4 -4
- data/.github/ISSUE_TEMPLATE/reporte-de-error.md +40 -0
- data/.gitignore +3 -1
- data/.travis.yml +2 -0
- data/CHANGELOG.md +4 -0
- data/Makefile +1 -1
- data/lib/transbank/sdk.rb +5 -5
- data/lib/transbank/sdk/oneclick/errors/oneclick_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_delete_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_finish_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_start_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/inscription/errors/oneclick_mall_inscription_errors.rb +3 -0
- data/lib/transbank/sdk/oneclick/inscription/inscription.rb +3 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_delete_response.rb +18 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_finish_response.rb +15 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_start_response.rb +14 -0
- data/lib/transbank/sdk/oneclick/inscription/responses/oneclick_mall_inscription_responses.rb +3 -0
- data/lib/transbank/sdk/oneclick/mall/base.rb +51 -0
- data/lib/transbank/sdk/oneclick/mall/errors/mall_transaction_authorize_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/errors/mall_transaction_refund_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/errors/mall_transaction_status_error.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/errors/oneclick_mall_errors.rb +3 -0
- data/lib/transbank/sdk/oneclick/mall/mall_inscription.rb +73 -0
- data/lib/transbank/sdk/oneclick/mall/mall_transaction.rb +97 -0
- data/lib/transbank/sdk/oneclick/mall/oneclick_mall.rb +5 -0
- data/lib/transbank/sdk/oneclick/mall/responses/mall_transaction_authorize_response.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/responses/mall_transaction_refund_response.rb +16 -0
- data/lib/transbank/sdk/oneclick/mall/responses/mall_transaction_status_response.rb +15 -0
- data/lib/transbank/sdk/oneclick/mall/responses/oneclick_mall_responses.rb +3 -0
- data/lib/transbank/sdk/oneclick/oneclick.rb +3 -0
- data/lib/transbank/sdk/onepay/models/item.rb +1 -1
- data/lib/transbank/sdk/onepay/models/refund.rb +4 -5
- data/lib/transbank/sdk/onepay/models/shopping_cart.rb +1 -1
- data/lib/transbank/sdk/onepay/models/transaction.rb +11 -9
- data/lib/transbank/sdk/onepay/requests/request.rb +1 -1
- data/lib/transbank/sdk/onepay/responses/response.rb +1 -1
- data/lib/transbank/sdk/transaccion_completa/base.rb +49 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaccion_completa_error.rb +16 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaccion_completa_errors.rb +6 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_commit_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_create_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_installments_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_refund_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/errors/transaction_status_error.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/options.rb +7 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaccion_completa_responses.rb +5 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_commit_response.rb +16 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_create_response.rb +10 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_installments_response.rb +13 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_refund_response.rb +14 -0
- data/lib/transbank/sdk/transaccion_completa/responses/transaction_status_response.rb +16 -0
- data/lib/transbank/sdk/transaccion_completa/transaccion_completa.rb +5 -0
- data/lib/transbank/sdk/transaccion_completa/transaction.rb +117 -0
- data/lib/transbank/sdk/transbank_error.rb +6 -0
- data/lib/transbank/sdk/utils/json_utils.rb +75 -0
- data/lib/transbank/sdk/utils/net_helper.rb +82 -0
- data/lib/transbank/sdk/{onepay/utils → utils}/request_builder.rb +0 -0
- data/lib/transbank/sdk/{onepay/utils → utils}/signature_utils.rb +0 -0
- data/lib/transbank/sdk/utils/utils.rb +9 -0
- data/lib/transbank/sdk/version.rb +1 -1
- data/lib/transbank/sdk/webpay/errors/integration_type_error.rb +8 -0
- data/lib/transbank/sdk/webpay/errors/webpay_error.rb +14 -0
- data/transbank-sdk.gemspec +1 -1
- metadata +56 -10
- data/lib/transbank/sdk/onepay/utils/json_utils.rb +0 -77
- data/lib/transbank/sdk/onepay/utils/net_helper.rb +0 -38
- data/lib/transbank/sdk/onepay/utils/utils.rb +0 -9
@@ -0,0 +1,75 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Utils
|
3
|
+
module JSONUtils
|
4
|
+
def self.included(mod)
|
5
|
+
# Implement #to_h if the class that includes this module doesn't have it
|
6
|
+
# implemented. Used in several model classes to make them easier to
|
7
|
+
# transform to hashes so they can be transformed to JSON afterwards
|
8
|
+
unless mod.respond_to? :to_h
|
9
|
+
mod.send(:define_method, :to_h) do
|
10
|
+
JSON.parse(self.jsonify)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
# Get all instance variables of an instance of a class,
|
15
|
+
# then for all of these variables,
|
16
|
+
# if the instance of the class' respond_to? returns true,
|
17
|
+
# #send the variable name (so, you'll get the value of the instance variable's
|
18
|
+
# getter), then save that to a Hash where [key] is the instance variable's name
|
19
|
+
# and [value] is its value
|
20
|
+
#
|
21
|
+
# Finally, generate a JSON string from this hash
|
22
|
+
# @return [String] a JSON string created from the Hash resulting from the
|
23
|
+
# above operation
|
24
|
+
def jsonify
|
25
|
+
instance_vars = instance_variables.map! { |var| var.to_s.gsub!(/^@/, '') }
|
26
|
+
instance_as_hash =
|
27
|
+
instance_vars.reduce({}) do |resulting_hash, instance_variable|
|
28
|
+
if respond_to? instance_variable
|
29
|
+
value = send(instance_variable)
|
30
|
+
# Safe navigation operator is Ruby 2.3+
|
31
|
+
value = value.to_h if value && value.respond_to?(:to_h) unless value.is_a? Array
|
32
|
+
value = value.to_a if value && value.respond_to?(:to_a) unless value.is_a? Hash
|
33
|
+
if value.is_a? Array
|
34
|
+
value = value.map {|x| x.respond_to?(:jsonify) ? JSON.parse(x.jsonify) : x }
|
35
|
+
end
|
36
|
+
|
37
|
+
value = value.jsonify if value.respond_to? :jsonify
|
38
|
+
resulting_hash[instance_variable] = value
|
39
|
+
end
|
40
|
+
resulting_hash
|
41
|
+
end
|
42
|
+
# Some values can't be null and must be removed if they are
|
43
|
+
instance_as_hash.reject! do |key, value|
|
44
|
+
%w[commerce_logo_url width_height].include?(key) && value.nil?
|
45
|
+
end
|
46
|
+
JSON.generate instance_as_hash
|
47
|
+
end
|
48
|
+
|
49
|
+
# Receive a Hash and return a new hash same as the one we received,
|
50
|
+
# but all keys that were strings or camelCase'd are snake_case'd and
|
51
|
+
# turned into symbols.
|
52
|
+
# Example: {'camelCaseKey': "somevalue"}
|
53
|
+
# Would return: {camel_case_key: "somevalue"}
|
54
|
+
def transform_hash_keys(hash)
|
55
|
+
hash.reduce({}) do |new_hsh, (key, val)|
|
56
|
+
new_key = underscore(key).to_sym
|
57
|
+
new_hsh[new_key] = val
|
58
|
+
new_hsh
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# FROM https://stackoverflow.com/a/1509957
|
63
|
+
# Transforms camelCaseWords to snake_case_words
|
64
|
+
def underscore(camel_cased_word)
|
65
|
+
camel_cased_word.to_s.gsub(/::/, '/')
|
66
|
+
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
67
|
+
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
|
68
|
+
.tr("-", "_")
|
69
|
+
.downcase
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
|
@@ -0,0 +1,82 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Utils
|
3
|
+
module NetHelper
|
4
|
+
def http_get(uri_string:, headers:nil)
|
5
|
+
uri = URI.parse(uri_string)
|
6
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
7
|
+
http.use_ssl = uri.scheme == 'https'
|
8
|
+
request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
|
9
|
+
request = Net::HTTP::Get.new(uri.path, request_headers)
|
10
|
+
http.request(request)
|
11
|
+
end
|
12
|
+
# POST a request to Transbank's servers, and return the parsed response
|
13
|
+
# @param uri_string [String] an URI to post to
|
14
|
+
# @param body [Hash] the body of your POST request
|
15
|
+
# @return [Hash] the JSON.parse'd response body
|
16
|
+
def http_post(uri_string:, body: nil, headers: nil, camel_case_keys: true)
|
17
|
+
uri = URI.parse(uri_string)
|
18
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
19
|
+
http.use_ssl = uri.scheme == 'https'
|
20
|
+
|
21
|
+
request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
|
22
|
+
request = Net::HTTP::Post.new(uri.path, request_headers)
|
23
|
+
sendable_body = camel_case_keys ? keys_to_camel_case(body) : body
|
24
|
+
request.body = sendable_body.to_json
|
25
|
+
http.request(request)
|
26
|
+
end
|
27
|
+
|
28
|
+
def http_put(uri_string:, body: nil, headers: nil)
|
29
|
+
uri = URI.parse(uri_string)
|
30
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
31
|
+
http.use_ssl = uri.scheme == 'https'
|
32
|
+
|
33
|
+
request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
|
34
|
+
request = Net::HTTP::Put.new(uri.path, request_headers)
|
35
|
+
request.body = JSON.generate(body)
|
36
|
+
http.request(request)
|
37
|
+
end
|
38
|
+
|
39
|
+
def http_delete(uri_string:, body: nil, headers: nil)
|
40
|
+
uri = URI.parse(uri_string)
|
41
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
42
|
+
http.use_ssl = uri.scheme == 'https'
|
43
|
+
|
44
|
+
request_headers = {'Content-Type' => 'application/json'}.merge(headers || {})
|
45
|
+
request = Net::HTTP::Delete.new(uri.path, request_headers)
|
46
|
+
request.body = JSON.generate(body)
|
47
|
+
http.request(request)
|
48
|
+
end
|
49
|
+
|
50
|
+
# Required for sending data to Transbank on Onepay.
|
51
|
+
def keys_to_camel_case(hash)
|
52
|
+
hash.reduce({}) do |new_hash, (key, val)|
|
53
|
+
if val.is_a? Array
|
54
|
+
val = val.map {|value| value.is_a?(Hash) ? keys_to_camel_case(value) : value }
|
55
|
+
end
|
56
|
+
new_key = snake_to_camel_case(key.to_s)
|
57
|
+
new_hash[new_key] = val
|
58
|
+
new_hash
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def snake_to_camel_case(str)
|
63
|
+
str.split('_').reduce { |string, current_word| string + current_word.capitalize }
|
64
|
+
end
|
65
|
+
|
66
|
+
def webpay_headers(commerce_code:, api_key:)
|
67
|
+
{
|
68
|
+
"Tbk-Api-Key-Id" => commerce_code,
|
69
|
+
"Tbk-Api-Key-Secret" => api_key
|
70
|
+
}
|
71
|
+
end
|
72
|
+
|
73
|
+
def patpass_comercio_headers(commerce_code:, api_key:)
|
74
|
+
{
|
75
|
+
"commercecode" => commerce_code,
|
76
|
+
"Authorization" => api_key
|
77
|
+
}
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
File without changes
|
File without changes
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Transbank
|
2
|
+
module Webpay
|
3
|
+
module Errors
|
4
|
+
class WebpayError < ::Transbank::Errors::TransbankError
|
5
|
+
attr_accessor :message, :code
|
6
|
+
def initialize(message, code = nil)
|
7
|
+
self.message = message
|
8
|
+
self.code = code
|
9
|
+
super(message)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/transbank-sdk.gemspec
CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
|
|
21
21
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
22
|
spec.require_paths = ["lib"]
|
23
23
|
|
24
|
-
spec.add_development_dependency "bundler", "~> 1.
|
24
|
+
spec.add_development_dependency "bundler", "~> 2.1.0"
|
25
25
|
spec.add_development_dependency "rake", "~> 10.0"
|
26
26
|
spec.add_development_dependency "minitest", "~> 5.0"
|
27
27
|
spec.add_development_dependency "rubocop", "~> 0.59.2"
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: transbank-sdk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Transbank Developers
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-12-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 2.1.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 2.1.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -145,7 +145,9 @@ extra_rdoc_files: []
|
|
145
145
|
files:
|
146
146
|
- ".built"
|
147
147
|
- ".bundled"
|
148
|
+
- ".github/ISSUE_TEMPLATE/reporte-de-error.md"
|
148
149
|
- ".gitignore"
|
150
|
+
- ".rakeTasks"
|
149
151
|
- ".travis.yml"
|
150
152
|
- CHANGELOG.md
|
151
153
|
- Dockerfile
|
@@ -158,6 +160,29 @@ files:
|
|
158
160
|
- bin/setup
|
159
161
|
- docker-compose.yml
|
160
162
|
- lib/transbank/sdk.rb
|
163
|
+
- lib/transbank/sdk/oneclick/errors/oneclick_error.rb
|
164
|
+
- lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_delete_error.rb
|
165
|
+
- lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_finish_error.rb
|
166
|
+
- lib/transbank/sdk/oneclick/inscription/errors/mall_inscription_start_error.rb
|
167
|
+
- lib/transbank/sdk/oneclick/inscription/errors/oneclick_mall_inscription_errors.rb
|
168
|
+
- lib/transbank/sdk/oneclick/inscription/inscription.rb
|
169
|
+
- lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_delete_response.rb
|
170
|
+
- lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_finish_response.rb
|
171
|
+
- lib/transbank/sdk/oneclick/inscription/responses/mall_inscription_start_response.rb
|
172
|
+
- lib/transbank/sdk/oneclick/inscription/responses/oneclick_mall_inscription_responses.rb
|
173
|
+
- lib/transbank/sdk/oneclick/mall/base.rb
|
174
|
+
- lib/transbank/sdk/oneclick/mall/errors/mall_transaction_authorize_error.rb
|
175
|
+
- lib/transbank/sdk/oneclick/mall/errors/mall_transaction_refund_error.rb
|
176
|
+
- lib/transbank/sdk/oneclick/mall/errors/mall_transaction_status_error.rb
|
177
|
+
- lib/transbank/sdk/oneclick/mall/errors/oneclick_mall_errors.rb
|
178
|
+
- lib/transbank/sdk/oneclick/mall/mall_inscription.rb
|
179
|
+
- lib/transbank/sdk/oneclick/mall/mall_transaction.rb
|
180
|
+
- lib/transbank/sdk/oneclick/mall/oneclick_mall.rb
|
181
|
+
- lib/transbank/sdk/oneclick/mall/responses/mall_transaction_authorize_response.rb
|
182
|
+
- lib/transbank/sdk/oneclick/mall/responses/mall_transaction_refund_response.rb
|
183
|
+
- lib/transbank/sdk/oneclick/mall/responses/mall_transaction_status_response.rb
|
184
|
+
- lib/transbank/sdk/oneclick/mall/responses/oneclick_mall_responses.rb
|
185
|
+
- lib/transbank/sdk/oneclick/oneclick.rb
|
161
186
|
- lib/transbank/sdk/onepay/base.rb
|
162
187
|
- lib/transbank/sdk/onepay/errors/errors.rb
|
163
188
|
- lib/transbank/sdk/onepay/errors/integration_type_error.rb
|
@@ -186,12 +211,32 @@ files:
|
|
186
211
|
- lib/transbank/sdk/onepay/responses/responses.rb
|
187
212
|
- lib/transbank/sdk/onepay/responses/transaction_commit_response.rb
|
188
213
|
- lib/transbank/sdk/onepay/responses/transaction_create_response.rb
|
189
|
-
- lib/transbank/sdk/
|
190
|
-
- lib/transbank/sdk/
|
191
|
-
- lib/transbank/sdk/
|
192
|
-
- lib/transbank/sdk/
|
193
|
-
- lib/transbank/sdk/
|
214
|
+
- lib/transbank/sdk/transaccion_completa/base.rb
|
215
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaccion_completa_error.rb
|
216
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaccion_completa_errors.rb
|
217
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaction_commit_error.rb
|
218
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaction_create_error.rb
|
219
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaction_installments_error.rb
|
220
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaction_refund_error.rb
|
221
|
+
- lib/transbank/sdk/transaccion_completa/errors/transaction_status_error.rb
|
222
|
+
- lib/transbank/sdk/transaccion_completa/options.rb
|
223
|
+
- lib/transbank/sdk/transaccion_completa/responses/transaccion_completa_responses.rb
|
224
|
+
- lib/transbank/sdk/transaccion_completa/responses/transaction_commit_response.rb
|
225
|
+
- lib/transbank/sdk/transaccion_completa/responses/transaction_create_response.rb
|
226
|
+
- lib/transbank/sdk/transaccion_completa/responses/transaction_installments_response.rb
|
227
|
+
- lib/transbank/sdk/transaccion_completa/responses/transaction_refund_response.rb
|
228
|
+
- lib/transbank/sdk/transaccion_completa/responses/transaction_status_response.rb
|
229
|
+
- lib/transbank/sdk/transaccion_completa/transaccion_completa.rb
|
230
|
+
- lib/transbank/sdk/transaccion_completa/transaction.rb
|
231
|
+
- lib/transbank/sdk/transbank_error.rb
|
232
|
+
- lib/transbank/sdk/utils/json_utils.rb
|
233
|
+
- lib/transbank/sdk/utils/net_helper.rb
|
234
|
+
- lib/transbank/sdk/utils/request_builder.rb
|
235
|
+
- lib/transbank/sdk/utils/signature_utils.rb
|
236
|
+
- lib/transbank/sdk/utils/utils.rb
|
194
237
|
- lib/transbank/sdk/version.rb
|
238
|
+
- lib/transbank/sdk/webpay/errors/integration_type_error.rb
|
239
|
+
- lib/transbank/sdk/webpay/errors/webpay_error.rb
|
195
240
|
- sdk_test.sh
|
196
241
|
- transbank-sdk.gemspec
|
197
242
|
homepage: https://www.transbankdevelopers.cl/
|
@@ -212,7 +257,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
212
257
|
- !ruby/object:Gem::Version
|
213
258
|
version: '0'
|
214
259
|
requirements: []
|
215
|
-
|
260
|
+
rubyforge_project:
|
261
|
+
rubygems_version: 2.7.7
|
216
262
|
signing_key:
|
217
263
|
specification_version: 4
|
218
264
|
summary: Transbank SDK for Ruby
|
@@ -1,77 +0,0 @@
|
|
1
|
-
module Transbank
|
2
|
-
module Onepay
|
3
|
-
module Utils
|
4
|
-
module JSONUtils
|
5
|
-
def self.included(mod)
|
6
|
-
# Implement #to_h if the class that includes this module doesn't have it
|
7
|
-
# implemented. Used in several model classes to make them easier to
|
8
|
-
# transform to hashes so they can be transformed to JSON afterwards
|
9
|
-
unless mod.respond_to? :to_h
|
10
|
-
mod.send(:define_method, :to_h) do
|
11
|
-
JSON.parse(self.jsonify)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
# Get all instance variables of an instance of a class,
|
16
|
-
# then for all of these variables,
|
17
|
-
# if the instance of the class' respond_to? returns true,
|
18
|
-
# #send the variable name (so, you'll get the value of the instance variable's
|
19
|
-
# getter), then save that to a Hash where [key] is the instance variable's name
|
20
|
-
# and [value] is its value
|
21
|
-
#
|
22
|
-
# Finally, generate a JSON string from this hash
|
23
|
-
# @return [String] a JSON string created from the Hash resulting from the
|
24
|
-
# above operation
|
25
|
-
def jsonify
|
26
|
-
instance_vars = instance_variables.map! { |var| var.to_s.gsub!(/^@/, '') }
|
27
|
-
instance_as_hash =
|
28
|
-
instance_vars.reduce({}) do |resulting_hash, instance_variable|
|
29
|
-
if respond_to? instance_variable
|
30
|
-
value = send(instance_variable)
|
31
|
-
# Safe navigation operator is Ruby 2.3+
|
32
|
-
value = value.to_h if value && value.respond_to?(:to_h) unless value.is_a? Array
|
33
|
-
value = value.to_a if value && value.respond_to?(:to_a) unless value.is_a? Hash
|
34
|
-
if value.is_a? Array
|
35
|
-
value = value.map {|x| x.respond_to?(:jsonify) ? JSON.parse(x.jsonify) : x }
|
36
|
-
end
|
37
|
-
|
38
|
-
value = value.jsonify if value.respond_to? :jsonify
|
39
|
-
resulting_hash[instance_variable] = value
|
40
|
-
end
|
41
|
-
resulting_hash
|
42
|
-
end
|
43
|
-
# Some values can't be null and must be removed if they are
|
44
|
-
instance_as_hash.reject! do |key, value|
|
45
|
-
%w[commerce_logo_url width_height].include?(key) && value.nil?
|
46
|
-
end
|
47
|
-
JSON.generate instance_as_hash
|
48
|
-
end
|
49
|
-
|
50
|
-
# Receive a Hash and return a new hash same as the one we received,
|
51
|
-
# but all keys that were strings or camelCase'd are snake_case'd and
|
52
|
-
# turned into symbols.
|
53
|
-
# Example: {'camelCaseKey': "somevalue"}
|
54
|
-
# Would return: {camel_case_key: "somevalue"}
|
55
|
-
def transform_hash_keys(hash)
|
56
|
-
hash.reduce({}) do |new_hsh, (key, val)|
|
57
|
-
new_key = underscore(key).to_sym
|
58
|
-
new_hsh[new_key] = val
|
59
|
-
new_hsh
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
# FROM https://stackoverflow.com/a/1509957
|
64
|
-
# Transforms camelCaseWords to snake_case_words
|
65
|
-
def underscore(camel_cased_word)
|
66
|
-
camel_cased_word.to_s.gsub(/::/, '/')
|
67
|
-
.gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
|
68
|
-
.gsub(/([a-z\d])([A-Z])/,'\1_\2')
|
69
|
-
.tr("-", "_")
|
70
|
-
.downcase
|
71
|
-
end
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
|
@@ -1,38 +0,0 @@
|
|
1
|
-
module Transbank
|
2
|
-
module Onepay
|
3
|
-
module Utils
|
4
|
-
module NetHelper
|
5
|
-
# POST a request to Transbank's servers, and return the parsed response
|
6
|
-
# @param uri_string [String] an URI to post to
|
7
|
-
# @param body [Hash] the body of your POST request
|
8
|
-
# @return [Hash] the JSON.parse'd response body
|
9
|
-
def http_post(uri_string, body)
|
10
|
-
uri = URI.parse(uri_string)
|
11
|
-
http = Net::HTTP.new(uri.host, uri.port)
|
12
|
-
http.use_ssl = uri.scheme == 'https'
|
13
|
-
request = Net::HTTP::Post.new(uri.path, 'Content-Type'=> 'application/json')
|
14
|
-
camel_cased_body = keys_to_camel_case(body)
|
15
|
-
request.body = JSON.generate(camel_cased_body)
|
16
|
-
result = http.request(request)
|
17
|
-
JSON.parse(result.body)
|
18
|
-
end
|
19
|
-
|
20
|
-
# Required for sending data to Transbank.
|
21
|
-
def keys_to_camel_case(hash)
|
22
|
-
hash.reduce({}) do |new_hash, (key, val)|
|
23
|
-
if val.is_a? Array
|
24
|
-
val = val.map {|value| value.is_a?(Hash) ? keys_to_camel_case(value) : value }
|
25
|
-
end
|
26
|
-
new_key = snake_to_camel_case(key.to_s)
|
27
|
-
new_hash[new_key] = val
|
28
|
-
new_hash
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
def snake_to_camel_case(str)
|
33
|
-
str.split('_').reduce { |string, current_word| string + current_word.capitalize }
|
34
|
-
end
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|