Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify request/response handling #808

Merged
merged 6 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions lib/stripe/api_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,16 @@ def self.retrieve(id, opts = {})
instance.refresh
instance
end

protected def request_stripe_object(method:, path:, params:, opts: {})
resp, opts = request(method, path, params, opts)

# If we're getting back this thing, update; otherwise, instantiate.
if Util.object_name_matches_class?(resp.data[:object], self.class)
initialize_from(resp.data, opts)
else
Util.convert_to_stripe_object(resp.data, opts)
end
end
end
end
8 changes: 6 additions & 2 deletions lib/stripe/resources/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@ class Account < APIResource
operations: %i[create retrieve update delete list]

def reject(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/reject", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/reject",
params: params,
opts: opts
)
end

save_nested_resource :external_account
Expand Down
8 changes: 6 additions & 2 deletions lib/stripe/resources/credit_note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class CreditNote < APIResource
custom_method :void_credit_note, http_verb: :post, http_path: "void"

def void_credit_note(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/void", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/void",
params: params,
opts: opts
)
end
end
end
8 changes: 6 additions & 2 deletions lib/stripe/resources/dispute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ class Dispute < APIResource
custom_method :close, http_verb: :post

def close(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/close", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/close",
params: params,
opts: opts
)
end

def close_url
Expand Down
40 changes: 30 additions & 10 deletions lib/stripe/resources/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,48 @@ class Invoice < APIResource
custom_method :void_invoice, http_verb: :post, http_path: "void"

def finalize_invoice(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/finalize", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/finalize",
params: params,
opts: opts
)
end

def mark_uncollectible(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/mark_uncollectible", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/mark_uncollectible",
params: params,
opts: opts
)
end

def pay(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/pay", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/pay",
params: params,
opts: opts
)
end

def send_invoice(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/send", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/send",
params: params,
opts: opts
)
end

def void_invoice(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/void", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/void",
params: params,
opts: opts
)
end

def self.upcoming(params, opts = {})
Expand Down
16 changes: 12 additions & 4 deletions lib/stripe/resources/issuing/authorization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class Authorization < APIResource
custom_method :decline, http_verb: :post

def approve(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/approve", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/approve",
params: params,
opts: opts
)
end

def decline(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/decline", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/decline",
params: params,
opts: opts
)
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/stripe/resources/issuing/card.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ class Card < APIResource
custom_method :details, http_verb: :get

def details(params = {}, opts = {})
resp, opts = request(:get, resource_url + "/details", params, opts)
Util.convert_to_stripe_object(resp.data, opts)
request_stripe_object(
method: :get,
path: resource_url + "/details",
params: params,
opts: opts
)
end
end
end
Expand Down
16 changes: 12 additions & 4 deletions lib/stripe/resources/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class Order < APIResource
custom_method :return_order, http_verb: :post, http_path: "returns"

def pay(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/pay", params, opts)
Util.convert_to_stripe_object(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/pay",
params: params,
opts: opts
)
end

def return_order(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/returns", params, opts)
Util.convert_to_stripe_object(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/returns",
params: params,
opts: opts
)
end

private def pay_url
Expand Down
24 changes: 18 additions & 6 deletions lib/stripe/resources/payment_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ class PaymentIntent < APIResource
custom_method :confirm, http_verb: :post

def cancel(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/cancel", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/cancel",
params: params,
opts: opts
)
end

def capture(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/capture", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/capture",
params: params,
opts: opts
)
end

def confirm(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/confirm", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/confirm",
params: params,
opts: opts
)
end
end
end
16 changes: 12 additions & 4 deletions lib/stripe/resources/payment_method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class PaymentMethod < APIResource
custom_method :detach, http_verb: :post

def attach(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/attach", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/attach",
params: params,
opts: opts
)
end

def detach(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/detach", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/detach",
params: params,
opts: opts
)
end
end
end
8 changes: 6 additions & 2 deletions lib/stripe/resources/payout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class Payout < APIResource
custom_method :cancel, http_verb: :post

def cancel(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/cancel", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/cancel",
params: params,
opts: opts
)
end

def cancel_url
Expand Down
8 changes: 6 additions & 2 deletions lib/stripe/resources/review.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ class Review < APIResource
custom_method :approve, http_verb: :post

def approve(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/approve", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/approve",
params: params,
opts: opts
)
end
end
end
16 changes: 12 additions & 4 deletions lib/stripe/resources/setup_intent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,21 @@ class SetupIntent < APIResource
custom_method :confirm, http_verb: :post

def cancel(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/cancel", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/cancel",
params: params,
opts: opts
)
end

def confirm(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/confirm", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/confirm",
params: params,
opts: opts
)
end
end
end
8 changes: 6 additions & 2 deletions lib/stripe/resources/source.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ class Source < APIResource
custom_method :verify, http_verb: :post

def verify(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/verify", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/verify",
params: params,
opts: opts
)
end

def detach(params = {}, opts = {})
Expand Down
16 changes: 12 additions & 4 deletions lib/stripe/resources/subscription_schedule.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ class SubscriptionSchedule < APIResource
nested_resource_class_methods :revision, operations: %i[retrieve list]

def cancel(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/cancel", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/cancel",
params: params,
opts: opts
)
end

def release(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/release", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/release",
params: params,
opts: opts
)
end

def revisions(params = {}, opts = {})
Expand Down
8 changes: 6 additions & 2 deletions lib/stripe/resources/topup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ class Topup < APIResource
custom_method :cancel, http_verb: :post

def cancel(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/cancel", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/cancel",
params: params,
opts: opts
)
end
end
end
8 changes: 6 additions & 2 deletions lib/stripe/resources/transfer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ class Transfer < APIResource
operations: %i[create retrieve update list]

def cancel(params = {}, opts = {})
resp, opts = request(:post, resource_url + "/cancel", params, opts)
initialize_from(resp.data, opts)
request_stripe_object(
method: :post,
path: resource_url + "/cancel",
params: params,
opts: opts
)
end

def cancel_url
Expand Down
4 changes: 4 additions & 0 deletions lib/stripe/util.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ def self.object_classes
@object_classes ||= Stripe::ObjectTypes.object_names_to_classes
end

def self.object_name_matches_class?(object_name, klass)
Util.object_classes[object_name] == klass
end

# Converts a hash of fields or an array of hashes into a +StripeObject+ or
# array of +StripeObject+s. These new objects will be created as a concrete
# type as dictated by their `object` field (e.g. an `object` value of
Expand Down
Loading