sbrf_merchant 3.0.0 → 3.0.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7e42a848b5a67244c5552ee10258612a6c2be0ce73ad3927051e52c1e8776fe0
4
- data.tar.gz: 5588f6e57acb32f4104660251285a942cf199859374676352f947f29f1b62d03
3
+ metadata.gz: a6a5b6f47d214d86d9060dc1f22a90bb1978869578d5b7260e1512b0046ede11
4
+ data.tar.gz: eff4894c9b2ec93d898d5ee12409c33a58a2ddb46bcb7ce1a0e9d864484fc4b5
5
5
  SHA512:
6
- metadata.gz: 120dfd1f558921d0c8dd6fdf444151301241366eee65734dccf7e8140ea6fc09e6accc7a844c8b6837a8e2d87a1929f97b39d817b9244c97ab702d62e4f8e7bb
7
- data.tar.gz: '098e739143b5da07dd5638300cff26291ca1ffeddcc43670492db091213184dff71d07c8c52570b3f858d31dd4453a6b6389034dd37a04219ce5d800b569a71e'
6
+ metadata.gz: 596032ff87d08f9c8629e888e98af091f193aeeb72531658a64929509229169f3a0f1d975e1b87ba342992da4089d80abbc0d162cebe6aa0c1e00f82ed7a1a1d
7
+ data.tar.gz: 751bba5276208ea197b5a5b86259e4d94b81520f831b83d2155af81d8600ed498a034ac861e7889e42429cd658ed78ab32581d6573a9f7d0f90c0907fe3e6c93
data/README.md CHANGED
@@ -58,6 +58,17 @@ response.success? # => true
58
58
  response.order_id #<order-id>
59
59
 
