Skip to content

Commit

Permalink
post_document request
Browse files Browse the repository at this point in the history
  • Loading branch information
GeorgeGorbanev committed Jan 15, 2018
1 parent ef76dc8 commit 07df688
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/atol/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
46 changes: 46 additions & 0 deletions lib/atol/request/post_document.rb
Original file line number Diff line number Diff line change
@@ -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
9 changes: 9 additions & 0 deletions spec/atol/errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
65 changes: 65 additions & 0 deletions spec/atol/request/post_document_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 07df688

Please sign in to comment.