bongloy 4.21.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. checksums.yaml +7 -0
  2. data/.circleci/config.yml +76 -0
  3. data/.editorconfig +10 -0
  4. data/.gitattributes +4 -0
  5. data/.github/ISSUE_TEMPLATE.md +5 -0
  6. data/.github/README.md +79 -0
  7. data/.github/pull.yml +5 -0
  8. data/.gitignore +8 -0
  9. data/.rubocop.yml +43 -0
  10. data/.rubocop_todo.yml +38 -0
  11. data/.travis.yml +43 -0
  12. data/.vscode/extensions.json +7 -0
  13. data/.vscode/settings.json +8 -0
  14. data/CHANGELOG.md +770 -0
  15. data/CONTRIBUTORS +3 -0
  16. data/Gemfile +39 -0
  17. data/History.txt +1 -0
  18. data/LICENSE +21 -0
  19. data/README.md +282 -0
  20. data/Rakefile +36 -0
  21. data/VERSION +1 -0
  22. data/bin/stripe-console +16 -0
  23. data/bongloy.gemspec +37 -0
  24. data/lib/bongloy.rb +7 -0
  25. data/lib/data/ca-certificates.crt +4043 -0
  26. data/lib/stripe.rb +208 -0
  27. data/lib/stripe/api_operations/create.rb +12 -0
  28. data/lib/stripe/api_operations/delete.rb +35 -0
  29. data/lib/stripe/api_operations/list.rb +30 -0
  30. data/lib/stripe/api_operations/nested_resource.rb +70 -0
  31. data/lib/stripe/api_operations/request.rb +53 -0
  32. data/lib/stripe/api_operations/save.rb +94 -0
  33. data/lib/stripe/api_resource.rb +107 -0
  34. data/lib/stripe/errors.rb +156 -0
  35. data/lib/stripe/list_object.rb +110 -0
  36. data/lib/stripe/oauth.rb +63 -0
  37. data/lib/stripe/object_types.rb +98 -0
  38. data/lib/stripe/resources.rb +79 -0
  39. data/lib/stripe/resources/account.rb +174 -0
  40. data/lib/stripe/resources/account_link.rb +9 -0
  41. data/lib/stripe/resources/alipay_account.rb +34 -0
  42. data/lib/stripe/resources/apple_pay_domain.rb +16 -0
  43. data/lib/stripe/resources/application_fee.rb +24 -0
  44. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  45. data/lib/stripe/resources/balance.rb +7 -0
  46. data/lib/stripe/resources/balance_transaction.rb +13 -0
  47. data/lib/stripe/resources/bank_account.rb +42 -0
  48. data/lib/stripe/resources/bitcoin_receiver.rb +23 -0
  49. data/lib/stripe/resources/bitcoin_transaction.rb +15 -0
  50. data/lib/stripe/resources/capability.rb +33 -0
  51. data/lib/stripe/resources/card.rb +37 -0
  52. data/lib/stripe/resources/charge.rb +84 -0
  53. data/lib/stripe/resources/checkout/session.rb +11 -0
  54. data/lib/stripe/resources/country_spec.rb +9 -0
  55. data/lib/stripe/resources/coupon.rb +12 -0
  56. data/lib/stripe/resources/credit_note.rb +18 -0
  57. data/lib/stripe/resources/customer.rb +95 -0
  58. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  59. data/lib/stripe/resources/discount.rb +7 -0
  60. data/lib/stripe/resources/dispute.rb +23 -0
  61. data/lib/stripe/resources/ephemeral_key.rb +19 -0
  62. data/lib/stripe/resources/event.rb +9 -0
  63. data/lib/stripe/resources/exchange_rate.rb +9 -0
  64. data/lib/stripe/resources/file.rb +44 -0
  65. data/lib/stripe/resources/file_link.rb +11 -0
  66. data/lib/stripe/resources/invoice.rb +48 -0
  67. data/lib/stripe/resources/invoice_item.rb +12 -0
  68. data/lib/stripe/resources/invoice_line_item.rb +7 -0
  69. data/lib/stripe/resources/issuer_fraud_record.rb +9 -0
  70. data/lib/stripe/resources/issuing/authorization.rb +25 -0
  71. data/lib/stripe/resources/issuing/card.rb +20 -0
  72. data/lib/stripe/resources/issuing/card_details.rb +9 -0
  73. data/lib/stripe/resources/issuing/cardholder.rb +13 -0
  74. data/lib/stripe/resources/issuing/dispute.rb +13 -0
  75. data/lib/stripe/resources/issuing/transaction.rb +12 -0
  76. data/lib/stripe/resources/login_link.rb +14 -0
  77. data/lib/stripe/resources/order.rb +32 -0
  78. data/lib/stripe/resources/order_return.rb +9 -0
  79. data/lib/stripe/resources/payment_intent.rb +30 -0
  80. data/lib/stripe/resources/payment_method.rb +24 -0
  81. data/lib/stripe/resources/payout.rb +24 -0
  82. data/lib/stripe/resources/person.rb +31 -0
  83. data/lib/stripe/resources/plan.rb +12 -0
  84. data/lib/stripe/resources/product.rb +12 -0
  85. data/lib/stripe/resources/radar/early_fraud_warning.rb +11 -0
  86. data/lib/stripe/resources/radar/value_list.rb +14 -0
  87. data/lib/stripe/resources/radar/value_list_item.rb +13 -0
  88. data/lib/stripe/resources/recipient.rb +17 -0
  89. data/lib/stripe/resources/recipient_transfer.rb +7 -0
  90. data/lib/stripe/resources/refund.rb +11 -0
  91. data/lib/stripe/resources/reporting/report_run.rb +12 -0
  92. data/lib/stripe/resources/reporting/report_type.rb +12 -0
  93. data/lib/stripe/resources/reversal.rb +29 -0
  94. data/lib/stripe/resources/review.rb +16 -0
  95. data/lib/stripe/resources/setup_intent.rb +24 -0
  96. data/lib/stripe/resources/sigma/scheduled_query_run.rb +15 -0
  97. data/lib/stripe/resources/sku.rb +12 -0
  98. data/lib/stripe/resources/source.rb +42 -0
  99. data/lib/stripe/resources/source_transaction.rb +7 -0
  100. data/lib/stripe/resources/subscription.rb +25 -0
  101. data/lib/stripe/resources/subscription_item.rb +17 -0
  102. data/lib/stripe/resources/subscription_schedule.rb +32 -0
  103. data/lib/stripe/resources/subscription_schedule_revision.rb +34 -0
  104. data/lib/stripe/resources/tax_id.rb +26 -0
  105. data/lib/stripe/resources/tax_rate.rb +11 -0
  106. data/lib/stripe/resources/terminal/connection_token.rb +11 -0
  107. data/lib/stripe/resources/terminal/location.rb +14 -0
  108. data/lib/stripe/resources/terminal/reader.rb +14 -0
  109. data/lib/stripe/resources/three_d_secure.rb +13 -0
  110. data/lib/stripe/resources/token.rb +9 -0
  111. data/lib/stripe/resources/topup.rb +18 -0
  112. data/lib/stripe/resources/transfer.rb +27 -0
  113. data/lib/stripe/resources/usage_record.rb +23 -0
  114. data/lib/stripe/resources/usage_record_summary.rb +7 -0
  115. data/lib/stripe/resources/webhook_endpoint.rb +12 -0
  116. data/lib/stripe/singleton_api_resource.rb +26 -0
  117. data/lib/stripe/stripe_client.rb +686 -0
  118. data/lib/stripe/stripe_object.rb +583 -0
  119. data/lib/stripe/stripe_response.rb +50 -0
  120. data/lib/stripe/util.rb +336 -0
  121. data/lib/stripe/version.rb +5 -0
  122. data/lib/stripe/webhook.rb +90 -0
  123. data/stripe.gemspec +37 -0
  124. data/test/api_stub_helpers.rb +1 -0
  125. data/test/openapi/README.md +9 -0
  126. data/test/stripe/account_link_test.rb +18 -0
  127. data/test/stripe/account_test.rb +428 -0
  128. data/test/stripe/alipay_account_test.rb +37 -0
  129. data/test/stripe/api_operations_test.rb +80 -0
  130. data/test/stripe/api_resource_test.rb +544 -0
  131. data/test/stripe/apple_pay_domain_test.rb +46 -0
  132. data/test/stripe/application_fee_refund_test.rb +37 -0
  133. data/test/stripe/application_fee_test.rb +58 -0
  134. data/test/stripe/balance_test.rb +13 -0
  135. data/test/stripe/bank_account_test.rb +36 -0
  136. data/test/stripe/capability_test.rb +45 -0
  137. data/test/stripe/charge_test.rb +80 -0
  138. data/test/stripe/checkout/session_test.rb +41 -0
  139. data/test/stripe/country_spec_test.rb +20 -0
  140. data/test/stripe/coupon_test.rb +61 -0
  141. data/test/stripe/credit_note_test.rb +61 -0
  142. data/test/stripe/customer_balance_transaction_test.rb +37 -0
  143. data/test/stripe/customer_card_test.rb +42 -0
  144. data/test/stripe/customer_test.rb +269 -0
  145. data/test/stripe/dispute_test.rb +51 -0
  146. data/test/stripe/ephemeral_key_test.rb +93 -0
  147. data/test/stripe/errors_test.rb +20 -0
  148. data/test/stripe/exchange_rate_test.rb +20 -0
  149. data/test/stripe/file_link_test.rb +41 -0
  150. data/test/stripe/file_test.rb +97 -0
  151. data/test/stripe/file_upload_test.rb +79 -0
  152. data/test/stripe/invoice_item_test.rb +66 -0
  153. data/test/stripe/invoice_line_item_test.rb +8 -0
  154. data/test/stripe/invoice_test.rb +213 -0
  155. data/test/stripe/issuer_fraud_record_test.rb +20 -0
  156. data/test/stripe/issuing/authorization_test.rb +72 -0
  157. data/test/stripe/issuing/card_test.rb +62 -0
  158. data/test/stripe/issuing/cardholder_test.rb +53 -0
  159. data/test/stripe/issuing/dispute_test.rb +45 -0
  160. data/test/stripe/issuing/transaction_test.rb +48 -0
  161. data/test/stripe/list_object_test.rb +156 -0
  162. data/test/stripe/login_link_test.rb +37 -0
  163. data/test/stripe/oauth_test.rb +88 -0
  164. data/test/stripe/order_return_test.rb +21 -0
  165. data/test/stripe/order_test.rb +82 -0
  166. data/test/stripe/payment_intent_test.rb +107 -0
  167. data/test/stripe/payment_method_test.rb +84 -0
  168. data/test/stripe/payout_test.rb +57 -0
  169. data/test/stripe/person_test.rb +46 -0
  170. data/test/stripe/plan_test.rb +98 -0
  171. data/test/stripe/product_test.rb +59 -0
  172. data/test/stripe/radar/early_fraud_warning_test.rb +22 -0
  173. data/test/stripe/radar/value_list_item_test.rb +48 -0
  174. data/test/stripe/radar/value_list_test.rb +61 -0
  175. data/test/stripe/recipient_test.rb +62 -0
  176. data/test/stripe/refund_test.rb +39 -0
  177. data/test/stripe/reporting/report_run_test.rb +33 -0
  178. data/test/stripe/reporting/report_type_test.rb +22 -0
  179. data/test/stripe/reversal_test.rb +43 -0
  180. data/test/stripe/review_test.rb +27 -0
  181. data/test/stripe/setup_intent_test.rb +84 -0
  182. data/test/stripe/sigma/scheduled_query_run_test.rb +22 -0
  183. data/test/stripe/sku_test.rb +60 -0
  184. data/test/stripe/source_test.rb +99 -0
  185. data/test/stripe/source_transaction_test.rb +19 -0
  186. data/test/stripe/stripe_client_test.rb +842 -0
  187. data/test/stripe/stripe_object_test.rb +525 -0
  188. data/test/stripe/stripe_response_test.rb +49 -0
  189. data/test/stripe/subscription_item_test.rb +63 -0
  190. data/test/stripe/subscription_schedule_revision_test.rb +37 -0
  191. data/test/stripe/subscription_schedule_test.rb +116 -0
  192. data/test/stripe/subscription_test.rb +80 -0
  193. data/test/stripe/tax_id_test.rb +31 -0
  194. data/test/stripe/tax_rate_test.rb +43 -0
  195. data/test/stripe/terminal/connection_token_test.rb +16 -0
  196. data/test/stripe/terminal/location_test.rb +68 -0
  197. data/test/stripe/terminal/reader_test.rb +62 -0
  198. data/test/stripe/three_d_secure_test.rb +23 -0
  199. data/test/stripe/topup_test.rb +62 -0
  200. data/test/stripe/transfer_test.rb +88 -0
  201. data/test/stripe/usage_record_summary_test.rb +19 -0
  202. data/test/stripe/usage_record_test.rb +28 -0
  203. data/test/stripe/util_test.rb +402 -0
  204. data/test/stripe/webhook_endpoint_test.rb +59 -0
  205. data/test/stripe/webhook_test.rb +96 -0
  206. data/test/stripe_mock.rb +77 -0
  207. data/test/stripe_test.rb +63 -0
  208. data/test/test_data.rb +61 -0
  209. data/test/test_helper.rb +71 -0
  210. metadata +372 -0
