Skip to content
7 changes: 5 additions & 2 deletions lib/chartmogul/api/actions/retrieve.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@ def self.included(base)

module ClassMethods
def retrieve(uuid, options = {})
path = "#{resource_path.apply(options)}/#{uuid}"
path_param_keys = resource_path.named_params.values
query_params = options.reject { |key| path_param_keys.include?(key) }
resp = handling_errors do
connection.get("#{resource_path.apply(options)}/#{uuid}") do |req|
connection.get(path) do |req|
req.headers['Content-Type'] = 'application/json'
req.params = query_params
end
end

json = ChartMogul::Utils::JSONParser.parse(resp.body, immutable_keys: immutable_keys)
new_from_json(json)
end
Expand Down
5 changes: 0 additions & 5 deletions lib/chartmogul/data_source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class DataSource < APIResource
def self.all(options = {})
DataSources.all(options)
end

def self.retrieve(uuid, options = {})
path = ChartMogul::ResourcePath.new('/v1/data_sources/:uuid')
custom!(:get, path.apply_with_get_params(options.merge(uuid: uuid)))
end
end

class DataSources < APIResource
Expand Down
5 changes: 5 additions & 0 deletions lib/chartmogul/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ class Invoice < APIResource

readonly_attr :uuid
readonly_attr :customer_uuid
readonly_attr :disabled
readonly_attr :disabled_at
readonly_attr :disabled_by
readonly_attr :edit_history_summary
readonly_attr :errors

writeable_attr :date, type: :time
writeable_attr :currency
Expand Down
14 changes: 14 additions & 0 deletions spec/chartmogul/customer_invoices_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -407,5 +407,19 @@
)
end
end

context 'with validation_type query parameter' do
let(:customer_uuid) { 'cus_23551596-2c7e-11ee-9ea1-2bfe193640c0' }

it 'accepts validation_type=all query parameter', uses_api: false do
allow(ChartMogul::CustomerInvoices).to receive(:connection).and_return(double('connection'))
expect(ChartMogul::CustomerInvoices.connection).to receive(:get) do |path|
expect(path).to eq("/v1/import/customers/#{customer_uuid}/invoices?validation_type=all")
double('response', body: '{"invoices": [], "cursor": null, "has_more": false}')
end

ChartMogul::CustomerInvoices.all(customer_uuid, validation_type: 'all')
end
end
end
end
33 changes: 28 additions & 5 deletions spec/chartmogul/data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
data_sources = ChartMogul::DataSource.all(
with_processing_status: true,
with_auto_churn_subscription_setting: true,
with_invoice_handling_setting: true,
with_invoice_handling_setting: true
)

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

data_source = ChartMogul::DataSource.retrieve(ds.uuid,
with_processing_status: true,
with_auto_churn_subscription_setting: true,
with_invoice_handling_setting: true,
)
with_processing_status: true,
with_auto_churn_subscription_setting: true,
with_invoice_handling_setting: true)
expect(data_source.uuid).to eq(ds.uuid)
expect(data_source.name).to eq(ds.name)
expect(data_source.created_at).to eq(ds.created_at)
Expand Down Expand Up @@ -130,5 +129,29 @@
data_source = described_class.retrieve('ds_5ee8bf93-b0b4-4722-8a17-6b624a3af072')
expect(data_source).to be
end

context 'with query parameters' do
it_behaves_like 'retrieve with query params', 'ds_123',
{ with_processing_status: true },
'{"uuid": "ds_123", "name": "Test"}'

it_behaves_like 'retrieve with query params', 'ds_123',
{ with_auto_churn_subscription_setting: true },
'{"uuid": "ds_123", "name": "Test"}'

it_behaves_like 'retrieve with query params', 'ds_123',
{ with_invoice_handling_setting: true },
'{"uuid": "ds_123", "name": "Test"}'

it_behaves_like 'retrieve with query params', 'ds_123',
{ with_processing_status: true,
with_auto_churn_subscription_setting: true,
with_invoice_handling_setting: true },
'{"uuid": "ds_123", "name": "Test"}'

