Skip to content
9 changes: 7 additions & 2 deletions lib/chartmogul/api/actions/retrieve.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require 'uri'

module ChartMogul
module API
module Actions
Expand All @@ -10,12 +12,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) }
path = "#{path}?#{URI.encode_www_form(query_params)}" if query_params.any?
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[refactor] Instead use the faraday library to pass params.
https://rubydoc.info/gems/faraday/Faraday/Request

resp = handling_errors do
connection.get("#{resource_path.apply(options)}/#{uuid}") do |req|
connection.get(path) do |req|
req.headers['Content-Type'] = 'application/json'
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
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
61 changes: 61 additions & 0 deletions spec/chartmogul/data_source_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,66 @@
data_source = described_class.retrieve('ds_5ee8bf93-b0b4-4722-8a17-6b624a3af072')
expect(data_source).to be
end

context 'with query parameters' do
it 'accepts with_processing_status query parameter', uses_api: false do
# Mock the connection to verify the URL includes the query parameter
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get) do |path|
expect(path).to eq('/v1/data_sources/ds_123?with_processing_status=true')
double('response', body: '{"uuid": "ds_123", "name": "Test"}')
end

described_class.retrieve('ds_123', with_processing_status: true)
end

it 'accepts with_auto_churn_subscription_setting query parameter', uses_api: false do
# Mock the connection to verify the URL includes the query parameter
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get) do |path|
expect(path).to eq('/v1/data_sources/ds_123?with_auto_churn_subscription_setting=true')
double('response', body: '{"uuid": "ds_123", "name": "Test"}')
end

described_class.retrieve('ds_123', with_auto_churn_subscription_setting: true)
end

it 'accepts with_invoice_handling_setting query parameter', uses_api: false do
# Mock the connection to verify the URL includes the query parameter
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get) do |path|
expect(path).to eq('/v1/data_sources/ds_123?with_invoice_handling_setting=true')
double('response', body: '{"uuid": "ds_123", "name": "Test"}')
end

described_class.retrieve('ds_123', with_invoice_handling_setting: true)
end

it 'accepts all three query parameters together', uses_api: false do
# Mock the connection to verify the URL includes all query parameters
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get) do |path|
expect(path).to eq('/v1/data_sources/ds_123?with_processing_status=true&with_auto_churn_subscription_setting=true&with_invoice_handling_setting=true')
double('response', body: '{"uuid": "ds_123", "name": "Test"}')
end

described_class.retrieve('ds_123',
with_processing_status: true,
with_auto_churn_subscription_setting: true,
with_invoice_handling_setting: true
)
end

it 'accepts with_processing_status=false query parameter', uses_api: false do
# Mock the connection to verify the URL includes the query parameter
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get) do |path|
expect(path).to eq('/v1/data_sources/ds_123?with_processing_status=false')
double('response', body: '{"uuid": "ds_123", "name": "Test"}')
end

described_class.retrieve('ds_123', with_processing_status: false)
end
end
end
end
24 changes: 24 additions & 0 deletions spec/chartmogul/invoice_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,29 @@
)
end
end

context 'with validation_type query parameter' do
let(:invoice_uuid) { 'inv_94d194de-04fa-4c81-8871-cc78af388eb3' }

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 'accepts validation_type=all in retrieve', uses_api: false do
allow(described_class).to receive(:connection).and_return(double('connection'))
expect(described_class.connection).to receive(:get) do |path|
expect(path).to eq("/v1/invoices/#{invoice_uuid}?validation_type=all")
double('response', body: "{\"uuid\": \"#{invoice_uuid}\", \"external_id\": \"test\", \"currency\": \"USD\"}")
end

described_class.retrieve(invoice_uuid, validation_type: 'all')
end
end
end
end