@@ -0,0 +1,208 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Stripe Ruby bindings
4
+ # API spec at https://stripe.com/docs/api
5
+ require "cgi"
6
+ require "faraday"
7
+ require "json"
8
+ require "logger"
9
+ require "openssl"
10
+ require "rbconfig"
11
+ require "securerandom"
12
+ require "set"
13
+ require "socket"
14
+ require "uri"
15
+
16
+ # Version
17
+ require "stripe/version"
18
+
19
+ # API operations
20
+ require "stripe/api_operations/create"
21
+ require "stripe/api_operations/delete"
22
+ require "stripe/api_operations/list"
23
+ require "stripe/api_operations/nested_resource"
24
+ require "stripe/api_operations/request"
25
+ require "stripe/api_operations/save"
26
+
27
+ # API resource support classes
28
+ require "stripe/errors"
29
+ require "stripe/object_types"
30
+ require "stripe/util"
31
+ require "stripe/stripe_client"
32
+ require "stripe/stripe_object"
33
+ require "stripe/stripe_response"
34
+ require "stripe/list_object"
35
+ require "stripe/api_resource"
36
+ require "stripe/singleton_api_resource"
37
+ require "stripe/webhook"
38
+
39
+ # Named API resources
40
+ require "stripe/resources"
41
+
42
+ # OAuth
43
+ require "stripe/oauth"
44
+
45
+ module Stripe
46
+ DEFAULT_CA_BUNDLE_PATH = __dir__ + "/data/ca-certificates.crt"
47
+
48
+ @app_info = nil
49
+
50
+ @api_base = "https://api.stripe.com"
51
+ @connect_base = "https://connect.stripe.com"
52
+ @uploads_base = "https://files.stripe.com"
53
+
54
+ @log_level = nil
55
+ @logger = nil
56
+
57
+ @proxy = nil
58
+
59
+ @max_network_retries = 0
60
+ @max_network_retry_delay = 2
61
+ @initial_network_retry_delay = 0.5
62
+
63
+ @ca_bundle_path = DEFAULT_CA_BUNDLE_PATH
64
+ @ca_store = nil
65
+ @verify_ssl_certs = true
66
+
67
+ @open_timeout = 30
68
+ @read_timeout = 80
69
+
70
+ @enable_telemetry = true
71
+
72
+ class << self
73
+ attr_accessor :stripe_account, :api_key, :api_base, :verify_ssl_certs,
74
+ :api_version, :client_id, :connect_base, :uploads_base,
75
+ :open_timeout, :read_timeout, :proxy
76
+
77
+ attr_reader :max_network_retry_delay, :initial_network_retry_delay
78
+ end
79
+
80
+ # Gets the application for a plugin that's identified some. See
81
+ # #set_app_info.
82
+ def self.app_info
83
+ @app_info
84
+ end
85
+
86
+ def self.app_info=(info)
87
+ @app_info = info
88
+ end
89
+
90
+ # The location of a file containing a bundle of CA certificates. By default
91
+ # the library will use an included bundle that can successfully validate
92
+ # Stripe certificates.
93
+ def self.ca_bundle_path
94
+ @ca_bundle_path
95
+ end
96
+
97
+ def self.ca_bundle_path=(path)
98
+ @ca_bundle_path = path
99
+
100
+ # empty this field so a new store is initialized
101
+ @ca_store = nil
102
+ end
103
+
104
+ # A certificate store initialized from the the bundle in #ca_bundle_path and
105
+ # which is used to validate TLS on every request.
106
+ #
107
+ # This was added to the give the gem "pseudo thread safety" in that it seems
108
+ # when initiating many parallel requests marshaling the certificate store is
109
+ # the most likely point of failure (see issue #382). Any program attempting
110
+ # to leverage this pseudo safety should make a call to this method (i.e.
111
+ # `Stripe.ca_store`) in their initialization code because it marshals lazily
112
+ # and is itself not thread safe.
113
+ def self.ca_store
114
+ @ca_store ||= begin
115
+ store = OpenSSL::X509::Store.new
116
+ store.add_file(ca_bundle_path)
117
+ store
118
+ end
119
+ end
120
+
121
+ # map to the same values as the standard library's logger
122
+ LEVEL_DEBUG = Logger::DEBUG
123
+ LEVEL_ERROR = Logger::ERROR
124
+ LEVEL_INFO = Logger::INFO
125
+
126
+ # When set prompts the library to log some extra information to $stdout and
127
+ # $stderr about what it's doing. For example, it'll produce information about
128
+ # requests, responses, and errors that are received. Valid log levels are
129
+ # `debug` and `info`, with `debug` being a little more verbose in places.
130
+ #
131
+ # Use of this configuration is only useful when `.logger` is _not_ set. When
132
+ # it is, the decision what levels to print is entirely deferred to the logger.
133
+ def self.log_level
134
+ @log_level
135
+ end
136
+
137
+ def self.log_level=(val)
138
+ # Backwards compatibility for values that we briefly allowed
139
+ if val == "debug"
140
+ val = LEVEL_DEBUG
141
+ elsif val == "info"
142
+ val = LEVEL_INFO
143
+ end
144
+
145
+ if !val.nil? && ![LEVEL_DEBUG, LEVEL_ERROR, LEVEL_INFO].include?(val)
146
+ raise ArgumentError,
147
+ "log_level should only be set to `nil`, `debug` or `info`"
148
+ end
149
+ @log_level = val
150
+ end
151
+
152
+ # Sets a logger to which logging output will be sent. The logger should
153
+ # support the same interface as the `Logger` class that's part of Ruby's
154
+ # standard library (hint, anything in `Rails.logger` will likely be
155
+ # suitable).
156
+ #
157
+ # If `.logger` is set, the value of `.log_level` is ignored. The decision on
158
+ # what levels to print is entirely deferred to the logger.
159
+ def self.logger
160
+ @logger
161
+ end
162
+
163
+ def self.logger=(val)
164
+ @logger = val
165
+ end
166
+
167
+ def self.max_network_retries
168
+ @max_network_retries
169
+ end
170
+
171
+ def self.max_network_retries=(val)
172
+ @max_network_retries = val.to_i
173
+ end
174
+
175
+ def self.enable_telemetry?
176
+ @enable_telemetry
177
+ end
178
+
179
+ def self.enable_telemetry=(val)
180
+ @enable_telemetry = val
181
+ end
182
+
183
+ # Sets some basic information about the running application that's sent along
184
+ # with API requests. Useful for plugin authors to identify their plugin when
185
+ # communicating with Stripe.
186
+ #
187
+ # Takes a name and optional partner program ID, plugin URL, and version.
188
+ def self.set_app_info(name, partner_id: nil, url: nil, version: nil)
189
+ @app_info = {
190
+ name: name,
191
+ partner_id: partner_id,
192
+ url: url,
193
+ version: version,
194
+ }
195
+ end
196
+
197
+ # DEPRECATED. Use `Util#encode_parameters` instead.
198
+ def self.uri_encode(params)
199
+ Util.encode_parameters(params)
200
+ end
201
+ private_class_method :uri_encode
202
+ class << self
203
+ extend Gem::Deprecate
204
+ deprecate :uri_encode, "Stripe::Util#encode_parameters", 2016, 1
205
+ end
206
+ end
207
+
208
+ Stripe.log_level = ENV["STRIPE_LOG"] unless ENV["STRIPE_LOG"].nil?
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ module Create
6
+ def create(params = {}, opts = {})
7
+ resp, opts = request(:post, resource_url, params, opts)
8
+ Util.convert_to_stripe_object(resp.data, opts)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ module Delete
6
+ module ClassMethods
7
+ # Deletes an API resource
8
+ #
9
+ # Deletes the identified resource with the passed in parameters.
10
+ #
11
+ # ==== Attributes
12
+ #
13
+ # * +id+ - ID of the resource to delete.
14
+ # * +params+ - A hash of parameters to pass to the API
15
+ # * +opts+ - A Hash of additional options (separate from the params /
16
+ # object values) to be added to the request. E.g. to allow for an
17
+ # idempotency_key to be passed in the request headers, or for the
18
+ # api_key to be overwritten. See {APIOperations::Request.request}.
19
+ def delete(id, params = {}, opts = {})
20
+ resp, opts = request(:delete, "#{resource_url}/#{id}", params, opts)
21
+ Util.convert_to_stripe_object(resp.data, opts)
22
+ end
23
+ end
24
+
25
+ def delete(params = {}, opts = {})
26
+ resp, opts = request(:delete, resource_url, params, opts)
27
+ initialize_from(resp.data, opts)
28
+ end
29
+
30
+ def self.included(base)
31
+ base.extend(ClassMethods)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ module List
6
+ def list(filters = {}, opts = {})
7
+ opts = Util.normalize_opts(opts)
8
+
9
+ resp, opts = request(:get, resource_url, filters, opts)
10
+ obj = ListObject.construct_from(resp.data, opts)
11
+
12
+ # set filters so that we can fetch the same limit, expansions, and
13
+ # predicates when accessing the next and previous pages
14
+ #
15
+ # just for general cleanliness, remove any paging options
16
+ obj.filters = filters.dup
17
+ obj.filters.delete(:ending_before)
18
+ obj.filters.delete(:starting_after)
19
+
20
+ obj
21
+ end
22
+
23
+ # The original version of #list was given the somewhat unfortunate name of
24
+ # #all, and this alias allows us to maintain backward compatibility (the
25
+ # choice was somewhat misleading in the way that it only returned a single
26
+ # page rather than all objects).
27
+ alias all list
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ # Adds methods to help manipulate a subresource from its parent resource so
6
+ # that it's possible to do so from a static context (i.e. without a
7
+ # pre-existing collection of subresources on the parent).
8
+ #
9
+ # For examle, a transfer gains the static methods for reversals so that the
10
+ # methods `.create_reversal`, `.retrieve_reversal`, `.update_reversal`,
11
+ # etc. all become available.
12
+ module NestedResource
13
+ def nested_resource_class_methods(resource, path: nil, operations: nil,
14
+ resource_plural: nil)
15
+ resource_plural ||= "#{resource}s"
16
+ path ||= resource_plural
17
+ raise ArgumentError, "operations array required" if operations.nil?
18
+
19
+ resource_url_method = :"#{resource}s_url"
20
+ define_singleton_method(resource_url_method) do |id, nested_id = nil|
21
+ url = "#{resource_url}/#{CGI.escape(id)}/#{CGI.escape(path)}"
22
+ url += "/#{CGI.escape(nested_id)}" unless nested_id.nil?
23
+ url
24
+ end
25
+
26
+ operations.each do |operation|
27
+ case operation
28
+ when :create
29
+ define_singleton_method(:"create_#{resource}") \
30
+ do |id, params = {}, opts = {}|
31
+ url = send(resource_url_method, id)
32
+ resp, opts = request(:post, url, params, opts)
33
+ Util.convert_to_stripe_object(resp.data, opts)
34
+ end
35
+ when :retrieve
36
+ define_singleton_method(:"retrieve_#{resource}") \
37
+ do |id, nested_id, opts = {}|
38
+ url = send(resource_url_method, id, nested_id)
39
+ resp, opts = request(:get, url, {}, opts)
40
+ Util.convert_to_stripe_object(resp.data, opts)
41
+ end
42
+ when :update
43
+ define_singleton_method(:"update_#{resource}") \
44
+ do |id, nested_id, params = {}, opts = {}|
45
+ url = send(resource_url_method, id, nested_id)
46
+ resp, opts = request(:post, url, params, opts)
47
+ Util.convert_to_stripe_object(resp.data, opts)
48
+ end
49
+ when :delete
50
+ define_singleton_method(:"delete_#{resource}") \
51
+ do |id, nested_id, params = {}, opts = {}|
52
+ url = send(resource_url_method, id, nested_id)
53
+ resp, opts = request(:delete, url, params, opts)
54
+ Util.convert_to_stripe_object(resp.data, opts)
55
+ end
56
+ when :list
57
+ define_singleton_method(:"list_#{resource_plural}") \
58
+ do |id, params = {}, opts = {}|
59
+ url = send(resource_url_method, id)
60
+ resp, opts = request(:get, url, params, opts)
61
+ Util.convert_to_stripe_object(resp.data, opts)
62
+ end
63
+ else
64
+ raise ArgumentError, "Unknown operation: #{operation.inspect}"
65
+ end
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ module Request
6
+ module ClassMethods
7
+ def request(method, url, params = {}, opts = {})
8
+ warn_on_opts_in_params(params)
9
+
10
+ opts = Util.normalize_opts(opts)
11
+ opts[:client] ||= StripeClient.active_client
12
+
13
+ headers = opts.clone
14
+ api_key = headers.delete(:api_key)
15
+ api_base = headers.delete(:api_base)
16
+ client = headers.delete(:client)
17
+ # Assume all remaining opts must be headers
18
+
19
+ resp, opts[:api_key] = client.execute_request(
20
+ method, url,
21
+ api_base: api_base, api_key: api_key,
22
+ headers: headers, params: params
23
+ )
24
+
25
+ # Hash#select returns an array before 1.9
26
+ opts_to_persist = {}
27
+ opts.each do |k, v|
28
+ opts_to_persist[k] = v if Util::OPTS_PERSISTABLE.include?(k)
29
+ end
30
+
31
+ [resp, opts_to_persist]
32
+ end
33
+
34
+ private def warn_on_opts_in_params(params)
35
+ Util::OPTS_USER_SPECIFIED.each do |opt|
36
+ if params.key?(opt)
37
+ warn("WARNING: #{opt} should be in opts instead of params.")
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ def self.included(base)
44
+ base.extend(ClassMethods)
45
+ end
46
+
47
+ protected def request(method, url, params = {}, opts = {})
48
+ opts = @opts.merge(Util.normalize_opts(opts))
49
+ self.class.request(method, url, params, opts)
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ module APIOperations
5
+ module Save
6
+ module ClassMethods
7
+ # Updates an API resource
8
+ #
9
+ # Updates the identified resource with the passed in parameters.
10
+ #
11
+ # ==== Attributes
12
+ #
13
+ # * +id+ - ID of the resource to update.
14
+ # * +params+ - A hash of parameters to pass to the API
15
+ # * +opts+ - A Hash of additional options (separate from the params /
16
+ # object values) to be added to the request. E.g. to allow for an
17
+ # idempotency_key to be passed in the request headers, or for the
18
+ # api_key to be overwritten. See {APIOperations::Request.request}.
19
+ def update(id, params = {}, opts = {})
20
+ params.each_key do |k|
21
+ if protected_fields.include?(k)
22
+ raise ArgumentError, "Cannot update protected field: #{k}"
23
+ end
24
+ end
25
+
26
+ resp, opts = request(:post, "#{resource_url}/#{id}", params, opts)
27
+ Util.convert_to_stripe_object(resp.data, opts)
28
+ end
29
+ end
30
+
31
+ # Creates or updates an API resource.
32
+ #
33
+ # If the resource doesn't yet have an assigned ID and the resource is one
34
+ # that can be created, then the method attempts to create the resource.
35
+ # The resource is updated otherwise.
36
+ #
37
+ # ==== Attributes
38
+ #
39
+ # * +params+ - Overrides any parameters in the resource's serialized data
40
+ # and includes them in the create or update. If +:req_url:+ is included
41
+ # in the list, it overrides the update URL used for the create or
42
+ # update.
43
+ # * +opts+ - A Hash of additional options (separate from the params /
44
+ # object values) to be added to the request. E.g. to allow for an
45
+ # idempotency_key to be passed in the request headers, or for the
46
+ # api_key to be overwritten. See {APIOperations::Request.request}.
47
+ def save(params = {}, opts = {})
48
+ # We started unintentionally (sort of) allowing attributes sent to
49
+ # +save+ to override values used during the update. So as not to break
50
+ # the API, this makes that official here.
51
+ update_attributes(params)
52
+
53
+ # Now remove any parameters that look like object attributes.
54
+ params = params.reject { |k, _| respond_to?(k) }
55
+
56
+ values = serialize_params(self).merge(params)
57
+
58
+ # note that id gets removed here our call to #url above has already
59
+ # generated a uri for this object with an identifier baked in
60
+ values.delete(:id)
61
+
62
+ resp, opts = request(:post, save_url, values, opts)
63
+ initialize_from(resp.data, opts)
64
+ end
65
+
66
+ def self.included(base)
67
+ # Set `metadata` as additive so that when it's set directly we remember
68
+ # to clear keys that may have been previously set by sending empty
69
+ # values for them.
70
+ #
71
+ # It's possible that not every object with `Save` has `metadata`, but
72
+ # it's a close enough heuristic, and having this option set when there
73
+ # is no `metadata` field is not harmful.
74
+ base.additive_object_param(:metadata)
75
+
76
+ base.extend(ClassMethods)
77
+ end
78
+
79
+ private def save_url
80
+ # This switch essentially allows us "upsert"-like functionality. If the
81
+ # API resource doesn't have an ID set (suggesting that it's new) and
82
+ # its class responds to .create (which comes from
83
+ # Stripe::APIOperations::Create), then use the URL to create a new
84
+ # resource. Otherwise, generate a URL based on the object's identifier
85
+ # for a normal update.
86
+ if self[:id].nil? && self.class.respond_to?(:create)
87
+ self.class.resource_url
88
+ else
89
+ resource_url
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end