Skip to content
This repository has been archived by the owner on May 27, 2021. It is now read-only.

Commit

Permalink
Refactor gateway selector error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
tjoyal committed Dec 10, 2013
1 parent 0a040d6 commit 48fb080
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
11 changes: 9 additions & 2 deletions lib/active_merchant/billing/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,15 @@ def self.mode=(mode)
#
# ActiveMerchant::Billing::Base.gateway('moneris').new
def self.gateway(name)
raise NameError if (name_str = name.to_s.downcase).blank?
Billing.const_get("#{name_str}_gateway".camelize)
name_str = name.to_s.strip.downcase

raise(ArgumentError, 'A gateway provider must be specified') if name_str.blank?

begin
Billing.const_get("#{name_str}_gateway".camelize)
rescue
raise ArgumentError, "The specified gateway is not valid (#{name_str})"
end
end

# Return the matching integration module
Expand Down
24 changes: 19 additions & 5 deletions test/unit/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,32 @@ def test_should_return_a_new_gateway_specified_by_symbol_name
assert_equal LinkpointGateway, Base.gateway(:linkpoint)
end

def test_should_raise_when_invalid_gateway_is_passed
assert_raise NameError do
Base.gateway(:nil)
def test_should_raise_when_nil_gateway_is_passed
e = assert_raise ArgumentError do
Base.gateway(nil)
end
assert_equal 'A gateway provider must be specified', e.message
end

assert_raise NameError do
def test_should_raise_when_empty_gateway_is_passed
e = assert_raise ArgumentError do
Base.gateway('')
end
assert_equal 'A gateway provider must be specified', e.message
end

assert_raise NameError do
def test_should_raise_when_invalid_gateway_symbol_is_passed
e = assert_raise ArgumentError do
Base.gateway(:hotdog)
end
assert_equal 'The specified gateway is not valid (hotdog)', e.message
end

def test_should_raise_when_invalid_gateway_string_is_passed
e = assert_raise ArgumentError do
Base.gateway('hotdog')
end
assert_equal 'The specified gateway is not valid (hotdog)', e.message
end

def test_should_return_an_integration_by_name
Expand Down

0 comments on commit 48fb080

Please sign in to comment.