Skip to content

Commit 0107fa7

Browse files
authored
Update retrieve to accept query params (#182)
* Update retrieve to accept query params * Add #edit_history_summary, #errors, #disabled, #disabled_at and #disabled_by to Invoice
1 parent 12e87fa commit 0107fa7

8 files changed

Lines changed: 151 additions & 15 deletions

File tree

lib/chartmogul/api/actions/retrieve.rb

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,15 @@ def self.included(base)
1010

1111
module ClassMethods
1212
def retrieve(uuid, options = {})
13+
path = "#{resource_path.apply(options)}/#{uuid}"
14+
path_param_keys = resource_path.named_params.values
15+
query_params = options.reject { |key| path_param_keys.include?(key) }
1316
resp = handling_errors do
14-
connection.get("#{resource_path.apply(options)}/#{uuid}") do |req|
17+
connection.get(path) do |req|
1518
req.headers['Content-Type'] = 'application/json'
19+
req.params = query_params
1620
end
1721
end
18-
1922
json = ChartMogul::Utils::JSONParser.parse(resp.body, immutable_keys: immutable_keys)
2023
new_from_json(json)
2124
end

lib/chartmogul/data_source.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@ class DataSource < APIResource
2424
def self.all(options = {})
2525
DataSources.all(options)
2626
end
27-
28-
def self.retrieve(uuid, options = {})
29-
path = ChartMogul::ResourcePath.new('/v1/data_sources/:uuid')
30-
custom!(:get, path.apply_with_get_params(options.merge(uuid: uuid)))
31-
end
3227
end
3328

3429
class DataSources < APIResource

lib/chartmogul/invoice.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ class Invoice < APIResource
77

88
readonly_attr :uuid
99
readonly_attr :customer_uuid
10+
readonly_attr :disabled
11+
readonly_attr :disabled_at
12+
readonly_attr :disabled_by
13+
readonly_attr :edit_history_summary
14+
readonly_attr :errors
1015

1116
writeable_attr :date, type: :time
1217
writeable_attr :currency

spec/chartmogul/customer_invoices_spec.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,19 @@
407407
)
408408
end
409409
end
410+
411+
context 'with validation_type query parameter' do
412+
let(:customer_uuid) { 'cus_23551596-2c7e-11ee-9ea1-2bfe193640c0' }
413+
414+
it 'accepts validation_type=all query parameter', uses_api: false do
415+
allow(ChartMogul::CustomerInvoices).to receive(:connection).and_return(double('connection'))
416+
expect(ChartMogul::CustomerInvoices.connection).to receive(:get) do |path|
417+
expect(path).to eq("/v1/import/customers/#{customer_uuid}/invoices?validation_type=all")
418+
double('response', body: '{"invoices": [], "cursor": null, "has_more": false}')
419+
end
420+
421+
ChartMogul::CustomerInvoices.all(customer_uuid, validation_type: 'all')
422+
end
423+
end
410424
end
411425
end

spec/chartmogul/data_source_spec.rb

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
data_sources = ChartMogul::DataSource.all(
4848
with_processing_status: true,
4949
with_auto_churn_subscription_setting: true,
50-
with_invoice_handling_setting: true,
50+
with_invoice_handling_setting: true
5151
)
5252

5353
expect(data_sources.size).to eq(1)
@@ -78,10 +78,9 @@
7878
expect(data_source.invoice_handling_setting[:manual][:prevent_subscription_for_invoice_written_off]).to eq(true)
7979

8080
data_source = ChartMogul::DataSource.retrieve(ds.uuid,
81-
with_processing_status: true,
82-
with_auto_churn_subscription_setting: true,
83-
with_invoice_handling_setting: true,
84-
)
81+
with_processing_status: true,
82+
with_auto_churn_subscription_setting: true,
83+
with_invoice_handling_setting: true)
8584
expect(data_source.uuid).to eq(ds.uuid)
8685
expect(data_source.name).to eq(ds.name)
8786
expect(data_source.created_at).to eq(ds.created_at)
@@ -130,5 +129,29 @@
130129
data_source = described_class.retrieve('ds_5ee8bf93-b0b4-4722-8a17-6b624a3af072')
131130
expect(data_source).to be
132131
end
132+
133+
context 'with query parameters' do
134+
it_behaves_like 'retrieve with query params', 'ds_123',
135+
{ with_processing_status: true },
136+
'{"uuid": "ds_123", "name": "Test"}'
137+
138+
it_behaves_like 'retrieve with query params', 'ds_123',
139+
{ with_auto_churn_subscription_setting: true },
140+
'{"uuid": "ds_123", "name": "Test"}'
141+
142+
it_behaves_like 'retrieve with query params', 'ds_123',
143+
{ with_invoice_handling_setting: true },
144+
'{"uuid": "ds_123", "name": "Test"}'
145+
146+
it_behaves_like 'retrieve with query params', 'ds_123',
147+
{ with_processing_status: true,
148+
with_auto_churn_subscription_setting: true,
149+
with_invoice_handling_setting: true },
150+
'{"uuid": "ds_123", "name": "Test"}'
151+
152+
it_behaves_like 'retrieve with query params', 'ds_123',
153+
{ with_processing_status: false },
154+
'{"uuid": "ds_123", "name": "Test"}'
155+
end
133156
end
134157
end

