From 60433c92812f17f5ebd1bca50331f444827b31cd Mon Sep 17 00:00:00 2001 From: Stephane Bounmy <159814+sbounmy@users.noreply.github.com> Date: Thu, 25 Apr 2024 15:52:14 +0200 Subject: [PATCH] refactored when request and retrieve same resource --- lib/pennylane/resources/customer_invoice.rb | 33 ++++++++++----------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/pennylane/resources/customer_invoice.rb b/lib/pennylane/resources/customer_invoice.rb index c8f69f9..b346291 100644 --- a/lib/pennylane/resources/customer_invoice.rb +++ b/lib/pennylane/resources/customer_invoice.rb @@ -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 @@ -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