-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changes from gocardless/gocardless-pro-ruby-template@7d42c692d543d5a1…
…308b0cb2435d1e3bd94c7d93
- Loading branch information
1 parent
34f85e9
commit bf82ab1
Showing
9 changed files
with
589 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# | ||
# This client is automatically generated from a template and JSON schema definition. | ||
# See https://github.com/gocardless/gocardless-pro-ruby#contributing before editing. | ||
# | ||
|
||
require 'uri' | ||
|
||
module GoCardlessPro | ||
# A module containing classes for each of the resources in the GC Api | ||
module Resources | ||
# Represents an instance of a payer_theme resource returned from the API | ||
|
||
# Custom colour themes for payment pages and customer notifications. | ||
class PayerTheme | ||
attr_reader :id | ||
|
||
# Initialize a payer_theme resource instance | ||
# @param object [Hash] an object returned from the API | ||
def initialize(object, response = nil) | ||
@object = object | ||
|
||
@id = object['id'] | ||
@response = response | ||
end | ||
|
||
def api_response | ||
ApiResponse.new(@response) | ||
end | ||
|
||
# Provides the payer_theme resource as a hash of all its readable attributes | ||
def to_h | ||
@object | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
require_relative './base_service' | ||
|
||
# encoding: utf-8 | ||
# | ||
# This client is automatically generated from a template and JSON schema definition. | ||
# See https://github.com/gocardless/gocardless-pro-ruby#contributing before editing. | ||
# | ||
|
||
module GoCardlessPro | ||
module Services | ||
# Service for making requests to the PayerTheme endpoints | ||
class PayerThemesService < BaseService | ||
# Creates a new payer theme associated with a creditor. If a creditor already | ||
# has payer themes, this will update the existing payer theme linked to the | ||
# creditor. | ||
# Example URL: /branding/payer_themes | ||
# @param options [Hash] parameters as a hash, under a params key. | ||
def create_for_creditor(options = {}) | ||
path = '/branding/payer_themes' | ||
|
||
params = options.delete(:params) || {} | ||
options[:params] = {} | ||
options[:params][envelope_key] = params | ||
|
||
options[:retry_failures] = true | ||
|
||
response = make_request(:post, path, options) | ||
|
||
return if response.body.nil? | ||
|
||
Resources::PayerTheme.new(unenvelope_body(response.body), response) | ||
end | ||
|
||
private | ||
|
||
# Unenvelope the response of the body using the service's `envelope_key` | ||
# | ||
# @param body [Hash] | ||
def unenvelope_body(body) | ||
body[envelope_key] || body['data'] | ||
end | ||
|
||
# return the key which API responses will envelope data under | ||
def envelope_key | ||
'payer_themes' | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
require 'spec_helper' | ||
|
||
describe GoCardlessPro::Resources::PayerTheme do | ||
let(:client) do | ||
GoCardlessPro::Client.new( | ||
access_token: 'SECRET_TOKEN' | ||
) | ||
end | ||
|
||
let(:response_headers) { { 'Content-Type' => 'application/json' } } | ||
|
||
describe '#create' do | ||
subject(:post_create_response) { client.payer_themes.create_for_creditor(params: new_resource) } | ||
context 'with a valid request' do | ||
let(:new_resource) do | ||
{ | ||
|
||
'id' => 'id-input', | ||
} | ||
end | ||
|
||
before do | ||
stub_request(:post, %r{.*api.gocardless.com/branding/payer_themes}). | ||
with( | ||
body: { | ||
'payer_themes' => { | ||
|
||
'id' => 'id-input', | ||
}, | ||
} | ||
). | ||
to_return( | ||
body: { | ||
'payer_themes' => | ||
|
||
{ | ||
|
||
'id' => 'id-input', | ||
}, | ||
|
||
}.to_json, | ||
headers: response_headers | ||
) | ||
end | ||
|
||
it 'creates and returns the resource' do | ||
expect(post_create_response).to be_a(GoCardlessPro::Resources::PayerTheme) | ||
end | ||
end | ||
|
||
context 'with a request that returns a validation error' do | ||
let(:new_resource) { {} } | ||
|
||
before do | ||
stub_request(:post, %r{.*api.gocardless.com/branding/payer_themes}).to_return( | ||
body: { | ||
error: { | ||
type: 'validation_failed', | ||
code: 422, | ||
errors: [ | ||
{ message: 'test error message', field: 'test_field' }, | ||
], | ||
}, | ||
}.to_json, | ||
headers: response_headers, | ||
status: 422 | ||
) | ||
end | ||
|
||
it 'throws the correct error' do | ||
expect { post_create_response }.to raise_error(GoCardlessPro::ValidationError) | ||
end | ||
end | ||
|
||
context 'with a request that returns an idempotent creation conflict error' do | ||
let(:id) { 'ID123' } | ||
|
||
let(:new_resource) do | ||
{ | ||
|
||
'id' => 'id-input', | ||
} | ||
end | ||
|
||
let!(:post_stub) do | ||
stub_request(:post, %r{.*api.gocardless.com/branding/payer_themes}).to_return( | ||
body: { | ||
error: { | ||
type: 'invalid_state', | ||
code: 409, | ||
errors: [ | ||
{ | ||
message: 'A resource has already been created with this idempotency key', | ||
reason: 'idempotent_creation_conflict', | ||
links: { | ||
conflicting_resource_id: id, | ||
}, | ||
}, | ||
], | ||
}, | ||
}.to_json, | ||
headers: response_headers, | ||
status: 409 | ||
) | ||
end | ||
|
||
it 'raises an InvalidStateError' do | ||
expect { post_create_response }.to raise_error(GoCardlessPro::InvalidStateError) | ||
expect(post_stub).to have_been_requested | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.