Skip to content

Commit

Permalink
fix: implement basic present? function
Browse files Browse the repository at this point in the history
  • Loading branch information
tferrerm committed Aug 20, 2024
1 parent 8cd5b82 commit c0e3a4d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
6 changes: 5 additions & 1 deletion lib/stripe_mock/request_handlers/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,10 +186,14 @@ def create_balance_transaction(route, method_url, params, headers)

private

def present?(obj)
obj && obj != {} && obj != []
end

def return_customer(customer, params)
customer = customer.clone

if params[:expand].present?
if present?(params[:expand])
[params[:expand]].flatten.each do |field|
case field
when 'default_source'
Expand Down
16 changes: 10 additions & 6 deletions lib/stripe_mock/request_handlers/invoices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def pay_invoice(route, method_url, params, headers)
:charge => charge[:id],
)

recurring_items = invoices[$1][:lines][:data].select { |item| item[:price] && get_price(nil, nil, { price: item[:price] }, nil)[:recurring].present? }
recurring_items = invoices[$1][:lines][:data].select { |item| item[:price] && present?(get_price(nil, nil, { price: item[:price] }, nil)[:recurring]) }
if recurring_items.any?
invoices[$1][:subscription] = create_subscription(nil, nil, { customer: invoices[$1][:customer], items: recurring_items.map { |item| { price: item[:price], quantity: item[:quantity] } } }, {})[:id]
end
Expand Down Expand Up @@ -217,20 +217,24 @@ def upcoming_invoice(route, method_url, params, headers = {})

private

def present?(obj)
obj && obj != {} && obj != []
end

def return_invoice(invoice, params)
inv = invoice.clone

if params[:expand].present?
if present?(params[:expand])
[params[:expand]].flatten.each do |field|
case field
when 'customer'
inv[:customer] = get_customer(nil, nil, {customer: inv[:customer]}, nil) if inv[:customer].present?
inv[:customer] = get_customer(nil, nil, {customer: inv[:customer]}, nil) if present?(inv[:customer])
when 'charge'
inv[:charge] = get_charge(nil, nil, {charge: inv[:charge]}, nil) if inv[:charge].present?
inv[:charge] = get_charge(nil, nil, {charge: inv[:charge]}, nil) if present?(inv[:charge])
when 'subscription'
inv[:subscription] = retrieve_subscription(nil, nil, {subscription: inv[:subscription]}, nil) if inv[:subscription].present?
inv[:subscription] = retrieve_subscription(nil, nil, {subscription: inv[:subscription]}, nil) if present?(inv[:subscription])
when 'payment_intent'
inv[:payment_intent] = get_payment_intent(nil, nil, {payment_intent: inv[:payment_intent]}, nil) if inv[:payment_intent].present?
inv[:payment_intent] = get_payment_intent(nil, nil, {payment_intent: inv[:payment_intent]}, nil) if present?(inv[:payment_intent])
end
end
end
Expand Down

0 comments on commit c0e3a4d

Please sign in to comment.