diff --git a/lib/atol/errors.rb b/lib/atol/errors.rb index fcfaa26..d93df40 100644 --- a/lib/atol/errors.rb +++ b/lib/atol/errors.rb @@ -3,4 +3,5 @@ class MissingConfigError < StandardError; end class AuthBadRequestError < StandardError; end class AuthUserOrPasswordError < StandardError; end class ConfigExpectedError < StandardError; end + class UnknownOperationError < StandardError; end end diff --git a/lib/atol/request/post_document.rb b/lib/atol/request/post_document.rb new file mode 100644 index 0000000..5538f6f --- /dev/null +++ b/lib/atol/request/post_document.rb @@ -0,0 +1,46 @@ +require 'atol' +require 'atol/errors' +require 'net/http' + +module Atol + module Request + class PostDocument + OPERATIONS = %i[sell sell_refund sell_correction buy buy_refund buy_correction].freeze + HEADERS = { 'Content-Type' => 'application/json' }.freeze + + def initialize(operation:, token:, body:, config: nil) + @config = config || Atol.config + raise(Atol::ConfigExpectedError) unless @config.is_a?(Atol::Config) + raise(Atol::MissingConfigError, 'group_code missing') if @config.group_code.empty? + + unless OPERATIONS.include?(operation.to_sym) + raise(Atol::UnknownOperationError, operation.to_s) + end + + @url = "#{Atol::URL}" + @url << '/' + @url << @config.group_code + @url << '/' + @url << operation.to_s + @url << '?token_id=' + @url << token + + @body = body + end + + def call + uri = URI(url) + req = Net::HTTP::Post.new(uri, HEADERS) + req.body = body + + Net::HTTP.start(uri.hostname, uri.port) do |http| + http.request(req) + end + end + + private + + attr_reader :url, :body + end + end +end diff --git a/spec/atol/errors_spec.rb b/spec/atol/errors_spec.rb index fb5c809..29e4043 100644 --- a/spec/atol/errors_spec.rb +++ b/spec/atol/errors_spec.rb @@ -2,7 +2,16 @@ describe Atol::MissingConfigError do it { expect(Atol::MissingConfigError).to be_a Class } +end +describe Atol::ConfigExpectedError do it { expect(Atol::ConfigExpectedError).to be_a Class } +end +describe Atol::AuthBadRequestError do it { expect(Atol::AuthBadRequestError).to be_a Class } +end +describe Atol::AuthUserOrPasswordError do it { expect(Atol::AuthUserOrPasswordError).to be_a Class } end +describe Atol::UnknownOperationError do + it { expect(Atol::UnknownOperationError).to be_a Class } +end diff --git a/spec/atol/request/post_document_spec.rb b/spec/atol/request/post_document_spec.rb new file mode 100644 index 0000000..3f22016 --- /dev/null +++ b/spec/atol/request/post_document_spec.rb @@ -0,0 +1,65 @@ +require 'net/http' +require './lib/atol/request/post_document' + +describe Atol::Request::PostDocument do + it { expect(described_class).to be_a Class } + + let(:config) { Atol::Config.new(overrides: { group_code: 'group_code_example' }) } + let(:token) { 'token_example' } + let(:body) { '{"json": "example"}' } + let(:base_params) { Hash[token: token, body: body, config: config] } + + describe '#new' do + context 'when given existing operation' do + it 'sell' do + params = base_params.merge(operation: 'sell') + expect { described_class.new(params) }.not_to raise_error + end + + it 'sell_refund' do + params = base_params.merge(operation: 'sell_refund') + expect { described_class.new(params) }.not_to raise_error + end + + it 'sell_correction' do + params = base_params.merge(operation: 'sell_correction') + expect { described_class.new(params) }.not_to raise_error + end + + it 'buy' do + params = base_params.merge(operation: 'buy') + expect { described_class.new(params) }.not_to raise_error + end + + it 'buy_refund' do + params = base_params.merge(operation: 'buy_refund') + expect { described_class.new(params) }.not_to raise_error + end + + it 'buy_correction' do + params = base_params.merge(operation: 'buy_correction') + expect { described_class.new(params) }.not_to raise_error + end + end + + context 'when given not existing operation' do + let(:not_existing_operation) { :kill_all_humans } + let(:params) { base_params.merge(operation: not_existing_operation) } + it { expect { described_class.new(params) }.to raise_error(Atol::UnknownOperationError) } + end + end + + describe '#call' do + before do + stub_request(:post, 'http://online.atol.ru:443/possystem/v3/group_code_example/sell?token_id=token_example') + .to_return(status: 200, body: '', headers: {}) + end + + it 'call return result of http request' do + params = base_params.merge(operation: :sell) + request = described_class.new(params) + response = request.call + expect(response.code).to eql '200' + end + end +end