Skip to content

Commit

Permalink
Add ! methods that raise the original exceptions returned by the Vert…
Browse files Browse the repository at this point in the history
…ex SOAP API
  • Loading branch information
nht007 committed Aug 7, 2024
1 parent e71ce1c commit 44edccc
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 13 deletions.
16 changes: 16 additions & 0 deletions lib/vertex_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,34 @@ def quotation(payload)
Resource::Quotation.new(payload).result
end

def quotation!(payload)
Resource::Quotation.new(payload).result!
end

def invoice(payload)
Resource::Invoice.new(payload).result
end

def invoice!(payload)
Resource::Invoice.new(payload).result!
end

def distribute_tax(payload)
Resource::DistributeTax.new(payload).result
end

def distribute_tax!(payload)
Resource::DistributeTax.new(payload).result!
end

def tax_area(payload)
Resource::TaxArea.new(payload).result
end

def tax_area!(payload)
Resource::TaxArea.new(payload).result!
end

def circuit
return unless circuit_configured?

Expand Down
30 changes: 23 additions & 7 deletions lib/vertex_client/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ def initialize(endpoint, resource_key=nil)

def request(payload)
call_with_circuit_if_available do
client.call(
:vertex_envelope,
message: shell_with_auth.merge(payload)
)
raw_request(payload)
end
end

def request!(payload)
call_with_circuit_if_available(raise_exceptions: true) do
raw_request(payload)
end
end

Expand All @@ -32,6 +35,13 @@ def client

private

def raw_request(payload)
client.call(
:vertex_envelope,
message: shell_with_auth.merge(payload)
)
end

def config
@config ||= VertexClient.configuration
end
Expand All @@ -40,14 +50,20 @@ def resource_config
config.resource_config[@resource_key] || {}
end

def call_with_circuit_if_available
def call_with_circuit_if_available(raise_exceptions: false)
if VertexClient.circuit
VertexClient.circuit.run(exception: false) { yield }
begin
VertexClient.circuit.run(exception: raise_exceptions) { yield }
rescue Circuitbox::ServiceFailureError => e
nil
raise e.original if raise_exceptions
end
else
begin
yield
rescue => _e
rescue => e
nil
raise e if raise_exceptions
end
end
end
Expand Down
16 changes: 12 additions & 4 deletions lib/vertex_client/resources/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ def initialize(params)
end

def result
@result ||= (response ? formatted_response : fallback_response)
@result ||= formatted_response(response) || fallback_response
end

def result!
formatted_response(response!)
end

def config_key
Expand All @@ -18,15 +22,19 @@ def config_key
private

def response
@response ||= connection.request(@payload.transform)
connection.request(@payload.transform)
end

def response!
connection.request!(@payload.transform)
end

def connection
@connection ||= Connection.new(self.class::ENDPOINT, config_key)
end

def formatted_response
response_type.new(response)
def formatted_response(raw_response)
response_type.new(raw_response) if raw_response
end

def payload_type
Expand Down
7 changes: 7 additions & 0 deletions test/connection_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@
connection.client.expects(:call).with(:vertex_envelope, message: expected_message)
connection.request({ just_a_test: true })
end

it 'raises if config.raise_exceptions? is true' do
connection.send(:config).stubs(:raise_exceptions?).returns(true)
connection.client.stubs(:call).raises(StandardError)

assert_raises(StandardError) { connection.request({ just_a_test: true }) }
end
end

describe 'config' do
Expand Down
26 changes: 24 additions & 2 deletions test/resources/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,33 @@ class VertexClient::Response::MyTest < VertexClient::Response::Base; end;
end
end

describe 'result!' do
describe 'without exceptions' do
before do
resource.stubs(:response!).returns({ test: :ok })
end

it 'returns a formatted response' do
resource.expects(:formatted_response)
resource.result!
end
end

describe 'with exceptions' do
before do
resource.stubs(:response!).raises(StandardError)
end

it 'raises the exception' do
assert_raises(StandardError) { resource.result! }
end
end
end

describe 'formatted_response' do
it 'returns a new response of the proper type' do
resource.expects(:response).returns({test: :ok})
VertexClient::Response::MyTest.expects(:new).with({test: :ok})
resource.send(:formatted_response)
resource.send(:formatted_response, {test: :ok})
end
end
end
28 changes: 28 additions & 0 deletions test/vertex_client_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,55 @@
assert VertexClient.quotation(working_quote_params)
end

it 'does a quotation!' do
VertexClient::Resource::Quotation.expects(:new)
.with(working_quote_params)
.returns(stub(result!: true))
assert VertexClient.quotation!(working_quote_params)
end

it 'does invoice' do
VertexClient::Resource::Invoice.expects(:new)
.with(working_quote_params)
.returns(stub(result: true))
assert VertexClient.invoice(working_quote_params)
end

it 'does invoice!' do
VertexClient::Resource::Invoice.expects(:new)
.with(working_quote_params)
.returns(stub(result!: true))
assert VertexClient.invoice!(working_quote_params)
end

it 'does distribute_tax' do
VertexClient::Resource::DistributeTax.expects(:new)
.with(working_quote_params)
.returns(stub(result: true))
assert VertexClient.distribute_tax(working_quote_params)
end

it 'does distribute_tax!' do
VertexClient::Resource::DistributeTax.expects(:new)
.with(working_quote_params)
.returns(stub(result!: true))
assert VertexClient.distribute_tax!(working_quote_params)
end

it 'does tax_area' do
VertexClient::Resource::TaxArea.expects(:new)
.with(working_quote_params)
.returns(stub(result: true))
assert VertexClient.tax_area(working_quote_params)
end

it 'does tax_area!' do
VertexClient::Resource::TaxArea.expects(:new)
.with(working_quote_params)
.returns(stub(result!: true))
assert VertexClient.tax_area!(working_quote_params)
end

describe 'circuit' do
it 'only exists if circuit_config is provided to configuration' do
VertexClient.configuration.circuit_config = nil
Expand Down

0 comments on commit 44edccc

Please sign in to comment.