-
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@d5f190cd24ec4019…
…fcd3fb69462fd19c2b057650
- Loading branch information
1 parent
244b5a9
commit 8fbd025
Showing
6 changed files
with
267 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,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
48
lib/gocardless_pro/services/transferred_mandates_service.rb
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,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 |
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,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 |
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,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 |