forked from sputnik8/atol-rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ef76dc8
commit 07df688
Showing
4 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |