Skip to content

Commit

Permalink
Support environment argument in Configuration#url
Browse files Browse the repository at this point in the history
This is an override argument, which will allow overriding the static
environment configuration.
  • Loading branch information
harrylewis committed Aug 29, 2024
1 parent 225ff37 commit ca35c25
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/payrix/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,16 @@ def environment=(environment)
@environment = environment.to_sym
end

def url
case @environment
def url(environment_override = nil)
environment = @environment

unless environment_override.nil?
validate_environment!(environment_override)

environment = environment_override
end

case environment
when Payrix::MODES.fetch(:sandbox)
'https://test-api.payrix.com'
when Payrix::MODES.fetch(:production)
Expand Down
62 changes: 59 additions & 3 deletions spec/lib/payrix/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,15 +77,31 @@
end

describe '#url' do
context 'when environment is not configured' do
context 'when environment is not configured and nothing is passed' do
it 'returns the sandbox URL https://test-api.payrix.com' do
configuration = described_class.new

expect(configuration.url).to eq('https://test-api.payrix.com')
end
end

context 'when environment is set to sandbox' do
context 'when environment is not configured and sandbox is passed' do
it 'returns the sandbox URL https://test-api.payrix.com' do
configuration = described_class.new

expect(configuration.url(:sandbox)).to eq('https://test-api.payrix.com')
end
end

context 'when environment is not configured and production is passed' do
it 'returns the production URL https://api.payrix.com' do
configuration = described_class.new

expect(configuration.url(:production)).to eq('https://api.payrix.com')
end
end

context 'when environment is set to sandbox and nothing is passed' do
it 'returns the sandbox URL https://test-api.payrix.com' do
configuration = described_class.new

Expand All @@ -95,7 +111,27 @@
end
end

context 'when environment is set to production' do
context 'when environment is set to sandbox and sandbox is passed' do
it 'returns the sandbox URL https://test-api.payrix.com' do
configuration = described_class.new

configuration.environment = :sandbox

expect(configuration.url(:sandbox)).to eq('https://test-api.payrix.com')
end
end

context 'when environment is set to sandbox and production is passed' do
it 'returns the production URL https://api.payrix.com' do
configuration = described_class.new

configuration.environment = :sandbox

expect(configuration.url(:production)).to eq('https://api.payrix.com')
end
end

context 'when environment is set to production and nothing is passed' do
it 'returns the production URL https://api.payrix.com' do
configuration = described_class.new

Expand All @@ -104,5 +140,25 @@
expect(configuration.url).to eq('https://api.payrix.com')
end
end

context 'when environment is set to production and sandbox is passed' do
it 'returns the sandbox URL https://test-api.payrix.com' do
configuration = described_class.new

configuration.environment = :production

expect(configuration.url(:sandbox)).to eq('https://test-api.payrix.com')
end
end

context 'when environment is set to production and production is passed' do
it 'returns the production URL https://api.payrix.com' do
configuration = described_class.new

configuration.environment = :production

expect(configuration.url(:production)).to eq('https://api.payrix.com')
end
end
end
end

0 comments on commit ca35c25

Please sign in to comment.