From 6d9510c8f5a65566cce9089a6a3f534ac348e423 Mon Sep 17 00:00:00 2001 From: Harry Lewis Date: Thu, 29 Aug 2024 13:41:53 -0600 Subject: [PATCH] Support per-request `:environment` configuration --- CHANGELOG.md | 4 ++++ README.md | 10 ++++++---- lib/payrix/client.rb | 2 +- spec/lib/payrix/client_spec.rb | 12 ++++++++---- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c1b11c8..ae1c95b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,10 @@ - Remove `Payrix.test_mode` configuration. +### Additions + +- Support per-request `:environment` configuration for all CRUD operations. + ## 1.0.0 (2023-11-27) - Interact with the Payrix API! diff --git a/README.md b/README.md index dcce201..fe5621a 100644 --- a/README.md +++ b/README.md @@ -240,14 +240,16 @@ merchant.members # => [...} #### Per Request Configuration -Pass in `:api_key` to set the API key used per request. +Set the following configuration parameters per request. + +- `:api_key` - Set the API key used. +- `:environment` - Set the environment used (see valid values in Configuration below). ```ruby -Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { api_key: 'b442...' }) +Payrix::Merchant.retrieve('t1_mer_620acd189522582b3fb7849', { api_key: 'b442...', environment: :production }) ``` -This is useful if you need to set the API key in a thread-safe way, which is not possible through -`Payrix.api_key=`. If both are set, the per request configuration takes precedent. +This is useful if you need to configure the request in a thread-safe way. This is not possible using the static configuration mentioned in Configuration below. The per-request configuration always takes precedent. ### Errors diff --git a/lib/payrix/client.rb b/lib/payrix/client.rb index 526d031..049d9f2 100644 --- a/lib/payrix/client.rb +++ b/lib/payrix/client.rb @@ -4,7 +4,7 @@ module Payrix # The Payrix::Client is used both internally and externally to initiate API requests to the Payrix API. class Client def request(method:, resource:, data: {}, filters: {}, options: {}) - url = Payrix.configuration.url + url = Payrix.configuration.url(options[:environment]) page_number = Payrix::RequestOptions::Page::Number.construct(options[:page]) page_limit = Payrix::RequestOptions::Page::Limit.construct(options[:limit]) diff --git a/spec/lib/payrix/client_spec.rb b/spec/lib/payrix/client_spec.rb index a041775..900a86e 100644 --- a/spec/lib/payrix/client_spec.rb +++ b/spec/lib/payrix/client_spec.rb @@ -37,7 +37,7 @@ WebMock .stub_request( :get, - 'https://test-api.payrix.com/txns?page[number]=1&page[limit]=10&expand[merchant][]=', + 'https://api.payrix.com/txns?page[number]=1&page[limit]=10&expand[merchant][]=', ) .with( headers: { @@ -68,6 +68,7 @@ limit: 10, expand: ['merchant'], api_key: 'custom-key', + environment: :production, }, ) @@ -110,7 +111,7 @@ it 'supports advanced requests' do stub = WebMock - .stub_request(:post, 'https://test-api.payrix.com/txns?expand[merchant][]=') + .stub_request(:post, 'https://api.payrix.com/txns?expand[merchant][]=') .with( headers: { 'Content-Type' => 'application/json', @@ -142,6 +143,7 @@ options: { expand: ['merchant'], api_key: 'custom-key', + environment: :production, }, ) @@ -192,7 +194,7 @@ it 'supports advanced requests' do stub = WebMock - .stub_request(:put, 'https://test-api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da') + .stub_request(:put, 'https://api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da') .with( headers: { 'Content-Type' => 'application/json', @@ -223,6 +225,7 @@ }, options: { api_key: 'custom-key', + environment: :production, }, ) @@ -261,7 +264,7 @@ it 'supports advanced requests' do stub = WebMock - .stub_request(:delete, 'https://test-api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da') + .stub_request(:delete, 'https://api.payrix.com/txns/t1_txn_64026b07cc6a79dd5cfd0da') .with( headers: { 'Content-Type' => 'application/json', @@ -286,6 +289,7 @@ resource: 'txns/t1_txn_64026b07cc6a79dd5cfd0da', options: { api_key: 'custom-key', + environment: :production, }, )