it_behaves_like 'retrieve with query params', 'ds_123',
{ with_processing_status: false },
'{"uuid": "ds_123", "name": "Test"}'
end
end
end
76 changes: 76 additions & 0 deletions spec/chartmogul/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@
{
date: '2016-01-01 12:00:00',
currency: 'USD',
disabled: false,
disabled_at: nil,
disabled_by: nil,
edit_history_summary: {
values_changed: {
amount_in_cents: {
original_value: 900,
edited_value: 1000
}
},
latest_edit_author: 'admin@example.com',
latest_edit_performed_at: '2024-01-10T12:00:00.000Z'
},
errors: nil,
line_items: [
{
type: 'subscription',
Expand Down Expand Up @@ -273,5 +287,67 @@
)
end
end

context 'with query params' do
it 'accepts validation_type=all in list', uses_api: false do
allow(ChartMogul::Invoices).to receive(:connection).and_return(double('connection'))
expect(ChartMogul::Invoices.connection).to receive(:get) do |path|
expect(path).to eq('/v1/invoices?validation_type=all')
double('response', body: '{"invoices": [], "cursor": null, "has_more": false}')
end

described_class.all(validation_type: 'all')
end

it_behaves_like 'retrieve with query params', 'inv_94d194de-04fa-4c81-8871-cc78af388eb3',
{ validation_type: 'all', include_edit_history: true, with_disabled: true },
<<-JSON,
{
"uuid": "inv_94d194de-04fa-4c81-8871-cc78af388eb3",
"external_id": "test",
"currency": "USD",
"disabled": true,
"disabled_at": "2024-01-15T10:30:00.000Z",
"disabled_by": "user@example.com",
"edit_history_summary": {
"values_changed": {
"currency": {
"original_value": "EUR",
"edited_value": "USD"
},
"date": {
"original_value": "2024-01-01T00:00:00.000Z",
"edited_value": "2024-01-02T00:00:00.000Z"
}
},
"latest_edit_author": "editor@example.com",
"latest_edit_performed_at": "2024-01-20T15:45:00.000Z"
},
"errors": {
"currency": ["Currency is invalid", "Currency must be supported"],
"date": ["Date is in the future"]
}
}
JSON
lambda { |invoice|
expect(invoice.disabled).to eq(true)
expect(invoice.disabled_at).to eq('2024-01-15T10:30:00.000Z')
expect(invoice.disabled_by).to eq('user@example.com')
expect(invoice.edit_history_summary).to be_a(Hash)
expect(invoice.edit_history_summary[:values_changed]).to be_a(Hash)
expect(invoice.edit_history_summary[:values_changed][:currency][:original_value]).to eq('EUR')
expect(invoice.edit_history_summary[:values_changed][:currency][:edited_value]).to eq('USD')
expect(invoice.edit_history_summary[:latest_edit_author]).to eq('editor@example.com')
expect(invoice.edit_history_summary[:latest_edit_performed_at]).to eq('2024-01-20T15:45:00.000Z')
expect(invoice.errors).to be_a(Hash)
expect(invoice.errors[:currency]).to be_an(Array)
expect(invoice.errors[:currency].length).to eq(2)
expect(invoice.errors[:currency][0]).to eq('Currency is invalid')
expect(invoice.errors[:currency][1]).to eq('Currency must be supported')
expect(invoice.errors[:date]).to be_an(Array)
expect(invoice.errors[:date].length).to eq(1)
expect(invoice.errors[:date][0]).to eq('Date is in the future')
}
end
end
end
5 changes: 2 additions & 3 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
require 'vcr'
require 'webmock/rspec'
require_relative 'support/shared_example_raises_deprecated_param_error'
require_relative 'support/shared_examples_retrieve_with_query_params'

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

if example.metadata[:uses_api]
ChartMogul.api_key = ENV['TEST_API_KEY'] || 'dummy-token'
end
ChartMogul.api_key = ENV['TEST_API_KEY'] || 'dummy-token' if example.metadata[:uses_api]
end
end
21 changes: 21 additions & 0 deletions spec/support/shared_examples_retrieve_with_query_params.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

RSpec.shared_examples 'retrieve with query params' do |resource_id, query_params, response_body_mock, assertions = nil|
it "accepts #{query_params.keys.join(', ')} query parameter(s)", uses_api: false do
path = "#{described_class.resource_path.path}/#{resource_id}"
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get).with(path) do |&req_block|
req = double('request')
headers = {}
allow(req).to receive(:headers).and_return(headers)
allow(req).to receive(:[]=) { |k, v| headers[k] = v }
expect(req).to receive(:params=).with(hash_including(query_params))

req_block.call(req)
double('response', body: response_body_mock)
end

retrieved_resource = described_class.retrieve(resource_id, query_params)
instance_exec(retrieved_resource, &assertions) if assertions
end
end