diff --git a/lib/payrix/configuration.rb b/lib/payrix/configuration.rb index e3a63a6..54f4430 100644 --- a/lib/payrix/configuration.rb +++ b/lib/payrix/configuration.rb @@ -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) diff --git a/spec/lib/payrix/configuration_spec.rb b/spec/lib/payrix/configuration_spec.rb index ef59547..97f239a 100644 --- a/spec/lib/payrix/configuration_spec.rb +++ b/spec/lib/payrix/configuration_spec.rb @@ -77,7 +77,7 @@ 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 @@ -85,7 +85,23 @@ 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 @@ -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 @@ -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