atol 0.3 → 0.4

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
  SHA1:
3
- metadata.gz: 2b0571eec0780fb18eafe60b99e4c5786c8a3166
4
- data.tar.gz: f1caad94cb03daed307d5c24f96665a157a5e82b
3
+ metadata.gz: 43777b45ecaae54c9ad99454f9281423d8793cbc
4
+ data.tar.gz: 867efae667bc6acc6e601e651711eaf8c1ba56e9
5
5
  SHA512:
6
- metadata.gz: fb3aaa2101bf30f554c8216b439c675c584e91ec6649010544ae450a62dd601abfd1c89009709e542f4220548a297c89416b2ca33a1203a318fdb688e66c15f9
7
- data.tar.gz: 8ce89512fa8ffb53d36cd69d32ca1c558a38ae5b7c6fbd3ec04ed3e36a11b7382355adb30f799de549aefab271a2292380836739f4c226830f9a994f934f0b8d
6
+ metadata.gz: 608913a36f029a07911c48003a7191bfa34de6c37942559605155b3e503add8a6a4113995af0208a9ec7a05943cd1c1a0d0105dd067698aec9fdc2f380357d4d
7
+ data.tar.gz: 849b133128f46db62b5b1707a5986bd90fe2346d6f017cb5716e1fd478b4df1df34523723a366e7b24613b29ec375943a358618ba611402b322371821ca02ab2
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- atol (0.3)
4
+ atol (0.4)
5
5
  anyway_config (~> 1.0)
6
6
 
7
7
  GEM
@@ -80,4 +80,4 @@ DEPENDENCIES
80
80
  webmock (~> 3.0)
81
81
 
82
82
  BUNDLED WITH
83
- 1.16.1
83
+ 1.16.4
data/README.md CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  # atol-rb
7
7
 
