sbrf_merchant 3.0.1 → 3.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a6a5b6f47d214d86d9060dc1f22a90bb1978869578d5b7260e1512b0046ede11
4
- data.tar.gz: eff4894c9b2ec93d898d5ee12409c33a58a2ddb46bcb7ce1a0e9d864484fc4b5
3
+ metadata.gz: 0e29ff9dfedf968e73a6318f1cc014e9a106838f0695a86a99ec8aa2519c8e8a
4
+ data.tar.gz: bbef1d87432e6f353ee5cab58934db1f42c3b49a73557ae09a49b01f11738d54
5
5
  SHA512:
6
- metadata.gz: 596032ff87d08f9c8629e888e98af091f193aeeb72531658a64929509229169f3a0f1d975e1b87ba342992da4089d80abbc0d162cebe6aa0c1e00f82ed7a1a1d
7
- data.tar.gz: 751bba5276208ea197b5a5b86259e4d94b81520f831b83d2155af81d8600ed498a034ac861e7889e42429cd658ed78ab32581d6573a9f7d0f90c0907fe3e6c93
6
+ metadata.gz: 15e0ab7663e5695859e6af9ee159cb5e3dcfb70fbae6fd123e07a1a6916ad4cca05073e6e1a6aaa6629de1ed3bfdd200d161f0a0deef146966f3467cefdcc255
7
+ data.tar.gz: aa8e6a4b467ba45e06f09dd0fbcb0954aa8694fdb8f553b0d004482b14bfed9038b7a2faa0dcf7bfbfc019807f2629a7275d7261ffec373194730d567e0ff0b2
data/README.md CHANGED
@@ -11,28 +11,36 @@ Ruby клиент для работы с платёжным шлюзом Сбе
11
11
  Ключевые особенности:
12
12
  - Простой (~200 LOC).
13
13
  - Расширяемый из коробки. Если В API появится новый метод то данный клиент будет его поддерживать без каких либо правок в исходный код