60
60
  ```
61
+ ### Использование альтернативного http клиента
62
+ По умолчанию гем использует ```Net::Http``` для отправки HTTP запросов. Если в вашем проекте используется другой Http клиент, например [Faraday](https://github.com/lostisland/faraday) то нужно реализовать callable класс или Proc и передать его в конструктор:
63
+ ```ruby
64
+ client = SBRF::Api::Client.new(
65
+ user_name: '<Merchant Username>',
66
+ password: '<Merchant Password>',
67
+ host: '<Sberbank API Host>' # например https://3dsec.sberbank.ru',
68
+ http_client: ->(uri, params) { Faraday.post(uri, params) }
69
+ )
70
+ ```
71
+
61
72
  ## Copyright
62
73
  Copyright (c) 2018 Eugene Kozlov.
63
74
 
@@ -20,10 +20,10 @@ module SbrfMerchant
20
20
  user_name:,
21
21
  password:,
22
22
  host:,
23
- response_body_postprocessor: SbrfMerchant::Api::Response::BodyPostProcessor,
24
- request_body_preprocessor: SbrfMerchant::Api::Request::BodyPreProcessor,
25
- http_client: SbrfMerchant::Utils::Http::Client,
26
- uri_builder: SbrfMerchant::Api::Request::UriBuilder
23
+ response_body_postprocessor: SbrfMerchant::Api::Response::BodyPostProcessor.new,
24
+ request_body_preprocessor: SbrfMerchant::Api::Request::BodyPreProcessor.new,
25
+ http_client: SbrfMerchant::Utils::Http::Client.new,
26
+ uri_builder: SbrfMerchant::Api::Request::UriBuilder.new
27
27
  )
28
28
  @user_name = user_name
29
29
  @password = password
@@ -6,9 +6,11 @@ require 'sbrf_merchant/utils/hash/to_camel_case_keys'
6
6
  module SbrfMerchant
7
7
  module Api
8
8
  module Request
9
- BodyPreProcessor = SbrfMerchant::Utils::HighOrderFunctions::Compose.call(
10
- SbrfMerchant::Utils::Hash::ToCamelCaseKeys
11
- )
9
+ class BodyPreProcessor
10
+ def call(body)
11
+ SbrfMerchant::Utils::Hash::ToCamelCaseKeys.new.call(body)
12
+ end
13
+ end
12
14
  end
13
15
  end
14
16
  end
@@ -6,8 +6,13 @@ require 'sbrf_merchant/utils/string/to_camel_case'
6
6
  module SbrfMerchant
7
7
  module Api
8
8
  module Request
9
- UriBuilderConstructor = lambda do |method_name_converter|
10
- lambda do |host, method_name|
9
+ class UriBuilder
10
+ attr_reader :method_name_converter
11
+ def initialize(method_name_converter: SbrfMerchant::Utils::String::ToCamelCase.new)
12
+ @method_name_converter = method_name_converter
13
+ end
14
+
15
+ def call(host, method_name)
11
16
  URI.join(
12
17
  host,
13
18
  '/payment/rest/',
@@ -15,8 +20,6 @@ module SbrfMerchant
15
20
  )
16
21
  end
17
22
  end
18
-
19
- UriBuilder = UriBuilderConstructor.call(SbrfMerchant::Utils::String::ToCamelCase)
20
23
  end
21
24
  end
22
25
  end
@@ -1,16 +1,21 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'ostruct'
4
-
5
3
  module SbrfMerchant
6
4
  module Api
7
5
  module Response
8
- AppendSuccessFlagToHash = lambda do |hash|
9
- hash.merge(success?: IsSuccess.call(hash))
10
- end
6
+ class AppendSuccessFlagToHash
7
+ def call(hash)
8
+ hash.merge('success?' => success?(hash))
9
+ end
10
+
11
+ private
11
12
 
12
- IsSuccess = lambda do |hash|
13
- !hash[:form_url].nil? || hash[:error_code] == '0'
13
+ # Ответ считается успешным когда errorCode == '0' но есть ньюанс,
14
+ # В успешном ответе на запрос register errorCode отсутствует,
15
+ # поэтому проверяем по параметру formUrl
16
+ def success?(hash)
17
+ !hash['formUrl'].nil? || hash['errorCode'] == '0'
18
+ end
14
19
  end
15
20
  end
16
21
  end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sbrf_merchant/utils/string/to_camel_case'
4
+
5
+ module SbrfMerchant
6
+ module Api
7
+ module Response
8
+ # Проблема - API сбербанка возвращает ответ в формате camelCase,
9
+ # что не очень красиво выглядит в руби коде
10
+
11
+ # Решение - декоратор для хэша на котором будут вызываться методы в snake_case,
12
+ class BodyDecorator
13
+ attr_reader :hash, :to_camel_case
14
+
15
+ def initialize(hash, to_camel_case = ::SBRF::Utils::String::ToCamelCase.new)
16
+ @hash = hash
17
+ @to_camel_case = to_camel_case
18
+ end
19
+
20
+ def method_missing(meth, *args)
21
+ value = hash[key(meth)]
22
+
23
+ # Если значение не найдено то поведение по умолчанию
24
+ return super if value.nil?
25
+
26
+ # Если значение - хэш то возвращаем его, предварительно обернув в декоратор
27
+ return self.class.new(value) if value.is_a?(Hash)
28
+
29
+ # В остальных случаях возвращаем самое значение
30
+ value
31
+ end
32
+
33
+ def respond_to?(meth)
34
+ hash.key?(key(meth))
35
+ end
36
+
37
+ def respond_to_missing?(meth)
38
+ hash.key?(key(meth))
39
+ end
40
+
41
+ private
42
+
43
+ def key(meth)
44
+ to_camel_case.call(meth.to_s)
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
@@ -2,21 +2,21 @@
2
2
 
3
3
  require 'sbrf_merchant/utils/high_order_functions/compose'
4
4
  require 'sbrf_merchant/utils/json/to_hash_parser'
5
- require 'sbrf_merchant/utils/json/to_object_parser'
6
- require 'sbrf_merchant/utils/hash/to_snake_case_keys'
7
5
  require 'sbrf_merchant/api/response/append_success_flag_to_hash'
8
- require 'json/ext'
6
+ require 'sbrf_merchant/api/response/body_decorator'
9
7
 
10
8
  module SbrfMerchant
11
9
  module Api
12
10
  module Response
13
- BodyPostProcessor = SbrfMerchant::Utils::HighOrderFunctions::Compose.call(
14
- SbrfMerchant::Utils::JSON::ToHashParser,
15
- SbrfMerchant::Utils::Hash::ToSnakeCaseKeys,
16
- SbrfMerchant::Api::Response::AppendSuccessFlagToHash,
17
- ->(hash) { JSON.dump(hash) },
18
- SbrfMerchant::Utils::JSON::ToObjectParser.call(OpenStruct)
19
- )
11
+ class BodyPostProcessor
12
+ def call(body)
13
+ ::SbrfMerchant::Utils::HighOrderFunctions::Compose.new(
14
+ ::SbrfMerchant::Utils::JSON::ToHashParser.new,
15
+ ::SbrfMerchant::Api::Response::AppendSuccessFlagToHash.new,
16
+ ->(hash) { ::SbrfMerchant::Api::Response::BodyDecorator.new(hash) }
17
+ ).call(body)
18
+ end
19
+ end
20
20
  end
21
21
  end
22
22
  end
@@ -5,7 +5,11 @@ require 'awrence'
5
5
  module SbrfMerchant
6
6
  module Utils
7
7
  module Hash
8
- ToCamelCaseKeys = ->(hash) { hash.to_camelback_keys }
8
+ class ToCamelCaseKeys
9
+ def call(hash)
10
+ hash.to_camelback_keys
11
+ end
12
+ end
9
13
  end
10
14
  end
11
15
  end
@@ -3,11 +3,18 @@
3
3
  module SbrfMerchant
4
4
  module Utils
5
5
  module HighOrderFunctions
6
- Compose = lambda { |*functions|
7
- lambda { |arg|
8
- functions.inject(arg) { |acc, function| function.call(acc) }
9
- }
10
- }
6
+ # Обьединяем несколько callable сущностей в одну
7
+ class Compose
8
+ attr_reader :functions
9
+
10
+ def initialize(*functions)
11
+ @functions = functions
12
+ end
13
+
14
+ def call(arg)
15
+ functions.inject(arg) { |acc, func| func.call(acc) }
16
+ end
17
+ end
11
18
  end
12
19
  end
13
20
  end
@@ -5,12 +5,14 @@ require 'net/http'
5
5
  module SbrfMerchant
6
6
  module Utils
7
7
  module Http
8
- Client = lambda do |uri, params|
9
- request = ::Net::HTTP::Post.new(uri)
10
- request.set_form_data(params)
8
+ class Client
9
+ def call(uri, params)
10
+ request = ::Net::HTTP::Post.new(uri)
11
+ request.set_form_data(params)
11
12
 
12
- ::Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
13
- http.request(request)
13
+ ::Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
14
+ http.request(request)
15
+ end
14
16
  end
15
17
  end
16
18
  end
@@ -5,7 +5,11 @@ require 'json/ext'
5
5
  module SbrfMerchant
6
6
  module Utils
7
7
  module JSON
8
- ToHashParser = ->(json) { ::JSON.parse(json, symbolize_names: true) }
8
+ class ToHashParser
9
+ def call(json)
10
+ ::JSON.parse(json)
11
+ end
12
+ end
9
13
  end
10
14
  end
11
15
  end
@@ -3,9 +3,11 @@
3
3
  module SbrfMerchant
4
4
  module Utils
5
5
  module String
6
- ToCamelCase = lambda do |str|
7
- str.split('_').inject('') do |buffer, e|
8
- buffer + (buffer.empty? ? e : e.capitalize)
6
+ class ToCamelCase
7
+ def call(str)
8
+ str.split('_').inject('') do |buffer, e|
9
+ buffer + (buffer.empty? ? e : e.capitalize)
10
+ end
9
11
  end
10
12
  end
11
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SbrfMerchant
4
- VERSION = '3.0.0'
4
+ VERSION = '3.0.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sbrf_merchant
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Kozlov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-10-23 00:00:00.000000000 Z
11
+ date: 2019-11-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,20 +122,6 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- - !ruby/object:Gem::Dependency
126
- name: plissken
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :runtime
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
125
  description: Sberbank Merchant API Client for Ruby
140
126
  email:
141
127
  - kozlovea8@gmail.com
@@ -151,13 +137,12 @@ files:
151
137
  - lib/sbrf_merchant/api/request/body_preprocessor.rb
152
138
  - lib/sbrf_merchant/api/request/uri_builder.rb
153
139
  - lib/sbrf_merchant/api/response/append_success_flag_to_hash.rb
140
+ - lib/sbrf_merchant/api/response/body_decorator.rb
154
141
  - lib/sbrf_merchant/api/response/body_postprocessor.rb
155
142
  - lib/sbrf_merchant/utils/hash/to_camel_case_keys.rb
156
- - lib/sbrf_merchant/utils/hash/to_snake_case_keys.rb
157
143
  - lib/sbrf_merchant/utils/high_order_functions/compose.rb
158
144
  - lib/sbrf_merchant/utils/http/client.rb
159
145
  - lib/sbrf_merchant/utils/json/to_hash_parser.rb
160
- - lib/sbrf_merchant/utils/json/to_object_parser.rb
161
146
  - lib/sbrf_merchant/utils/string/to_camel_case.rb
162
147
  - lib/sbrf_merchant/version.rb
163
148
  homepage: https://github.com/abstractart/sbrf_merchant
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'plissken'
4
-
5
- module SbrfMerchant
6
- module Utils
7
- module Hash
8
- ToSnakeCaseKeys = ->(hash) { hash.to_snake_keys }
9
- end
10
- end
11
- end
@@ -1,15 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json/ext'
4
-
5
- module SbrfMerchant
6
- module Utils
7
- module JSON
8
- ToObjectParser = lambda { |object_class|
9
- lambda { |json|
10
- ::JSON.parse(json, object_class: object_class)
11
- }
12
- }
13
- end
14
- end
15
- end