Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template changes #130

Merged
merged 4 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/gocardless_pro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ module GoCardlessPro
require_relative 'gocardless_pro/resources/event'
require_relative 'gocardless_pro/services/events_service'

require_relative 'gocardless_pro/resources/export'
require_relative 'gocardless_pro/services/exports_service'

require_relative 'gocardless_pro/resources/instalment_schedule'
require_relative 'gocardless_pro/services/instalment_schedules_service'

Expand Down
7 changes: 6 additions & 1 deletion lib/gocardless_pro/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def events
@events ||= Services::EventsService.new(@api_service)
end

# Access to the service for export to make API calls
def exports
@exports ||= Services::ExportsService.new(@api_service)
end

# Access to the service for instalment_schedule to make API calls
def instalment_schedules
@instalment_schedules ||= Services::InstalmentSchedulesService.new(@api_service)
Expand Down Expand Up @@ -228,7 +233,7 @@ def default_options
'User-Agent' => "#{user_agent}",
'Content-Type' => 'application/json',
'GoCardless-Client-Library' => 'gocardless-pro-ruby',
'GoCardless-Client-Version' => '2.56.0',
'GoCardless-Client-Version' => '2.57.0',
},
}
end
Expand Down
44 changes: 44 additions & 0 deletions lib/gocardless_pro/resources/export.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#
# 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 export resource returned from the API

# File-based exports of data
class Export
attr_reader :created_at
attr_reader :currency
attr_reader :download_url
attr_reader :export_type
attr_reader :id

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

@created_at = object['created_at']
@currency = object['currency']
@download_url = object['download_url']
@export_type = object['export_type']
@id = object['id']
@response = response
end

def api_response
ApiResponse.new(@response)
end

# Provides the export resource as a hash of all its readable attributes
def to_h
@object
end
end
end
end
2 changes: 2 additions & 0 deletions lib/gocardless_pro/resources/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class Subscription
attr_reader :metadata
attr_reader :month
attr_reader :name
attr_reader :parent_plan_paused
attr_reader :payment_reference
attr_reader :retry_if_possible
attr_reader :start_date
Expand All @@ -108,6 +109,7 @@ def initialize(object, response = nil)
@metadata = object['metadata']
@month = object['month']
@name = object['name']
@parent_plan_paused = object['parent_plan_paused']
@payment_reference = object['payment_reference']
@retry_if_possible = object['retry_if_possible']
@start_date = object['start_date']
Expand Down
75 changes: 75 additions & 0 deletions lib/gocardless_pro/services/exports_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
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 Export endpoints
class ExportsService < BaseService
# Returns a single export.
# Example URL: /exports/:identity
#
# @param identity # Unique identifier, beginning with "EX".
# @param options [Hash] parameters as a hash, under a params key.
def get(identity, options = {})
path = sub_url('/exports/:identity', {
'identity' => identity,
})

options[:retry_failures] = true

response = make_request(:get, path, options)

return if response.body.nil?

Resources::Export.new(unenvelope_body(response.body), response)
end

# Returns a list of exports which are available for download.
# Example URL: /exports
# @param options [Hash] parameters as a hash, under a params key.
def list(options = {})
path = '/exports'

options[:retry_failures] = true

response = make_request(:get, path, options)

ListResponse.new(
response: response,
unenveloped_body: unenvelope_body(response.body),
resource_class: Resources::Export
)
end

# Get a lazily enumerated list of all the items returned. This is similar to the `list` method but will paginate for you automatically.
#
# @param options [Hash] parameters as a hash. If the request is a GET, these will be converted to query parameters.
# Otherwise they will be the body of the request.
def all(options = {})
Paginator.new(
service: self,
options: options
).enumerator
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
'exports'
end
end
end
end
5 changes: 5 additions & 0 deletions lib/gocardless_pro/services/logos_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ module Services
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.
#
# We support JPG and PNG formats. Your logo will be scaled to a maximum of 300px
# by 40px. For more guidance on how to upload logos that will look
# great across your customer payment page and notification emails see
# [here](https://developer.gocardless.com/gc-embed/setting-up-branding#tips_for_uploading_your_logo).
# Example URL: /branding/logos
# @param options [Hash] parameters as a hash, under a params key.
def create_for_creditor(options = {})
Expand Down
2 changes: 1 addition & 1 deletion lib/gocardless_pro/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module GoCardlessPro