14
- - Написан в ОО + ФП стиле без "магии". Легко встроится в ваши сервисные объекты и юнит тесты
14
+ - Написан в ОО стиле без "магии". Легко встроится в ваши сервисные объекты и юнит тесты
15
+
16
+ # Содержание
17
+ - [Установка](#установка)
18
+ - [Документация](#документация)
19
+ - [Примеры использования](#примеры-использования)
20
+ - [Регистрации заказа](#регистрация-заказа)
21
+ - [Замена HTTP клиента](#замена-http-клиента)
22
+ - [Замена JSON парсера](#замена-json-парсера)
15
23
 
16
24
  ## Установка
17
25
 
18
- ### RubyGems.org ###
26
+ ### RubyGems.org
19
27
 
20
28
  ```sh
21
29
  % gem install sbrf_merchant
22
30
  ```
23
31
 
24
- ### Bundler ###
32
+ ### Bundler
25
33
 
26
34
  ```ruby
27
35
  # Gemfile
28
36
  gem 'sbrf_merchant'
29
37
  ```
30
38
  ## Документация
31
- - [Site(RU)](https://securepayments.sberbank.ru/wiki/doku.php/integration:api:start)
39
+ - [WEB(RU)](https://securepayments.sberbank.ru/wiki/doku.php/integration:api:start)
32
40
  - [PDF(RU)](http://cs.petrsu.ru/~vadim/sd2018/Merchant-Manual-SBRF.pdf)
33
41
 
34
- ## Использование
35
- ### Пример регистрации заказа
42
+ ## Примеры использования
43
+ ### Регистрация заказа
36
44
  ```ruby
37
45
  # Cоздаем клиент
38
46
  client = SBRF::Api::Client.new(
@@ -51,14 +59,14 @@ params = {
51
59
  order_number: "something_unique_string",
52
60
  return_url: 'localhost:3000'
53
61
  }
54
- response = client.call(:register, params)
62
+ response = client.call(:register, **params)
55
63
 
56
64
  response.success? # => true
57
65
  # В ответе доступ к атрибутам в snake_case
58
66
  response.order_id #<order-id>
59
67
 
60
68
  ```
61
- ### Использование альтернативного http клиента
69
+ ### Замена HTTP клиента
62
70
  По умолчанию гем использует ```Net::Http``` для отправки HTTP запросов. Если в вашем проекте используется другой Http клиент, например [Faraday](https://github.com/lostisland/faraday) то нужно реализовать callable класс или Proc и передать его в конструктор:
63
71
  ```ruby
64
72
  client = SBRF::Api::Client.new(
@@ -66,10 +74,18 @@ client = SBRF::Api::Client.new(
66
74
  password: '<Merchant Password>',
67
75
  host: '<Sberbank API Host>' # например https://3dsec.sberbank.ru',
68
76
  http_client: ->(uri, params) { Faraday.post(uri, params) }
69
- )
77
+ )
70
78
  ```
71
79
 
80
+ ### Замена JSON парсера
81
+ По умолчанию гем использует класс```JSON``` для парсинга JSON строк. Если в вашем проекте используется другой парсер, например [Oj](https://github.com/ohler55/oj) то нужно реализовать callable класс или Proc и передать его в конструктор:
82
+ ```ruby
83
+ client = SBRF::Api::Client.new(
84
+ user_name: '<Merchant Username>',
85
+ password: '<Merchant Password>',
86
+ host: '<Sberbank API Host>' # например https://3dsec.sberbank.ru',
87
+ json_parser: ->(json) { Oj.load(json, symbol_keys: true) }
88
+ )
89
+ ```
72
90
  ## Copyright
73
- Copyright (c) 2018 Eugene Kozlov.
74
-
75
- [license]: LICENSE.md
91
+ Copyright (c) 2019 Eugene Kozlov.
@@ -3,6 +3,7 @@
3
3
  require 'sbrf_merchant/api/response/body_postprocessor'
4
4
  require 'sbrf_merchant/api/request/body_preprocessor'
5
5
  require 'sbrf_merchant/api/request/uri_builder'
6
+ require 'sbrf_merchant/utils/json/to_hash_parser'
6
7
  require 'sbrf_merchant/utils/http/client'
7
8
 
8
9
  module SbrfMerchant
@@ -14,16 +15,18 @@ module SbrfMerchant
14
15
  :response_body_postprocessor,
15
16
  :http_client,
16
17
  :uri_builder,
17
- :request_body_preprocessor
18
+ :request_body_preprocessor,
19
+ :json_parser
18
20
 
19
21
  def initialize(
20
22
  user_name:,
21
23
  password:,
22
24
  host:,
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
25
+ response_body_postprocessor: SBRF::Api::Response::BodyPostProcessor.new,
26
+ request_body_preprocessor: SBRF::Api::Request::BodyPreProcessor.new,
27
+ http_client: SBRF::Utils::Http::Client.new,
28
+ uri_builder: SBRF::Api::Request::UriBuilder.new,
29
+ json_parser: SBRF::Utils::JSON::ToHashParser.new
27
30
  )
28
31
  @user_name = user_name
29
32
  @password = password
@@ -32,14 +35,16 @@ module SbrfMerchant
32
35
  @http_client = http_client
33
36
  @uri_builder = uri_builder
34
37
  @request_body_preprocessor = request_body_preprocessor
38
+ @json_parser = json_parser
35
39
  end
36
40
 
37
41
  def call(method_name, **params)
38
42
  prepared_params = request_body_preprocessor.call(auth_params.merge(params))
39
43
  uri = uri_builder.call(host, method_name)
40
-
41
44
  response = http_client.call(uri, prepared_params)
42
- response_body_postprocessor.call(response.body)
45
+ parsed_body = json_parser.call(response.body)
46
+
47
+ response_body_postprocessor.call(parsed_body)
43
48
  end
44
49
 
45
50
  private
@@ -1,6 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sbrf_merchant/utils/high_order_functions/compose'
4
3
  require 'sbrf_merchant/utils/hash/to_camel_case_keys'
5
4
 
6
5
  module SbrfMerchant
@@ -5,7 +5,7 @@ module SbrfMerchant
5
5
  module Response
6
6
  class AppendSuccessFlagToHash
7
7
  def call(hash)
8
- hash.merge('success?' => success?(hash))
8
+ hash.merge(success?: success?(hash))
9
9
  end
10
10
 
11
11
  private
@@ -14,7 +14,7 @@ module SbrfMerchant
14
14
  # В успешном ответе на запрос register errorCode отсутствует,
15
15
  # поэтому проверяем по параметру formUrl
16
16
  def success?(hash)
17
- !hash['formUrl'].nil? || hash['errorCode'] == '0'
17
+ !hash[:form_url].nil? || hash[:error_code] == '0'
18
18
  end
19
19
  end
20
20
  end
@@ -5,24 +5,18 @@ require 'sbrf_merchant/utils/string/to_camel_case'
5
5
  module SbrfMerchant
6
6
  module Api
7
7
  module Response
8
- # Проблема - API сбербанка возвращает ответ в формате camelCase,
9
- # что не очень красиво выглядит в руби коде
10
-
11
- # Решение - декоратор для хэша на котором будут вызываться методы в snake_case,
12
8
  class BodyDecorator
13
- attr_reader :hash, :to_camel_case
9
+ attr_reader :hash
14
10
 
15
- def initialize(hash, to_camel_case = ::SBRF::Utils::String::ToCamelCase.new)
11
+ def initialize(hash)
16
12
  @hash = hash
17
- @to_camel_case = to_camel_case
18
13
  end
19
14
 
20
15
  def method_missing(meth, *args)
21
- value = hash[key(meth)]
22
-
23
- # Если значение не найдено то поведение по умолчанию
24
- return super if value.nil?
16
+ # Если ключ отсутствует то поведение по умолчанию
17
+ return super unless hash.key?(meth)
25
18
 
19
+ value = hash[meth]
26
20
  # Если значение - хэш то возвращаем его, предварительно обернув в декоратор
27
21
  return self.class.new(value) if value.is_a?(Hash)
28
22
 
@@ -31,17 +25,11 @@ module SbrfMerchant
31
25
  end
32
26
 
33
27
  def respond_to?(meth)
34
- hash.key?(key(meth))
28
+ hash.key?(meth)
35
29
  end
36
30
 
37
31
  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)
32
+ hash.key?(meth)
45
33
  end
46
34
  end
47
35
  end
@@ -1,20 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'sbrf_merchant/utils/high_order_functions/compose'
4
- require 'sbrf_merchant/utils/json/to_hash_parser'
5
3
  require 'sbrf_merchant/api/response/append_success_flag_to_hash'
6
4
  require 'sbrf_merchant/api/response/body_decorator'
5
+ require 'sbrf_merchant/utils/hash/to_snake_case_keys'
7
6
 
8
7
  module SbrfMerchant
9
8
  module Api
10
9
  module Response
11
10
  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)
11
+ def call(hash)
12
+ result = ::SbrfMerchant::Utils::Hash::ToSnakeCaseKeys.new.call(hash)
13
+ result = ::SbrfMerchant::Api::Response::AppendSuccessFlagToHash.new.call(result)
14
+ result = ::SbrfMerchant::Api::Response::BodyDecorator.new(result)
15
+
16
+ result
18
17
  end
19
18
  end
20
19
  end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'plissken'
4
+
5
+ module SbrfMerchant
6
+ module Utils
7
+ module Hash
8
+ class ToSnakeCaseKeys
9
+ def call(hash)
10
+ hash.to_snake_keys
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,13 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'json/ext'
3
+ require 'json'
4
4
 
5
5
  module SbrfMerchant
6
6
  module Utils
7
7
  module JSON
8
8
  class ToHashParser
9
9
  def call(json)
10
- ::JSON.parse(json)
10
+ ::JSON.parse(json, symbolize_names: true)
11
11
  end
12
12
  end
13
13
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SbrfMerchant
4
- VERSION = '3.0.1'
4
+ VERSION = '3.1.0'
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.1
4
+ version: 3.1.0
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-11-24 00:00:00.000000000 Z
11
+ date: 2020-01-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -109,7 +109,7 @@ dependencies:
109
109
  - !ruby/object:Gem::Version
110
110
  version: '0'
111
111
  - !ruby/object:Gem::Dependency
112
- name: json
112
+ name: plissken
113
113
  requirement: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - ">="
@@ -122,7 +122,7 @@ dependencies:
122
122
  - - ">="
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
- description: Sberbank Merchant API Client for Ruby
125
+ description: Simple and flexible Sberbank acquiring API client for Ruby
126
126
  email:
127
127
  - kozlovea8@gmail.com
128
128
  executables: []
@@ -140,7 +140,7 @@ files:
140
140
  - lib/sbrf_merchant/api/response/body_decorator.rb
141
141
  - lib/sbrf_merchant/api/response/body_postprocessor.rb
142
142
  - lib/sbrf_merchant/utils/hash/to_camel_case_keys.rb
143
- - lib/sbrf_merchant/utils/high_order_functions/compose.rb
143
+ - lib/sbrf_merchant/utils/hash/to_snake_case_keys.rb
144
144
  - lib/sbrf_merchant/utils/http/client.rb
145
145
  - lib/sbrf_merchant/utils/json/to_hash_parser.rb
146
146
  - lib/sbrf_merchant/utils/string/to_camel_case.rb
@@ -1,20 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module SbrfMerchant
4
- module Utils
5
- module HighOrderFunctions
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
18
- end
19
- end
20
- end