8
- Пакет содержит набор классов для работы с [KaaS-сервисом АТОЛ-онлайн](https://online.atol.ru/) по [описанному протоколу](https://online.atol.ru/files/%D0%90%D0%A2%D0%9E%D0%9B%20%D0%9E%D0%BD%D0%BB%D0%B0%D0%B8%CC%86%D0%BD._%D0%9E%D0%BF%D0%B8%D1%81%D0%B0%D0%BD%D0%B8%D0%B5%20%D0%BF%D1%80%D0%BE%D1%82%D0%BE%D0%BA%D0%BE%D0%BB%D0%B0.pdf).
8
+ Пакет содержит набор классов для работы с [KaaS-сервисом АТОЛ-онлайн](https://online.atol.ru/) по [описанному протоколу](https://online.atol.ru/files/ffd/API_AO_v4.pdf).
9
9
 
10
10
  ##### Совместимость
11
11
 
12
- Для корректной работы необходим интерпретатор Руби версии 2.2 и выше. Пакет работает с версией протокола v3.
12
+ Для корректной работы необходим интерпретатор Руби версии 2.2 и выше. Пакет работает с версией протокола v4.
13
13
 
14
14
 
15
15
  ## Использование
@@ -9,22 +9,17 @@ module Atol
9
9
  @config = config || Atol.config
10
10
  raise(Atol::ConfigExpectedError) unless @config.is_a?(Atol::Config)
11
11
  raise(Atol::MissingConfigError, 'group_code missing') if @config.group_code.nil?
12
-
13
- @url = "#{Atol::URL}/#{@config.group_code}/report/#{uuid}?tokenid=#{token}"
14
- @http_client = @config.http_client
12
+ @token = token
13
+ @uuid = uuid
15
14
  end
16
15
 
17
16
  def call
18
- uri = URI(url)
19
-
20
- http = @http_client.new(uri.host, uri.port)
17
+ http_client = @config.http_client
18
+ uri = URI("#{Atol::URL}/#{@config.group_code}/report/#{@uuid}")
19
+ http = http_client.new(uri.host, uri.port)
21
20
  http.use_ssl = true
22
- http.get(uri.request_uri)
21
+ http.get(uri.request_uri, 'Token' => @token)
23
22
  end
24
-
25
- private
26
-
27
- attr_reader :url
28
23
  end
29
24
  end
30
25
  end
@@ -27,16 +27,12 @@ module Atol
27
27
 
28
28
  def call
29
29
  uri = URI(Atol::URL + PATH)
30
- uri.query = URI.encode_www_form(login: login, pass: password)
30
+ uri.query = URI.encode_www_form(login: @login, pass: @password)
31
31
 
32
32
  http = @http_client.new(uri.host, uri.port)
33
33
  http.use_ssl = true
34
34
  http.get(uri.request_uri)
35
35
  end
36
-
37
- private
38
-
39
- attr_reader :login, :password
40
36
  end
41
37
  end
42
38
  end
@@ -6,20 +6,32 @@ module Atol
6
6
  module Item
7
7
  class Body
8
8
  def initialize(name:, price:, quantity: 1, config: nil)
9
- @config = config || Atol.config
10
-
11
9
  raise Atol::ZeroItemQuantityError if quantity.to_i.zero?
12
10
 
13
- @body = Hash[]
14
- @body[:name] = name
15
- @body[:price] = price.to_f
16
- @body[:quantity] = quantity.to_f
17
- @body[:sum] = (@body[:price] * @body[:quantity]).to_f.round(2)
18
- @body[:tax] = @config.default_tax
11
+ @config = config || Atol.config
12
+ @name = name
13
+ @price = price.to_f
14
+ @quantity = quantity.to_f
19
15
  end
20
16
 
21
17
  def to_h
22
- @body.clone
18
+ body.clone
19
+ end
20
+
21
+ def to_json
22
+ body.to_json
23
+ end
24
+
25
+ private
26
+
27
+ def body
28
+ @body ||= {
29
+ name: @name,
30
+ price: @price,
31
+ quantity: @quantity,
32
+ sum: (@price * @quantity).round(2),
33
+ tax: @config.default_tax
34
+ }
23
35
  end
24
36
  end
25
37
  end
@@ -8,20 +8,12 @@ module Atol
8
8
  def initialize(external_id:, phone: '', email: '', items:, config: nil)
9
9
  raise(Atol::EmptyClientContactError) if phone.empty? && email.empty?
10
10
  raise(Atol::EmptySellItemsError) if items.empty?
11
- @config = config || Atol.config
12
-
13
- @body = body_template
14
- @body[:external_id] = external_id
15
- @body[:receipt][:attributes][:email] = email unless email.empty?
16
- @body[:receipt][:attributes][:phone] = phone unless phone.empty?
17
- @body[:service][:callback_url] = @config.callback_url if @config.callback_url
18
11
 
19
- total = items.inject(0) do |sum, item|
20
- sum += item[:sum]
21
- end
22
-
23
- @body[:receipt][:total] = @body[:receipt][:payments][0][:sum] = total
24
- @body[:receipt][:items] = items
12
+ @config = config || Atol.config
13
+ @external_id = external_id
14
+ @phone = phone
15
+ @email = email
16
+ @items = items
25
17
  end
26
18
 
27
19
  def to_h
@@ -34,28 +26,41 @@ module Atol
34
26
 
35
27
  private
36
28
 
37
- attr_reader :config, :body
29
+ def body
30
+ @body ||= body_template.tap do |result|
31
+ result[:external_id] = @external_id
32
+ result[:receipt][:attributes][:email] = @email unless @email.empty?
33
+ result[:receipt][:attributes][:phone] = @phone unless @phone.empty?
34
+ result[:service][:callback_url] = @config.callback_url if @config.callback_url
35
+
36
+ total = @items.sum(0) { |item| item[:sum] }
37
+
38
+ result[:receipt][:total] = total
39
+ result[:receipt][:payments][0][:sum] = total
40
+ result[:receipt][:items] = @items
41
+ end
42
+ end
38
43
 
39
44
  def body_template
40
- Hash[
45
+ {
41
46
  receipt: {
42
47
  attributes: {
43
- sno: config.default_sno
48
+ sno: @config.default_sno
44
49
  },
45
50
  items: [],
46
51
  payments: [
47
52
  {
48
53
  sum: 0,
49
- type: config.default_payment_type
54
+ type: @config.default_payment_type
50
55
  }
51
56
  ]
52
57
  },
53
58
  service: {
54
- inn: config.inn.to_s,
55
- payment_address: config.payment_address
59
+ inn: @config.inn.to_s,
60
+ payment_address: @config.payment_address
56
61
  },
57
62
  timestamp: Time.now.strftime(Atol::TIMESTAMP_FORMAT)
58
- ]
63
+ }
59
64
  end
60
65
  end
61
66
  end
@@ -7,7 +7,7 @@ module Atol
7
7
  module Request
8
8
  class PostDocument
9
9
  OPERATIONS = %i[sell sell_refund sell_correction buy buy_refund buy_correction].freeze
10
- HEADERS = { 'Content-Type' => 'application/json' }.freeze
10
+ HEADERS = { 'Content-Type' => 'application/json; charset=utf-8' }.freeze
11
11
 
12
12
  def initialize(operation:, token:, body:, config: nil, req_logger: nil, res_logger: nil)
13
13
  @config = config || Atol.config
@@ -16,36 +16,30 @@ module Atol
16
16
  raise(Atol::MissingConfigError, 'group_code missing') if @config.group_code.empty?
17
17
  raise(Atol::UnknownOperationError, operation.to_s) unless OPERATIONS.include?(operation.to_sym)
18
18
 
19
- @url = "#{Atol::URL}/#{@config.group_code}/#{operation}?tokenid=#{token}"
19
+ @operation = operation
20
+ @token = token
20
21
  @body = body
21
22
  @req_logger = req_logger
22
23
  @res_logger = res_logger
23
- @http_client = @config.http_client
24
24
  end
25
25
 
26
26
  def call
27
- uri = URI(url)
28
- req = @http_client::Post.new(uri, HEADERS)
29
- req.body = body
27
+ http_client = @config.http_client
28
+ uri = URI("#{Atol::URL}/#{@config.group_code}/#{@operation}")
29
+ req_headers = HEADERS.merge(Token: @token)
30
+ req = http_client::Post.new(uri, req_headers)
31
+ req.body = @body
30
32
 
31
- if req_logger.respond_to?(:call)
32
- req_logger.call(req)
33
- end
33
+ @req_logger.call(req) if @req_logger.respond_to?(:call)
34
34
 
35
- res = @http_client.start(uri.hostname, uri.port, use_ssl: true) do |http|
35
+ res = http_client.start(uri.hostname, uri.port, use_ssl: true) do |http|
36
36
  http.request(req)
37
37
  end
38
-
39
- if res_logger.respond_to?(:call)
40
- res_logger.call(res)
41
- end
38
+
39
+ @res_logger.call(res) if @res_logger.respond_to?(:call)
42
40
 
43
41
  res
44
42
  end
45
-
46
- private
47
-
48
- attr_reader :url, :body, :req_logger, :res_logger
49
43
  end
50
44
  end
51
45
  end
@@ -7,11 +7,11 @@ module Atol
7
7
  def initialize(uuid:, token:, config: nil)
8
8
  @config = config || Atol.config
9
9
  raise(Atol::ConfigExpectedError) unless @config.is_a?(Atol::Config)
10
- @params = Hash[uuid: uuid, token: token, config: @config]
10
+ @params = { uuid: uuid, token: token, config: @config }
11
11
  end
12
12
 
13
13
  def call
14
- request = Atol::Request::GetDocumentState.new(params)
14
+ request = Atol::Request::GetDocumentState.new(@params)
15
15
  response = request.call
16
16
  encoded_body = response.body.force_encoding(Atol::ENCODING)
17
17
  json = JSON.parse(encoded_body)
@@ -24,10 +24,6 @@ module Atol
24
24
  raise(encoded_body)
25
25
  end
26
26
  end
27
-
28
- private
29
-
30
- attr_reader :params
31
27
  end
32
28
  end
33
29
  end
@@ -13,6 +13,7 @@ module Atol
13
13
  @config.req_tries_number.times do |i|
14
14
  request = Atol::Request::GetToken.new(config: @config)
15
15
  response = request.call
16
+ byebug
16
17
  encoded_body = response.body.encode(Atol::ENCODING)
17
18
  json = JSON.parse(encoded_body)
18
19
 
@@ -7,18 +7,18 @@ module Atol
7
7
  def initialize(operation:, token:, body:, config: nil, req_logger: nil, res_logger: nil)
8
8
  @config = config || Atol.config
9
9
  raise(Atol::ConfigExpectedError) unless @config.is_a?(Atol::Config)
10
- @params = Hash[
10
+ @params = {
11
11
  operation: operation,
12
12
  token: token,
13
13
  body: body,
14
14
  config: config,
15
15
  req_logger: req_logger,
16
16
  res_logger: res_logger
17
- ]
17
+ }
18
18
  end
19
19
 
20
20
  def call
21
- request = Atol::Request::PostDocument.new(params)
21
+ request = Atol::Request::PostDocument.new(@params)
22
22
  response = request.call
23
23
  encoded_body = response.body.force_encoding(Atol::ENCODING)
24
24
  json = JSON.parse(encoded_body)
@@ -31,10 +31,6 @@ module Atol
31
31
  raise(encoded_body)
32
32
  end
33
33
  end
34
-
35
- private
36
-
37
- attr_reader :params
38
34
  end
39
35
  end
40
36
  end
data/lib/atol/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  module Atol
2
2
  module Version
3
- API = 'v3'.freeze
4
- LIB = '0.3'.freeze
3
+ API = 'v4'.freeze
4
+ LIB = '0.4'.freeze
5
5
  end
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: atol
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: '0.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - GeorgeGorbanev
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-04-20 00:00:00.000000000 Z
11
+ date: 2018-12-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: anyway_config