Support Payrix Canada API through region configuration#13
Conversation
7a88777 to
2a60930
Compare
2a60930 to
a388e35
Compare
a388e35 to
4b2a974
Compare
4b2a974 to
e22f657
Compare
| class Client | ||
| def request(method:, resource:, data: {}, filters: {}, options: {}) | ||
| url = Payrix.configuration.url(options[:environment]) | ||
| url = Payrix.configuration.url(options[:region], options[:environment]) |
There was a problem hiding this comment.
Here we are supporting a new :region option for all requests.
| def region=(region) | ||
| validate_region!(region) | ||
|
|
||
| @region = region.to_sym | ||
| end |
There was a problem hiding this comment.
This is used for setting the region configuration globally.
| unless environment_override.nil? | ||
| validate_environment!(environment_override) | ||
| def url(region_override = nil, environment_override = nil) | ||
| region = region_override || @region |
There was a problem hiding this comment.
This supports a region override, which always take precedent over the global configuration.
| when Payrix::ENVIRONMENTS.fetch(:production) | ||
| 'https://api.payrix.com' | ||
| end | ||
| Payrix::ENDPOINTS[region.to_sym][environment.to_sym] |
There was a problem hiding this comment.
The set of API endpoints has been extracted to a "look up table", where additional regions can be added easily in the future.
| def validate_region!(region) | ||
| raise InvalidRegionError unless region.respond_to?(:to_sym) | ||
| raise InvalidRegionError unless Payrix::REGIONS.values.include?(region.to_sym) | ||
| end |
There was a problem hiding this comment.
This library aims to be as developer friendly as possible. This means surfacing developer errors as soon as possible. We ensure the region setting is within the supported list, or raise an error before we even send the request to the Payrix API.
This way, a developer doesn't need to wait long to get this feedback.
| REGIONS.fetch(:ca) => { | ||
| ENVIRONMENTS.fetch(:sandbox) => 'https://test-api.payrixcanada.com', | ||
| ENVIRONMENTS.fetch(:production) => 'https://api.payrixcanada.com', | ||
| }, |
There was a problem hiding this comment.
We support the Payrix Canada API by routing to these endpoints, when configured with :ca above.
There was a problem hiding this comment.
I didn't skimp on the tests. We have many permutations and combinations below.
| require 'webmock/rspec' | ||
| require 'pry' | ||
|
|
||
| Payrix.region = :us |
There was a problem hiding this comment.
Since the region is now required, this means our tests required this configuration as well.
Description
This PR introduces support for the Payrix Canada API. This API is served through a different set of endpoints.
These differ from the USA API endpoints. Therefore, a new required region configuration has been introduced in order to specify which API to use. This configuration can be set globally.
It can also be configured on a per-request basis.