module GoCardlessPro
# Current version of the GC gem
VERSION = '2.56.0'
VERSION = '2.57.0'
end
191 changes: 191 additions & 0 deletions spec/resources/export_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
require 'spec_helper'

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

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

describe '#get' do
let(:id) { 'ID123' }

subject(:get_response) { client.exports.get(id) }

context 'passing in a custom header' do
let!(:stub) do
stub_url = '/exports/:identity'.gsub(':identity', id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).
with(headers: { 'Foo' => 'Bar' }).
to_return(
body: {
'exports' => {

'created_at' => 'created_at-input',
'currency' => 'currency-input',
'download_url' => 'download_url-input',
'export_type' => 'export_type-input',
'id' => 'id-input',
},
}.to_json,
headers: response_headers
)
end

subject(:get_response) do
client.exports.get(id, headers: {
'Foo' => 'Bar',
})
end

it 'includes the header' do
get_response
expect(stub).to have_been_requested
end
end

context 'when there is a export to return' do
before do
stub_url = '/exports/:identity'.gsub(':identity', id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
body: {
'exports' => {

'created_at' => 'created_at-input',
'currency' => 'currency-input',
'download_url' => 'download_url-input',
'export_type' => 'export_type-input',
'id' => 'id-input',
},
}.to_json,
headers: response_headers
)
end

it 'wraps the response in a resource' do
expect(get_response).to be_a(GoCardlessPro::Resources::Export)
end
end

context 'when nothing is returned' do
before do
stub_url = '/exports/:identity'.gsub(':identity', id)
stub_request(:get, /.*api.gocardless.com#{stub_url}/).to_return(
body: '',
headers: response_headers
)
end

it 'returns nil' do
expect(get_response).to be_nil
end
end

context "when an ID is specified which can't be included in a valid URI" do
let(:id) { '`' }

it "doesn't raise an error" do
expect { get_response }.to_not raise_error(/bad URI/)
end
end
end

describe '#list' do
describe 'with no filters' do
subject(:get_list_response) { client.exports.list }

before do
stub_request(:get, %r{.*api.gocardless.com/exports}).to_return(
body: {
'exports' => [{

'created_at' => 'created_at-input',
'currency' => 'currency-input',
'download_url' => 'download_url-input',
'export_type' => 'export_type-input',
'id' => 'id-input',
}],
meta: {
cursors: {
before: nil,
after: 'ABC123',
},
},
}.to_json,
headers: response_headers
)
end

it 'wraps each item in the resource class' do
expect(get_list_response.records.map { |x| x.class }.uniq.first).to eq(GoCardlessPro::Resources::Export)

expect(get_list_response.records.first.created_at).to eq('created_at-input')

expect(get_list_response.records.first.currency).to eq('currency-input')

expect(get_list_response.records.first.download_url).to eq('download_url-input')

expect(get_list_response.records.first.export_type).to eq('export_type-input')

expect(get_list_response.records.first.id).to eq('id-input')
end

it 'exposes the cursors for before and after' do
expect(get_list_response.before).to eq(nil)
expect(get_list_response.after).to eq('ABC123')
end

specify { expect(get_list_response.api_response.headers).to eql('content-type' => 'application/json') }
end
end

describe '#all' do
let!(:first_response_stub) do
stub_request(:get, %r{.*api.gocardless.com/exports$}).to_return(
body: {
'exports' => [{

'created_at' => 'created_at-input',
'currency' => 'currency-input',
'download_url' => 'download_url-input',
'export_type' => 'export_type-input',
'id' => 'id-input',
}],
meta: {
cursors: { after: 'AB345' },
limit: 1,
},
}.to_json,
headers: response_headers
)
end

let!(:second_response_stub) do
stub_request(:get, %r{.*api.gocardless.com/exports\?after=AB345}).to_return(
body: {
'exports' => [{

'created_at' => 'created_at-input',
'currency' => 'currency-input',
'download_url' => 'download_url-input',
'export_type' => 'export_type-input',
'id' => 'id-input',
}],
meta: {
limit: 2,
cursors: {},
},
}.to_json,
headers: response_headers
)
end

it 'automatically makes the extra requests' do
expect(client.exports.all.to_a.length).to eq(2)
expect(first_response_stub).to have_been_requested
expect(second_response_stub).to have_been_requested
end
end
end
Loading
Loading