Skip to content

Commit

Permalink
Changes from gocardless/gocardless-pro-ruby-template@d5f190cd24ec4019…
Browse files Browse the repository at this point in the history
…fcd3fb69462fd19c2b057650
  • Loading branch information
gocardless-robot committed Nov 21, 2023
1 parent 244b5a9 commit 8fbd025
Show file tree
Hide file tree
Showing 6 changed files with 267 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 @@ -129,6 +129,9 @@ module GoCardlessPro
require_relative 'gocardless_pro/resources/tax_rate'
require_relative 'gocardless_pro/services/tax_rates_service'

require_relative 'gocardless_pro/resources/transferred_mandate'
require_relative 'gocardless_pro/services/transferred_mandates_service'

require_relative 'gocardless_pro/resources/verification_detail'
require_relative 'gocardless_pro/services/verification_details_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 @@ -153,6 +153,11 @@ def tax_rates
@tax_rates ||= Services::TaxRatesService.new(@api_service)
end

# Access to the service for transferred_mandate to make API calls
def transferred_mandates
@transferred_mandates ||= Services::TransferredMandatesService.new(@api_service)
end

# Access to the service for verification_detail to make API calls
def verification_details
@verification_details ||= Services::VerificationDetailsService.new(@api_service)
Expand Down
60 changes: 60 additions & 0 deletions lib/gocardless_pro/resources/transferred_mandate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#
# 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 transferred_mandate resource returned from the API

# Mandates that have been transferred using Current Account Switch Service
class TransferredMandate
attr_reader :encrypted_data
attr_reader :key
attr_reader :kid

# Initialize a transferred_mandate resource instance
# @param object [Hash] an object returned from the API
def initialize(object, response = nil)
@object = object

@encrypted_data = object['encrypted_data']
@key = object['key']
@kid = object['kid']
@links = object['links']
@response = response
end

def api_response
ApiResponse.new(@response)
end

# Return the links that the resource has
def links
@transferred_mandate_links ||= Links.new(@links)
end

# Provides the transferred_mandate resource as a hash of all its readable attributes
def to_h
@object
end

class Links
def initialize(links)
@links = links || {}
end

def customer_bank_account
@links['customer_bank_account']
end

def mandate
@links['mandate']
end
end
end
end
end
48 changes: 48 additions & 0 deletions lib/gocardless_pro/services/transferred_mandates_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 TransferredMandate endpoints
class TransferredMandatesService < BaseService
# Returns encrypted bank details for the transferred mandate
# Example URL: /transferred_mandate/:identity
#
# @param identity # Unique identifier, beginning with "MD". Note that this prefix may not
# apply to mandates created before 2016.
# @param options [Hash] parameters as a hash, under a params key.
def transferred_mandates(identity, options = {})
path = sub_url('/transferred_mandate/:identity', {
'identity' => identity,
})

options[:retry_failures] = false

response = make_request(:get, path, options)

return if response.body.nil?

Resources::TransferredMandate.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
'transferred_mandate'
end
end
end
end
70 changes: 70 additions & 0 deletions spec/resources/transferred_mandate_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
require 'spec_helper'

describe GoCardlessPro::Resources::TransferredMandate do
let(:client) do
GoCardlessPro::Client.new(
access_token: 'SECRET_TOKEN'
)
end

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

describe '#transferred_mandates' do
subject(:get_response) { client.transferred_mandates.transferred_mandates(resource_id) }

let(:resource_id) { 'ABC123' }

let!(:stub) do
# /transferred_mandate/%v
stub_url = '/transferred_mandate/:identity'.gsub(':identity', resource_id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
body: {
'transferred_mandate' => {

'encrypted_data' => 'encrypted_data-input',
'key' => 'key-input',
'kid' => 'kid-input',
'links' => 'links-input',
},
}.to_json,

headers: response_headers
)
end

it 'wraps the response and calls the right endpoint' do
expect(get_response).to be_a(GoCardlessPro::Resources::TransferredMandate)

expect(stub).to have_been_requested
end

context 'when the request needs a body and custom header' do
let(:body) { { foo: 'bar' } }
let(:headers) { { 'Foo' => 'Bar' } }
subject(:get_response) { client.transferred_mandates.transferred_mandates(resource_id, body, headers) }

let(:resource_id) { 'ABC123' }

let!(:stub) do
# /transferred_mandate/%v
stub_url = '/transferred_mandate/:identity'.gsub(':identity', resource_id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).
with(
body: { foo: 'bar' },
headers: { 'Foo' => 'Bar' }
).to_return(
body: {
'transferred_mandate' => {

'encrypted_data' => 'encrypted_data-input',
'key' => 'key-input',
'kid' => 'kid-input',
'links' => 'links-input',
},
}.to_json,
headers: response_headers
)
end
end
end
end
81 changes: 81 additions & 0 deletions spec/services/transferred_mandates_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
require 'spec_helper'

describe GoCardlessPro::Services::TransferredMandatesService do
let(:client) do
GoCardlessPro::Client.new(
access_token: 'SECRET_TOKEN'
)
end

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

describe '#transferred_mandates' do
subject(:get_response) { client.transferred_mandates.transferred_mandates(resource_id) }

let(:resource_id) { 'ABC123' }

let!(:stub) do
# /transferred_mandate/%v
stub_url = '/transferred_mandate/:identity'.gsub(':identity', resource_id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
body: {
'transferred_mandate' => {

'encrypted_data' => 'encrypted_data-input',
'key' => 'key-input',
'kid' => 'kid-input',
'links' => 'links-input',
},
}.to_json,

headers: response_headers
)
end

it 'wraps the response and calls the right endpoint' do
expect(get_response).to be_a(GoCardlessPro::Resources::TransferredMandate)

expect(stub).to have_been_requested
end

describe 'retry behaviour' do
it "doesn't retry errors" do
stub_url = '/transferred_mandate/:identity'.gsub(':identity', resource_id)
stub = stub_request(:get, /.*api.gocardless.com#{stub_url}/).
to_timeout

expect { get_response }.to raise_error(Faraday::ConnectionFailed)
expect(stub).to have_been_requested
end
end

context 'when the request needs a body and custom header' do
let(:body) { { foo: 'bar' } }
let(:headers) { { 'Foo' => 'Bar' } }
subject(:get_response) { client.transferred_mandates.transferred_mandates(resource_id, body, headers) }

let(:resource_id) { 'ABC123' }

let!(:stub) do
# /transferred_mandate/%v
stub_url = '/transferred_mandate/:identity'.gsub(':identity', resource_id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).
with(
body: { foo: 'bar' },
headers: { 'Foo' => 'Bar' }
).to_return(
body: {
'transferred_mandate' => {

'encrypted_data' => 'encrypted_data-input',
'key' => 'key-input',
'kid' => 'kid-input',
'links' => 'links-input',
},
}.to_json,
headers: response_headers
)
end
end
end
end

0 comments on commit 8fbd025

Please sign in to comment.