Skip to content

Commit

Permalink
add configuration options for read + open timeouts
Browse files Browse the repository at this point in the history
  • Loading branch information
Lars Brillert committed May 23, 2018
1 parent 9867b4e commit cdf8dcf
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 14 deletions.
14 changes: 8 additions & 6 deletions lib/iban_calculator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,18 @@ module IbanCalculator
include ActiveSupport::Configurable

# Configuration
config_accessor(:url) { 'https://ssl.ibanrechner.de/soap/?wsdl' }
config_accessor(:user) { '' }
config_accessor(:password) { '' }
config_accessor(:logger) { Logger.new(STDOUT) }
config_accessor(:url) { 'https://ssl.ibanrechner.de/soap/?wsdl' }
config_accessor(:user) { '' }
config_accessor(:password) { '' }
config_accessor(:logger) { Logger.new(STDOUT) }
config_accessor(:read_timeout) { 5 }
config_accessor(:open_timeout) { 5 }

# Errors
ServiceError = Class.new(StandardError)

def self.calculate_iban(attributes = {})
client = IbanBic.new(config.user, config.password, config.url, config.logger)
client = IbanBic.new(config)
client.calculate_iban(attributes)
end

Expand All @@ -34,7 +36,7 @@ def self.validate_iban(iban)
end

def self.execute(method, options = {})
client = Savon.client(wsdl: config.url, logger: config.logger)
client = Savon.client(wsdl: config.url, logger: config.logger, read_timeout: config.read_timeout, open_timeout: config.open_timeout)
client.call(method, message: options).tap do |response|
status = response.body[:"#{method}_response"][:return][:result]
fail(ServiceError, status) unless response.body[:"#{method}_response"][:return][:return_code]
Expand Down
15 changes: 8 additions & 7 deletions lib/iban_calculator/iban_bic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,14 @@ class IbanBic
PROBABLY_VALID_RESPONSE_CODE = 32..127
SERVICE_ERROR_RESPONSE_CODE = 65536

attr_accessor :user, :password, :url, :logger
attr_accessor :user, :password, :url, :logger, :config

def initialize(user, password, url, logger)
self.user = user
self.password = password
self.url = url
self.logger = logger
def initialize(config)
self.user = config.user
self.password = config.password
self.url = config.url
self.logger = config.logger
self.config = config
end

# You should provide country, bank_code, and account_number. (cin, abi, and cab for Italian accounts)
Expand Down Expand Up @@ -98,7 +99,7 @@ def log(message)
end

def client
@client ||= Savon.client(wsdl: url)
@client ||= Savon.client(wsdl: url, read_timeout: config.read_timeout, open_timeout: config.open_timeout)
end
end
end
15 changes: 14 additions & 1 deletion spec/iban_bic_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
require 'ostruct'

describe IbanCalculator::IbanBic do
subject { described_class.new('user', 'pass', 'url', Logger.new(STDOUT) ) }
let(:config) {
OpenStruct.new(
user: 'user',
password: 'pass',
url: 'url',
logger: Logger.new(STDOUT),
read_timeout: 5,
open_timeout: 5
)
}

subject { described_class.new(config) }

before { allow(subject.logger).to receive(:info) }

Expand Down
10 changes: 10 additions & 0 deletions spec/iban_calculator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@
end
end

describe '.calculate_iban' do
let(:error_message) { 'Service could not handle the request' }
let(:response) { { calculate_iban_response: { return: { return_code: '65536' } } } }

before { allow_any_instance_of(Savon::Client).to receive(:call) { double(body: response) } }

it 'raises a generic exception' do
expect { IbanCalculator.calculate_iban({}) }.to raise_error(IbanCalculator::ServiceError, error_message)
end
end

describe '.execute' do
context 'invalid username and password' do
Expand Down

0 comments on commit cdf8dcf

Please sign in to comment.