spec/chartmogul/invoice_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@
77
{
88
date: '2016-01-01 12:00:00',
99
currency: 'USD',
10+
disabled: false,
11+
disabled_at: nil,
12+
disabled_by: nil,
13+
edit_history_summary: {
14+
values_changed: {
15+
amount_in_cents: {
16+
original_value: 900,
17+
edited_value: 1000
18+
}
19+
},
20+
latest_edit_author: 'admin@example.com',
21+
latest_edit_performed_at: '2024-01-10T12:00:00.000Z'
22+
},
23+
errors: nil,
1024
line_items: [
1125
{
1226
type: 'subscription',
@@ -273,5 +287,67 @@
273287
)
274288
end
275289
end
290+
291+
context 'with query params' do
292+
it 'accepts validation_type=all in list', uses_api: false do
293+
allow(ChartMogul::Invoices).to receive(:connection).and_return(double('connection'))
294+
expect(ChartMogul::Invoices.connection).to receive(:get) do |path|
295+
expect(path).to eq('/v1/invoices?validation_type=all')
296+
double('response', body: '{"invoices": [], "cursor": null, "has_more": false}')
297+
end
298+
299+
described_class.all(validation_type: 'all')
300+
end
301+
302+
it_behaves_like 'retrieve with query params', 'inv_94d194de-04fa-4c81-8871-cc78af388eb3',
303+
{ validation_type: 'all', include_edit_history: true, with_disabled: true },
304+
<<-JSON,
305+
{
306+
"uuid": "inv_94d194de-04fa-4c81-8871-cc78af388eb3",
307+
"external_id": "test",
308+
"currency": "USD",
309+
"disabled": true,
310+
"disabled_at": "2024-01-15T10:30:00.000Z",
311+
"disabled_by": "user@example.com",
312+
"edit_history_summary": {
313+
"values_changed": {
314+
"currency": {
315+
"original_value": "EUR",
316+
"edited_value": "USD"
317+
},
318+
"date": {
319+
"original_value": "2024-01-01T00:00:00.000Z",
320+
"edited_value": "2024-01-02T00:00:00.000Z"
321+
}
322+
},
323+
"latest_edit_author": "editor@example.com",
324+
"latest_edit_performed_at": "2024-01-20T15:45:00.000Z"
325+
},
326+
"errors": {
327+
"currency": ["Currency is invalid", "Currency must be supported"],
328+
"date": ["Date is in the future"]
329+
}
330+
}
331+
JSON
332+
lambda { |invoice|
333+
expect(invoice.disabled).to eq(true)
334+
expect(invoice.disabled_at).to eq('2024-01-15T10:30:00.000Z')
335+
expect(invoice.disabled_by).to eq('user@example.com')
336+
expect(invoice.edit_history_summary).to be_a(Hash)
337+
expect(invoice.edit_history_summary[:values_changed]).to be_a(Hash)
338+
expect(invoice.edit_history_summary[:values_changed][:currency][:original_value]).to eq('EUR')
339+
expect(invoice.edit_history_summary[:values_changed][:currency][:edited_value]).to eq('USD')
340+
expect(invoice.edit_history_summary[:latest_edit_author]).to eq('editor@example.com')
341+
expect(invoice.edit_history_summary[:latest_edit_performed_at]).to eq('2024-01-20T15:45:00.000Z')
342+
expect(invoice.errors).to be_a(Hash)
343+
expect(invoice.errors[:currency]).to be_an(Array)
344+
expect(invoice.errors[:currency].length).to eq(2)
345+
expect(invoice.errors[:currency][0]).to eq('Currency is invalid')
346+
expect(invoice.errors[:currency][1]).to eq('Currency must be supported')
347+
expect(invoice.errors[:date]).to be_an(Array)
348+
expect(invoice.errors[:date].length).to eq(1)
349+
expect(invoice.errors[:date][0]).to eq('Date is in the future')
350+
}
351+
end
276352
end
277353
end

spec/spec_helper.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
require 'vcr'
1313
require 'webmock/rspec'
1414
require_relative 'support/shared_example_raises_deprecated_param_error'
15+
require_relative 'support/shared_examples_retrieve_with_query_params'
1516

1617
VCR.configure do |config|
1718
config.cassette_library_dir = 'spec/fixtures/vcr_cassettes'
@@ -28,8 +29,6 @@
2829
config.before(:each) do |example|
2930
Thread.current[ChartMogul::CONFIG_THREAD_KEY] = nil
3031

31-
if example.metadata[:uses_api]
32-
ChartMogul.api_key = ENV['TEST_API_KEY'] || 'dummy-token'
33-
end
32+
ChartMogul.api_key = ENV['TEST_API_KEY'] || 'dummy-token' if example.metadata[:uses_api]
3433
end
3534
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# frozen_string_literal: true
2+
3+
RSpec.shared_examples 'retrieve with query params' do |resource_id, query_params, response_body_mock, assertions = nil|
4+
it "accepts #{query_params.keys.join(', ')} query parameter(s)", uses_api: false do
5+
path = "#{described_class.resource_path.path}/#{resource_id}"
6+
allow(described_class).to receive(:connection).and_return(double('connection'))
7+
expect(described_class.connection).to receive(:get).with(path) do |&req_block|
8+
req = double('request')
9+
headers = {}
10+
allow(req).to receive(:headers).and_return(headers)
11+
allow(req).to receive(:[]=) { |k, v| headers[k] = v }
12+
expect(req).to receive(:params=).with(hash_including(query_params))
13+
14+
req_block.call(req)
15+
double('response', body: response_body_mock)
16+
end
17+
18+
retrieved_resource = described_class.retrieve(resource_id, query_params)
19+
instance_exec(retrieved_resource, &assertions) if assertions
20+
end
21+
end

0 commit comments

Comments
 (0)