Skip to content

Commit

Permalink
Changes from gocardless/gocardless-pro-ruby-template@7d42c692d543d5a1…
Browse files Browse the repository at this point in the history
…308b0cb2435d1e3bd94c7d93
  • Loading branch information
gocardless-robot committed May 28, 2024
1 parent 34f85e9 commit bf82ab1
Show file tree
Hide file tree
Showing 9 changed files with 589 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/gocardless_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ module GoCardlessPro
require_relative 'gocardless_pro/resources/payer_authorisation'
require_relative 'gocardless_pro/services/payer_authorisations_service'

require_relative 'gocardless_pro/resources/payer_theme'
require_relative 'gocardless_pro/services/payer_themes_service'

require_relative 'gocardless_pro/resources/payment'
require_relative 'gocardless_pro/services/payments_service'

Expand Down
5 changes: 5 additions & 0 deletions lib/gocardless_pro/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ def payer_authorisations
@payer_authorisations ||= Services::PayerAuthorisationsService.new(@api_service)
end

# Access to the service for payer_theme to make API calls
def payer_themes
@payer_themes ||= Services::PayerThemesService.new(@api_service)
end

# Access to the service for payment to make API calls
def payments
@payments ||= Services::PaymentsService.new(@api_service)
Expand Down
36 changes: 36 additions & 0 deletions lib/gocardless_pro/resources/payer_theme.rb
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
20 changes: 20 additions & 0 deletions lib/gocardless_pro/services/logos_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@ module GoCardlessPro
module Services
# Service for making requests to the Logo endpoints
class LogosService < BaseService
# Creates a new logo associated with a creditor. If a creditor already has a
# logo, this will update the existing logo linked to the creditor.
# Example URL: /branding/logos
# @param options [Hash] parameters as a hash, under a params key.
def create_for_creditor(options = {})
path = '/branding/logos'

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::Logo.new(unenvelope_body(response.body), response)
end

private

# Unenvelope the response of the body using the service's `envelope_key`
Expand Down
49 changes: 49 additions & 0 deletions lib/gocardless_pro/services/payer_themes_service.rb
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
102 changes: 102 additions & 0 deletions spec/resources/logo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,106 @@
end

let(:response_headers) { { 'Content-Type' => 'application/json' } }

describe '#create' do
subject(:post_create_response) { client.logos.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/logos}).
with(
body: {
'logos' => {

'id' => 'id-input',
},
}
).
to_return(
body: {
'logos' =>

{

'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::Logo)
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/logos}).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/logos}).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
113 changes: 113 additions & 0 deletions spec/resources/payer_theme_spec.rb
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
Loading

0 comments on commit bf82ab1

Please sign in to comment.