bongloy 4.21.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.circleci/config.yml +76 -0
- data/.editorconfig +10 -0
- data/.gitattributes +4 -0
- data/.github/ISSUE_TEMPLATE.md +5 -0
- data/.github/README.md +79 -0
- data/.github/pull.yml +5 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +43 -0
- data/.rubocop_todo.yml +38 -0
- data/.travis.yml +43 -0
- data/.vscode/extensions.json +7 -0
- data/.vscode/settings.json +8 -0
- data/CHANGELOG.md +770 -0
- data/CONTRIBUTORS +3 -0
- data/Gemfile +39 -0
- data/History.txt +1 -0
- data/LICENSE +21 -0
- data/README.md +282 -0
- data/Rakefile +36 -0
- data/VERSION +1 -0
- data/bin/stripe-console +16 -0
- data/bongloy.gemspec +37 -0
- data/lib/bongloy.rb +7 -0
- data/lib/data/ca-certificates.crt +4043 -0
- data/lib/stripe.rb +208 -0
- data/lib/stripe/api_operations/create.rb +12 -0
- data/lib/stripe/api_operations/delete.rb +35 -0
- data/lib/stripe/api_operations/list.rb +30 -0
- data/lib/stripe/api_operations/nested_resource.rb +70 -0
- data/lib/stripe/api_operations/request.rb +53 -0
- data/lib/stripe/api_operations/save.rb +94 -0
- data/lib/stripe/api_resource.rb +107 -0
- data/lib/stripe/errors.rb +156 -0
- data/lib/stripe/list_object.rb +110 -0
- data/lib/stripe/oauth.rb +63 -0
- data/lib/stripe/object_types.rb +98 -0
- data/lib/stripe/resources.rb +79 -0
- data/lib/stripe/resources/account.rb +174 -0
- data/lib/stripe/resources/account_link.rb +9 -0
- data/lib/stripe/resources/alipay_account.rb +34 -0
- data/lib/stripe/resources/apple_pay_domain.rb +16 -0
- data/lib/stripe/resources/application_fee.rb +24 -0
- data/lib/stripe/resources/application_fee_refund.rb +30 -0
- data/lib/stripe/resources/balance.rb +7 -0
- data/lib/stripe/resources/balance_transaction.rb +13 -0
- data/lib/stripe/resources/bank_account.rb +42 -0
- data/lib/stripe/resources/bitcoin_receiver.rb +23 -0
- data/lib/stripe/resources/bitcoin_transaction.rb +15 -0
- data/lib/stripe/resources/capability.rb +33 -0
- data/lib/stripe/resources/card.rb +37 -0
- data/lib/stripe/resources/charge.rb +84 -0
- data/lib/stripe/resources/checkout/session.rb +11 -0
- data/lib/stripe/resources/country_spec.rb +9 -0
- data/lib/stripe/resources/coupon.rb +12 -0
- data/lib/stripe/resources/credit_note.rb +18 -0
- data/lib/stripe/resources/customer.rb +95 -0
- data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
- data/lib/stripe/resources/discount.rb +7 -0
- data/lib/stripe/resources/dispute.rb +23 -0
- data/lib/stripe/resources/ephemeral_key.rb +19 -0
- data/lib/stripe/resources/event.rb +9 -0
- data/lib/stripe/resources/exchange_rate.rb +9 -0
- data/lib/stripe/resources/file.rb +44 -0
- data/lib/stripe/resources/file_link.rb +11 -0
- data/lib/stripe/resources/invoice.rb +48 -0
- data/lib/stripe/resources/invoice_item.rb +12 -0
- data/lib/stripe/resources/invoice_line_item.rb +7 -0
- data/lib/stripe/resources/issuer_fraud_record.rb +9 -0
- data/lib/stripe/resources/issuing/authorization.rb +25 -0
- data/lib/stripe/resources/issuing/card.rb +20 -0
- data/lib/stripe/resources/issuing/card_details.rb +9 -0
- data/lib/stripe/resources/issuing/cardholder.rb +13 -0
- data/lib/stripe/resources/issuing/dispute.rb +13 -0
- data/lib/stripe/resources/issuing/transaction.rb +12 -0
- data/lib/stripe/resources/login_link.rb +14 -0
- data/lib/stripe/resources/order.rb +32 -0
- data/lib/stripe/resources/order_return.rb +9 -0
- data/lib/stripe/resources/payment_intent.rb +30 -0
- data/lib/stripe/resources/payment_method.rb +24 -0
- data/lib/stripe/resources/payout.rb +24 -0
- data/lib/stripe/resources/person.rb +31 -0
- data/lib/stripe/resources/plan.rb +12 -0
- data/lib/stripe/resources/product.rb +12 -0
- data/lib/stripe/resources/radar/early_fraud_warning.rb +11 -0
- data/lib/stripe/resources/radar/value_list.rb +14 -0
- data/lib/stripe/resources/radar/value_list_item.rb +13 -0
- data/lib/stripe/resources/recipient.rb +17 -0
- data/lib/stripe/resources/recipient_transfer.rb +7 -0
- data/lib/stripe/resources/refund.rb +11 -0
- data/lib/stripe/resources/reporting/report_run.rb +12 -0
- data/lib/stripe/resources/reporting/report_type.rb +12 -0
- data/lib/stripe/resources/reversal.rb +29 -0
- data/lib/stripe/resources/review.rb +16 -0
- data/lib/stripe/resources/setup_intent.rb +24 -0
- data/lib/stripe/resources/sigma/scheduled_query_run.rb +15 -0
- data/lib/stripe/resources/sku.rb +12 -0
- data/lib/stripe/resources/source.rb +42 -0
- data/lib/stripe/resources/source_transaction.rb +7 -0
- data/lib/stripe/resources/subscription.rb +25 -0
- data/lib/stripe/resources/subscription_item.rb +17 -0
- data/lib/stripe/resources/subscription_schedule.rb +32 -0
- data/lib/stripe/resources/subscription_schedule_revision.rb +34 -0
- data/lib/stripe/resources/tax_id.rb +26 -0
- data/lib/stripe/resources/tax_rate.rb +11 -0
- data/lib/stripe/resources/terminal/connection_token.rb +11 -0
- data/lib/stripe/resources/terminal/location.rb +14 -0
- data/lib/stripe/resources/terminal/reader.rb +14 -0
- data/lib/stripe/resources/three_d_secure.rb +13 -0
- data/lib/stripe/resources/token.rb +9 -0
- data/lib/stripe/resources/topup.rb +18 -0
- data/lib/stripe/resources/transfer.rb +27 -0
- data/lib/stripe/resources/usage_record.rb +23 -0
- data/lib/stripe/resources/usage_record_summary.rb +7 -0
- data/lib/stripe/resources/webhook_endpoint.rb +12 -0
- data/lib/stripe/singleton_api_resource.rb +26 -0
- data/lib/stripe/stripe_client.rb +686 -0
- data/lib/stripe/stripe_object.rb +583 -0
- data/lib/stripe/stripe_response.rb +50 -0
- data/lib/stripe/util.rb +336 -0
- data/lib/stripe/version.rb +5 -0
- data/lib/stripe/webhook.rb +90 -0
- data/stripe.gemspec +37 -0
- data/test/api_stub_helpers.rb +1 -0
- data/test/openapi/README.md +9 -0
- data/test/stripe/account_link_test.rb +18 -0
- data/test/stripe/account_test.rb +428 -0
- data/test/stripe/alipay_account_test.rb +37 -0
- data/test/stripe/api_operations_test.rb +80 -0
- data/test/stripe/api_resource_test.rb +544 -0
- data/test/stripe/apple_pay_domain_test.rb +46 -0
- data/test/stripe/application_fee_refund_test.rb +37 -0
- data/test/stripe/application_fee_test.rb +58 -0
- data/test/stripe/balance_test.rb +13 -0
- data/test/stripe/bank_account_test.rb +36 -0
- data/test/stripe/capability_test.rb +45 -0
- data/test/stripe/charge_test.rb +80 -0
- data/test/stripe/checkout/session_test.rb +41 -0
- data/test/stripe/country_spec_test.rb +20 -0
- data/test/stripe/coupon_test.rb +61 -0
- data/test/stripe/credit_note_test.rb +61 -0
- data/test/stripe/customer_balance_transaction_test.rb +37 -0
- data/test/stripe/customer_card_test.rb +42 -0
- data/test/stripe/customer_test.rb +269 -0
- data/test/stripe/dispute_test.rb +51 -0
- data/test/stripe/ephemeral_key_test.rb +93 -0
- data/test/stripe/errors_test.rb +20 -0
- data/test/stripe/exchange_rate_test.rb +20 -0
- data/test/stripe/file_link_test.rb +41 -0
- data/test/stripe/file_test.rb +97 -0
- data/test/stripe/file_upload_test.rb +79 -0
- data/test/stripe/invoice_item_test.rb +66 -0
- data/test/stripe/invoice_line_item_test.rb +8 -0
- data/test/stripe/invoice_test.rb +213 -0
- data/test/stripe/issuer_fraud_record_test.rb +20 -0
- data/test/stripe/issuing/authorization_test.rb +72 -0
- data/test/stripe/issuing/card_test.rb +62 -0
- data/test/stripe/issuing/cardholder_test.rb +53 -0
- data/test/stripe/issuing/dispute_test.rb +45 -0
- data/test/stripe/issuing/transaction_test.rb +48 -0
- data/test/stripe/list_object_test.rb +156 -0
- data/test/stripe/login_link_test.rb +37 -0
- data/test/stripe/oauth_test.rb +88 -0
- data/test/stripe/order_return_test.rb +21 -0
- data/test/stripe/order_test.rb +82 -0
- data/test/stripe/payment_intent_test.rb +107 -0
- data/test/stripe/payment_method_test.rb +84 -0
- data/test/stripe/payout_test.rb +57 -0
- data/test/stripe/person_test.rb +46 -0
- data/test/stripe/plan_test.rb +98 -0
- data/test/stripe/product_test.rb +59 -0
- data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
- data/test/stripe/radar/value_list_item_test.rb +48 -0
- data/test/stripe/radar/value_list_test.rb +61 -0
- data/test/stripe/recipient_test.rb +62 -0
- data/test/stripe/refund_test.rb +39 -0
- data/test/stripe/reporting/report_run_test.rb +33 -0
- data/test/stripe/reporting/report_type_test.rb +22 -0
- data/test/stripe/reversal_test.rb +43 -0
- data/test/stripe/review_test.rb +27 -0
- data/test/stripe/setup_intent_test.rb +84 -0
- data/test/stripe/sigma/scheduled_query_run_test.rb +22 -0
- data/test/stripe/sku_test.rb +60 -0
- data/test/stripe/source_test.rb +99 -0
- data/test/stripe/source_transaction_test.rb +19 -0
- data/test/stripe/stripe_client_test.rb +842 -0
- data/test/stripe/stripe_object_test.rb +525 -0
- data/test/stripe/stripe_response_test.rb +49 -0
- data/test/stripe/subscription_item_test.rb +63 -0
- data/test/stripe/subscription_schedule_revision_test.rb +37 -0
- data/test/stripe/subscription_schedule_test.rb +116 -0
- data/test/stripe/subscription_test.rb +80 -0
- data/test/stripe/tax_id_test.rb +31 -0
- data/test/stripe/tax_rate_test.rb +43 -0
- data/test/stripe/terminal/connection_token_test.rb +16 -0
- data/test/stripe/terminal/location_test.rb +68 -0
- data/test/stripe/terminal/reader_test.rb +62 -0
- data/test/stripe/three_d_secure_test.rb +23 -0
- data/test/stripe/topup_test.rb +62 -0
- data/test/stripe/transfer_test.rb +88 -0
- data/test/stripe/usage_record_summary_test.rb +19 -0
- data/test/stripe/usage_record_test.rb +28 -0
- data/test/stripe/util_test.rb +402 -0
- data/test/stripe/webhook_endpoint_test.rb +59 -0
- data/test/stripe/webhook_test.rb +96 -0
- data/test/stripe_mock.rb +77 -0
- data/test/stripe_test.rb +63 -0
- data/test/test_data.rb +61 -0
- data/test/test_helper.rb +71 -0
- metadata +372 -0
@@ -0,0 +1,583 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Stripe
|
4
|
+
class StripeObject
|
5
|
+
include Enumerable
|
6
|
+
|
7
|
+
@@permanent_attributes = Set.new([:id]) # rubocop:disable Style/ClassVars
|
8
|
+
|
9
|
+
# The default :id method is deprecated and isn't useful to us
|
10
|
+
undef :id if method_defined?(:id)
|
11
|
+
|
12
|
+
# Sets the given parameter name to one which is known to be an additive
|
13
|
+
# object.
|
14
|
+
#
|
15
|
+
# Additive objects are subobjects in the API that don't have the same
|
16
|
+
# semantics as most subobjects, which are fully replaced when they're set.
|
17
|
+
# This is best illustrated by example. The `source` parameter sent when
|
18
|
+
# updating a subscription is *not* additive; if we set it:
|
19
|
+
#
|
20
|
+
# source[object]=card&source[number]=123
|
21
|
+
#
|
22
|
+
# We expect the old `source` object to have been overwritten completely. If
|
23
|
+
# the previous source had an `address_state` key associated with it and we
|
24
|
+
# didn't send one this time, that value of `address_state` is gone.
|
25
|
+
#
|
26
|
+
# By contrast, additive objects are those that will have new data added to
|
27
|
+
# them while keeping any existing data in place. The only known case of its
|
28
|
+
# use is for `metadata`, but it could in theory be more general. As an
|
29
|
+
# example, say we have a `metadata` object that looks like this on the
|
30
|
+
# server side:
|
31
|
+
#
|
32
|
+
# metadata = { old: "old_value" }
|
33
|
+
#
|
34
|
+
# If we update the object with `metadata[new]=new_value`, the server side
|
35
|
+
# object now has *both* fields:
|
36
|
+
#
|
37
|
+
# metadata = { old: "old_value", new: "new_value" }
|
38
|
+
#
|
39
|
+
# This is okay in itself because usually users will want to treat it as
|
40
|
+
# additive:
|
41
|
+
#
|
42
|
+
# obj.metadata[:new] = "new_value"
|
43
|
+
# obj.save
|
44
|
+
#
|
45
|
+
# However, in other cases, they may want to replace the entire existing
|
46
|
+
# contents:
|
47
|
+
#
|
48
|
+
# obj.metadata = { new: "new_value" }
|
49
|
+
# obj.save
|
50
|
+
#
|
51
|
+
# This is where things get a little bit tricky because in order to clear
|
52
|
+
# any old keys that may have existed, we actually have to send an explicit
|
53
|
+
# empty string to the server. So the operation above would have to send
|
54
|
+
# this form to get the intended behavior:
|
55
|
+
#
|
56
|
+
# metadata[old]=&metadata[new]=new_value
|
57
|
+
#
|
58
|
+
# This method allows us to track which parameters are considered additive,
|
59
|
+
# and lets us behave correctly where appropriate when serializing
|
60
|
+
# parameters to be sent.
|
61
|
+
def self.additive_object_param(name)
|
62
|
+
@additive_params ||= Set.new
|
63
|
+
@additive_params << name
|
64
|
+
end
|
65
|
+
|
66
|
+
# Returns whether the given name is an additive object parameter. See
|
67
|
+
# `.additive_object_param` for details.
|
68
|
+
def self.additive_object_param?(name)
|
69
|
+
@additive_params ||= Set.new
|
70
|
+
@additive_params.include?(name)
|
71
|
+
end
|
72
|
+
|
73
|
+
def initialize(id = nil, opts = {})
|
74
|
+
id, @retrieve_params = Util.normalize_id(id)
|
75
|
+
@opts = Util.normalize_opts(opts)
|
76
|
+
@original_values = {}
|
77
|
+
@values = {}
|
78
|
+
# This really belongs in APIResource, but not putting it there allows us
|
79
|
+
# to have a unified inspect method
|
80
|
+
@unsaved_values = Set.new
|
81
|
+
@transient_values = Set.new
|
82
|
+
@values[:id] = id if id
|
83
|
+
end
|
84
|
+
|
85
|
+
def self.construct_from(values, opts = {})
|
86
|
+
values = Stripe::Util.symbolize_names(values)
|
87
|
+
|
88
|
+
# work around protected #initialize_from for now
|
89
|
+
new(values[:id]).send(:initialize_from, values, opts)
|
90
|
+
end
|
91
|
+
|
92
|
+
# Determines the equality of two Stripe objects. Stripe objects are
|
93
|
+
# considered to be equal if they have the same set of values and each one
|
94
|
+
# of those values is the same.
|
95
|
+
def ==(other)
|
96
|
+
other.is_a?(StripeObject) &&
|
97
|
+
@values == other.instance_variable_get(:@values)
|
98
|
+
end
|
99
|
+
|
100
|
+
# Hash equality. As with `#==`, we consider two equivalent Stripe objects
|
101
|
+
# equal.
|
102
|
+
def eql?(other)
|
103
|
+
# Defer to the implementation on `#==`.
|
104
|
+
self == other
|
105
|
+
end
|
106
|
+
|
107
|
+
# As with equality in `#==` and `#eql?`, we hash two Stripe objects to the
|
108
|
+
# same value if they're equivalent objects.
|
109
|
+
def hash
|
110
|
+
@values.hash
|
111
|
+
end
|
112
|
+
|
113
|
+
# Indicates whether or not the resource has been deleted on the server.
|
114
|
+
# Note that some, but not all, resources can indicate whether they have
|
115
|
+
# been deleted.
|
116
|
+
def deleted?
|
117
|
+
@values.fetch(:deleted, false)
|
118
|
+
end
|
119
|
+
|
120
|
+
def to_s(*_args)
|
121
|
+
JSON.pretty_generate(to_hash)
|
122
|
+
end
|
123
|
+
|
124
|
+
def inspect
|
125
|
+
id_string = respond_to?(:id) && !id.nil? ? " id=#{id}" : ""
|
126
|
+
"#<#{self.class}:0x#{object_id.to_s(16)}#{id_string}> JSON: " +
|
127
|
+
JSON.pretty_generate(@values)
|
128
|
+
end
|
129
|
+
|
130
|
+
# Re-initializes the object based on a hash of values (usually one that's
|
131
|
+
# come back from an API call). Adds or removes value accessors as necessary
|
132
|
+
# and updates the state of internal data.
|
133
|
+
#
|
134
|
+
# Please don't use this method. If you're trying to do mass assignment, try
|
135
|
+
# #initialize_from instead.
|
136
|
+
def refresh_from(values, opts, partial = false)
|
137
|
+
initialize_from(values, opts, partial)
|
138
|
+
end
|
139
|
+
extend Gem::Deprecate
|
140
|
+
deprecate :refresh_from, "#update_attributes", 2016, 1
|
141
|
+
|
142
|
+
# Mass assigns attributes on the model.
|
143
|
+
#
|
144
|
+
# This is a version of +update_attributes+ that takes some extra options
|
145
|
+
# for internal use.
|
146
|
+
#
|
147
|
+
# ==== Attributes
|
148
|
+
#
|
149
|
+
# * +values+ - Hash of values to use to update the current attributes of
|
150
|
+
# the object.
|
151
|
+
# * +opts+ - Options for +StripeObject+ like an API key that will be reused
|
152
|
+
# on subsequent API calls.
|
153
|
+
#
|
154
|
+
# ==== Options
|
155
|
+
#
|
156
|
+
# * +:dirty+ - Whether values should be initiated as "dirty" (unsaved) and
|
157
|
+
# which applies only to new StripeObjects being initiated under this
|
158
|
+
# StripeObject. Defaults to true.
|
159
|
+
def update_attributes(values, opts = {}, dirty: true)
|
160
|
+
values.each do |k, v|
|
161
|
+
add_accessors([k], values) unless metaclass.method_defined?(k.to_sym)
|
162
|
+
@values[k] = Util.convert_to_stripe_object(v, opts)
|
163
|
+
dirty_value!(@values[k]) if dirty
|
164
|
+
@unsaved_values.add(k)
|
165
|
+
end
|
166
|
+
end
|
167
|
+
|
168
|
+
def [](key)
|
169
|
+
@values[key.to_sym]
|
170
|
+
end
|
171
|
+
|
172
|
+
def []=(key, value)
|
173
|
+
send(:"#{key}=", value)
|
174
|
+
end
|
175
|
+
|
176
|
+
def keys
|
177
|
+
@values.keys
|
178
|
+
end
|
179
|
+
|
180
|
+
def values
|
181
|
+
@values.values
|
182
|
+
end
|
183
|
+
|
184
|
+
def to_json(*_opts)
|
185
|
+
# TODO: pass opts to JSON.generate?
|
186
|
+
JSON.generate(@values)
|
187
|
+
end
|
188
|
+
|
189
|
+
def as_json(*opts)
|
190
|
+
@values.as_json(*opts)
|
191
|
+
end
|
192
|
+
|
193
|
+
def to_hash
|
194
|
+
maybe_to_hash = lambda do |value|
|
195
|
+
value && value.respond_to?(:to_hash) ? value.to_hash : value
|
196
|
+
end
|
197
|
+
|
198
|
+
@values.each_with_object({}) do |(key, value), acc|
|
199
|
+
acc[key] = case value
|
200
|
+
when Array
|
201
|
+
value.map(&maybe_to_hash)
|
202
|
+
else
|
203
|
+
maybe_to_hash.call(value)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
end
|
207
|
+
|
208
|
+
def each(&blk)
|
209
|
+
@values.each(&blk)
|
210
|
+
end
|
211
|
+
|
212
|
+
# Sets all keys within the StripeObject as unsaved so that they will be
|
213
|
+
# included with an update when #serialize_params is called. This method is
|
214
|
+
# also recursive, so any StripeObjects contained as values or which are
|
215
|
+
# values in a tenant array are also marked as dirty.
|
216
|
+
def dirty!
|
217
|
+
@unsaved_values = Set.new(@values.keys)
|
218
|
+
@values.each_value do |v|
|
219
|
+
dirty_value!(v)
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
# Implements custom encoding for Ruby's Marshal. The data produced by this
|
224
|
+
# method should be comprehendable by #marshal_load.
|
225
|
+
#
|
226
|
+
# This allows us to remove certain features that cannot or should not be
|
227
|
+
# serialized.
|
228
|
+
def marshal_dump
|
229
|
+
# The StripeClient instance in @opts is not serializable and is not
|
230
|
+
# really a property of the StripeObject, so we exclude it when
|
231
|
+
# dumping
|
232
|
+
opts = @opts.clone
|
233
|
+
opts.delete(:client)
|
234
|
+
[@values, opts]
|
235
|
+
end
|
236
|
+
|
237
|
+
# Implements custom decoding for Ruby's Marshal. Consumes data that's
|
238
|
+
# produced by #marshal_dump.
|
239
|
+
def marshal_load(data)
|
240
|
+
values, opts = data
|
241
|
+
initialize(values[:id])
|
242
|
+
initialize_from(values, opts)
|
243
|
+
end
|
244
|
+
|
245
|
+
def serialize_params(options = {})
|
246
|
+
update_hash = {}
|
247
|
+
|
248
|
+
@values.each do |k, v|
|
249
|
+
# There are a few reasons that we may want to add in a parameter for
|
250
|
+
# update:
|
251
|
+
#
|
252
|
+
# 1. The `force` option has been set.
|
253
|
+
# 2. We know that it was modified.
|
254
|
+
# 3. Its value is a StripeObject. A StripeObject may contain modified
|
255
|
+
# values within in that its parent StripeObject doesn't know about.
|
256
|
+
#
|
257
|
+
unsaved = @unsaved_values.include?(k)
|
258
|
+
next unless options[:force] || unsaved || v.is_a?(StripeObject)
|
259
|
+
update_hash[k.to_sym] = serialize_params_value(
|
260
|
+
@values[k], @original_values[k], unsaved, options[:force], key: k
|
261
|
+
)
|
262
|
+
end
|
263
|
+
|
264
|
+
# a `nil` that makes it out of `#serialize_params_value` signals an empty
|
265
|
+
# value that we shouldn't appear in the serialized form of the object
|
266
|
+
update_hash.reject! { |_, v| v.nil? }
|
267
|
+
|
268
|
+
update_hash
|
269
|
+
end
|
270
|
+
|
271
|
+
class << self
|
272
|
+
# This class method has been deprecated in favor of the instance method
|
273
|
+
# of the same name.
|
274
|
+
def serialize_params(obj, options = {})
|
275
|
+
obj.serialize_params(options)
|
276
|
+
end
|
277
|
+
extend Gem::Deprecate
|
278
|
+
deprecate :serialize_params, "#serialize_params", 2016, 9
|
279
|
+
end
|
280
|
+
|
281
|
+
# A protected field is one that doesn't get an accessor assigned to it
|
282
|
+
# (i.e. `obj.public = ...`) and one which is not allowed to be updated via
|
283
|
+
# the class level `Model.update(id, { ... })`.
|
284
|
+
def self.protected_fields
|
285
|
+
[]
|
286
|
+
end
|
287
|
+
|
288
|
+
protected def metaclass
|
289
|
+
class << self; self; end
|
290
|
+
end
|
291
|
+
|
292
|
+
protected def remove_accessors(keys)
|
293
|
+
# not available in the #instance_eval below
|
294
|
+
protected_fields = self.class.protected_fields
|
295
|
+
|
296
|
+
metaclass.instance_eval do
|
297
|
+
keys.each do |k|
|
298
|
+
next if protected_fields.include?(k)
|
299
|
+
next if @@permanent_attributes.include?(k)
|
300
|
+
|
301
|
+
# Remove methods for the accessor's reader and writer.
|
302
|
+
[k, :"#{k}=", :"#{k}?"].each do |method_name|
|
303
|
+
next unless method_defined?(method_name)
|
304
|
+
|
305
|
+
begin
|
306
|
+
remove_method(method_name)
|
307
|
+
rescue NameError
|
308
|
+
# In some cases there can be a method that's detected with
|
309
|
+
# `method_defined?`, but which cannot be removed with
|
310
|
+
# `remove_method`, even though it's on the same class. The only
|
311
|
+
# case so far that we've noticed this is when a class is
|
312
|
+
# reopened for monkey patching:
|
313
|
+
#
|
314
|
+
# https://github.com/stripe/stripe-ruby/issues/749
|
315
|
+
#
|
316
|
+
# Here we swallow that error and issue a warning so at least
|
317
|
+
# the program doesn't crash.
|
318
|
+
warn("WARNING: Unable to remove method `#{method_name}`; " \
|
319
|
+
"if custom, please consider renaming to a name that doesn't " \
|
320
|
+
"collide with an API property name.")
|
321
|
+
end
|
322
|
+
end
|
323
|
+
end
|
324
|
+
end
|
325
|
+
end
|
326
|
+
|
327
|
+
protected def add_accessors(keys, values)
|
328
|
+
# not available in the #instance_eval below
|
329
|
+
protected_fields = self.class.protected_fields
|
330
|
+
|
331
|
+
metaclass.instance_eval do
|
332
|
+
keys.each do |k|
|
333
|
+
next if protected_fields.include?(k)
|
334
|
+
next if @@permanent_attributes.include?(k)
|
335
|
+
|
336
|
+
if k == :method
|
337
|
+
# Object#method is a built-in Ruby method that accepts a symbol
|
338
|
+
# and returns the corresponding Method object. Because the API may
|
339
|
+
# also use `method` as a field name, we check the arity of *args
|
340
|
+
# to decide whether to act as a getter or call the parent method.
|
341
|
+
define_method(k) { |*args| args.empty? ? @values[k] : super(*args) }
|
342
|
+
else
|
343
|
+
define_method(k) { @values[k] }
|
344
|
+
end
|
345
|
+
|
346
|
+
define_method(:"#{k}=") do |v|
|
347
|
+
if v == ""
|
348
|
+
raise ArgumentError, "You cannot set #{k} to an empty string. " \
|
349
|
+
"We interpret empty strings as nil in requests. " \
|
350
|
+
"You may set (object).#{k} = nil to delete the property."
|
351
|
+
end
|
352
|
+
@values[k] = Util.convert_to_stripe_object(v, @opts)
|
353
|
+
dirty_value!(@values[k])
|
354
|
+
@unsaved_values.add(k)
|
355
|
+
end
|
356
|
+
|
357
|
+
if [FalseClass, TrueClass].include?(values[k].class)
|
358
|
+
define_method(:"#{k}?") { @values[k] }
|
359
|
+
end
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
# Disabling the cop because it's confused by the fact that the methods are
|
365
|
+
# protected, but we do define `#respond_to_missing?` just below. Hopefully
|
366
|
+
# this is fixed in more recent Rubocop versions.
|
367
|
+
# rubocop:disable Style/MissingRespondToMissing
|
368
|
+
protected def method_missing(name, *args)
|
369
|
+
# TODO: only allow setting in updateable classes.
|
370
|
+
if name.to_s.end_with?("=")
|
371
|
+
attr = name.to_s[0...-1].to_sym
|
372
|
+
|
373
|
+
# Pull out the assigned value. This is only used in the case of a
|
374
|
+
# boolean value to add a question mark accessor (i.e. `foo?`) for
|
375
|
+
# convenience.
|
376
|
+
val = args.first
|
377
|
+
|
378
|
+
# the second argument is only required when adding boolean accessors
|
379
|
+
add_accessors([attr], attr => val)
|
380
|
+
|
381
|
+
begin
|
382
|
+
mth = method(name)
|
383
|
+
rescue NameError
|
384
|
+
raise NoMethodError,
|
385
|
+
"Cannot set #{attr} on this object. HINT: you can't set: " \
|
386
|
+
"#{@@permanent_attributes.to_a.join(', ')}"
|
387
|
+
end
|
388
|
+
return mth.call(args[0])
|
389
|
+
elsif @values.key?(name)
|
390
|
+
return @values[name]
|
391
|
+
end
|
392
|
+
|
393
|
+
begin
|
394
|
+
super
|
395
|
+
rescue NoMethodError => e
|
396
|
+
# If we notice the accessed name if our set of transient values we can
|
397
|
+
# give the user a slightly more helpful error message. If not, just
|
398
|
+
# raise right away.
|
399
|
+
raise unless @transient_values.include?(name)
|
400
|
+
|
401
|
+
raise NoMethodError,
|
402
|
+
e.message + ". HINT: The '#{name}' attribute was set in the " \
|
403
|
+
"past, however. It was then wiped when refreshing the object " \
|
404
|
+
"with the result returned by Stripe's API, probably as a " \
|
405
|
+
"result of a save(). The attributes currently available on " \
|
406
|
+
"this object are: #{@values.keys.join(', ')}"
|
407
|
+
end
|
408
|
+
end
|
409
|
+
# rubocop:enable Style/MissingRespondToMissing
|
410
|
+
|
411
|
+
protected def respond_to_missing?(symbol, include_private = false)
|
412
|
+
@values && @values.key?(symbol) || super
|
413
|
+
end
|
414
|
+
|
415
|
+
# Re-initializes the object based on a hash of values (usually one that's
|
416
|
+
# come back from an API call). Adds or removes value accessors as necessary
|
417
|
+
# and updates the state of internal data.
|
418
|
+
#
|
419
|
+
# Protected on purpose! Please do not expose.
|
420
|
+
#
|
421
|
+
# ==== Options
|
422
|
+
#
|
423
|
+
# * +:values:+ Hash used to update accessors and values.
|
424
|
+
# * +:opts:+ Options for StripeObject like an API key.
|
425
|
+
# * +:partial:+ Indicates that the re-initialization should not attempt to
|
426
|
+
# remove accessors.
|
427
|
+
protected def initialize_from(values, opts, partial = false)
|
428
|
+
@opts = Util.normalize_opts(opts)
|
429
|
+
|
430
|
+
# the `#send` is here so that we can keep this method private
|
431
|
+
@original_values = self.class.send(:deep_copy, values)
|
432
|
+
|
433
|
+
removed = partial ? Set.new : Set.new(@values.keys - values.keys)
|
434
|
+
added = Set.new(values.keys - @values.keys)
|
435
|
+
|
436
|
+
# Wipe old state before setting new. This is useful for e.g. updating a
|
437
|
+
# customer, where there is no persistent card parameter. Mark those
|
438
|
+
# values which don't persist as transient
|
439
|
+
|
440
|
+
remove_accessors(removed)
|
441
|
+
add_accessors(added, values)
|
442
|
+
|
443
|
+
removed.each do |k|
|
444
|
+
@values.delete(k)
|
445
|
+
@transient_values.add(k)
|
446
|
+
@unsaved_values.delete(k)
|
447
|
+
end
|
448
|
+
|
449
|
+
update_attributes(values, opts, dirty: false)
|
450
|
+
values.each_key do |k|
|
451
|
+
@transient_values.delete(k)
|
452
|
+
@unsaved_values.delete(k)
|
453
|
+
end
|
454
|
+
|
455
|
+
self
|
456
|
+
end
|
457
|
+
|
458
|
+
protected def serialize_params_value(value, original, unsaved, force,
|
459
|
+
key: nil)
|
460
|
+
if value.nil?
|
461
|
+
""
|
462
|
+
|
463
|
+
# The logic here is that essentially any object embedded in another
|
464
|
+
# object that had a `type` is actually an API resource of a different
|
465
|
+
# type that's been included in the response. These other resources must
|
466
|
+
# be updated from their proper endpoints, and therefore they are not
|
467
|
+
# included when serializing even if they've been modified.
|
468
|
+
#
|
469
|
+
# There are _some_ known exceptions though.
|
470
|
+
#
|
471
|
+
# For example, if the value is unsaved (meaning the user has set it), and
|
472
|
+
# it looks like the API resource is persisted with an ID, then we include
|
473
|
+
# the object so that parameters are serialized with a reference to its
|
474
|
+
# ID.
|
475
|
+
#
|
476
|
+
# Another example is that on save API calls it's sometimes desirable to
|
477
|
+
# update a customer's default source by setting a new card (or other)
|
478
|
+
# object with `#source=` and then saving the customer. The
|
479
|
+
# `#save_with_parent` flag to override the default behavior allows us to
|
480
|
+
# handle these exceptions.
|
481
|
+
#
|
482
|
+
# We throw an error if a property was set explicitly but we can't do
|
483
|
+
# anything with it because the integration is probably not working as the
|
484
|
+
# user intended it to.
|
485
|
+
elsif value.is_a?(APIResource) && !value.save_with_parent
|
486
|
+
if !unsaved
|
487
|
+
nil
|
488
|
+
elsif value.respond_to?(:id) && !value.id.nil?
|
489
|
+
value
|
490
|
+
else
|
491
|
+
raise ArgumentError, "Cannot save property `#{key}` containing " \
|
492
|
+
"an API resource. It doesn't appear to be persisted and is " \
|
493
|
+
"not marked as `save_with_parent`."
|
494
|
+
end
|
495
|
+
|
496
|
+
elsif value.is_a?(Array)
|
497
|
+
update = value.map { |v| serialize_params_value(v, nil, true, force) }
|
498
|
+
|
499
|
+
# This prevents an array that's unchanged from being resent.
|
500
|
+
update if update != serialize_params_value(original, nil, true, force)
|
501
|
+
|
502
|
+
# Handle a Hash for now, but in the long run we should be able to
|
503
|
+
# eliminate all places where hashes are stored as values internally by
|
504
|
+
# making sure any time one is set, we convert it to a StripeObject. This
|
505
|
+
# will simplify our model by making data within an object more
|
506
|
+
# consistent.
|
507
|
+
#
|
508
|
+
# For now, you can still run into a hash if someone appends one to an
|
509
|
+
# existing array being held by a StripeObject. This could happen for
|
510
|
+
# example by appending a new hash onto `additional_owners` for an
|
511
|
+
# account.
|
512
|
+
elsif value.is_a?(Hash)
|
513
|
+
Util.convert_to_stripe_object(value, @opts).serialize_params
|
514
|
+
|
515
|
+
elsif value.is_a?(StripeObject)
|
516
|
+
update = value.serialize_params(force: force)
|
517
|
+
|
518
|
+
# If the entire object was replaced and this is an additive object,
|
519
|
+
# then we need blank each field of the old object that held a value
|
520
|
+
# because otherwise the update to the keys of the object will be
|
521
|
+
# additive instead of a full replacement. The new serialized values
|
522
|
+
# will override any of these empty values.
|
523
|
+
if original && unsaved && key && self.class.additive_object_param?(key)
|
524
|
+
update = empty_values(original).merge(update)
|
525
|
+
end
|
526
|
+
|
527
|
+
update
|
528
|
+
|
529
|
+
else
|
530
|
+
value
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
# Produces a deep copy of the given object including support for arrays,
|
535
|
+
# hashes, and StripeObjects.
|
536
|
+
private_class_method def self.deep_copy(obj)
|
537
|
+
case obj
|
538
|
+
when Array
|
539
|
+
obj.map { |e| deep_copy(e) }
|
540
|
+
when Hash
|
541
|
+
obj.each_with_object({}) do |(k, v), copy|
|
542
|
+
copy[k] = deep_copy(v)
|
543
|
+
copy
|
544
|
+
end
|
545
|
+
when StripeObject
|
546
|
+
obj.class.construct_from(
|
547
|
+
deep_copy(obj.instance_variable_get(:@values)),
|
548
|
+
obj.instance_variable_get(:@opts).select do |k, _v|
|
549
|
+
Util::OPTS_COPYABLE.include?(k)
|
550
|
+
end
|
551
|
+
)
|
552
|
+
else
|
553
|
+
obj
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
private def dirty_value!(value)
|
558
|
+
case value
|
559
|
+
when Array
|
560
|
+
value.map { |v| dirty_value!(v) }
|
561
|
+
when StripeObject
|
562
|
+
value.dirty!
|
563
|
+
end
|
564
|
+
end
|
565
|
+
|
566
|
+
# Returns a hash of empty values for all the values that are in the given
|
567
|
+
# StripeObject.
|
568
|
+
private def empty_values(obj)
|
569
|
+
values = case obj
|
570
|
+
when Hash then obj
|
571
|
+
when StripeObject then obj.instance_variable_get(:@values)
|
572
|
+
else
|
573
|
+
raise ArgumentError,
|
574
|
+
"#empty_values got unexpected object type: " \
|
575
|
+
"#{obj.class.name}"
|
576
|
+
end
|
577
|
+
|
578
|
+
values.each_with_object({}) do |(k, _), update|
|
579
|
+
update[k] = ""
|
580
|
+
end
|
581
|
+
end
|
582
|
+
end
|
583
|
+
end
|