Skip to content

Commit

Permalink
refactored when request and retrieve same resource
Browse files Browse the repository at this point in the history
  • Loading branch information
sbounmy committed Apr 25, 2024
1 parent 22a4a4f commit 60433c9
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions lib/pennylane/resources/customer_invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,8 @@ def update(attributes)
self
end

# API returns an empty body
# so we need to skip values assignment from the response
# GET /customer_invoices/:id again to get the updated values
def finalize
self.class.request_pennylane_object(method: :put,
path: "/customer_invoices/#{id}/finalize",
params: {},
opts: {}, with: { invoice: 'customer_invoice' })
resp, opts = self.class.request_pennylane_object(method: :get,
path: "/customer_invoices/#{id}",
params: {},
opts: {}, with: { invoice: 'customer_invoice' })
@values = resp.instance_variable_get :@values
self
request_and_retrieve(method: :put, path: "/customer_invoices/#{id}", action: 'finalize')
end

def mark_as_paid
Expand All @@ -70,12 +58,21 @@ def mark_as_paid
end

def send_by_email
self.class.request_pennylane_object(method: :post,
path: "/customer_invoices/#{id}/send_by_email",
params: {},
opts: {}, with: { invoice: 'customer_invoice' })
request_and_retrieve(method: :post, path: "/customer_invoices/#{id}", action: 'send_by_email')
end

private

# When API returns an empty body
# so we need to skip values assignment from the response
# GET /customer_invoices/:id again to get the updated values
def request_and_retrieve(method:, path:, action:)
self.class.request_pennylane_object(method: method,
path: "#{path}/#{action}",
params: {},
opts: {}, with: { invoice: 'customer_invoice' })
resp, opts = self.class.request_pennylane_object(method: :get,
path: "/customer_invoices/#{id}",
path: path,
params: {},
opts: {}, with: { invoice: 'customer_invoice' })
@values = resp.instance_variable_get :@values
Expand Down

0 comments on commit 60433c9

Please sign in to comment.