stripe 4.17.0 → 5.32.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (227) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +193 -1
  3. data/CODE_OF_CONDUCT.md +77 -0
  4. data/Gemfile +10 -14
  5. data/README.md +167 -63
  6. data/Rakefile +8 -7
  7. data/VERSION +1 -1
  8. data/lib/stripe/api_operations/create.rb +1 -1
  9. data/lib/stripe/api_operations/delete.rb +7 -3
  10. data/lib/stripe/api_operations/list.rb +1 -12
  11. data/lib/stripe/api_operations/nested_resource.rb +36 -27
  12. data/lib/stripe/api_operations/request.rb +47 -8
  13. data/lib/stripe/api_operations/save.rb +8 -7
  14. data/lib/stripe/api_resource.rb +30 -5
  15. data/lib/stripe/connection_manager.rb +164 -0
  16. data/lib/stripe/error_object.rb +94 -0
  17. data/lib/stripe/errors.rb +31 -10
  18. data/lib/stripe/instrumentation.rb +82 -0
  19. data/lib/stripe/list_object.rb +41 -7
  20. data/lib/stripe/multipart_encoder.rb +131 -0
  21. data/lib/stripe/oauth.rb +8 -6
  22. data/lib/stripe/object_types.rb +102 -0
  23. data/lib/stripe/{account.rb → resources/account.rb} +44 -33
  24. data/lib/stripe/{account_link.rb → resources/account_link.rb} +2 -1
  25. data/lib/stripe/resources/alipay_account.rb +34 -0
  26. data/lib/stripe/{apple_pay_domain.rb → resources/apple_pay_domain.rb} +2 -1
  27. data/lib/stripe/resources/application_fee.rb +14 -0
  28. data/lib/stripe/resources/application_fee_refund.rb +30 -0
  29. data/lib/stripe/{balance.rb → resources/balance.rb} +2 -1
  30. data/lib/stripe/{balance_transaction.rb → resources/balance_transaction.rb} +2 -5
  31. data/lib/stripe/resources/bank_account.rb +43 -0
  32. data/lib/stripe/{issuing/dispute.rb → resources/billing_portal/configuration.rb} +4 -3
  33. data/lib/stripe/{checkout → resources/billing_portal}/session.rb +3 -2
  34. data/lib/stripe/{bitcoin_receiver.rb → resources/bitcoin_receiver.rb} +4 -3
  35. data/lib/stripe/{bitcoin_transaction.rb → resources/bitcoin_transaction.rb} +1 -1
  36. data/lib/stripe/{capability.rb → resources/capability.rb} +11 -4
  37. data/lib/stripe/{card.rb → resources/card.rb} +12 -3
  38. data/lib/stripe/resources/charge.rb +23 -0
  39. data/lib/stripe/resources/checkout/session.rb +16 -0
  40. data/lib/stripe/{country_spec.rb → resources/country_spec.rb} +2 -1
  41. data/lib/stripe/{coupon.rb → resources/coupon.rb} +2 -1
  42. data/lib/stripe/resources/credit_note.rb +33 -0
  43. data/lib/stripe/resources/credit_note_line_item.rb +7 -0
  44. data/lib/stripe/resources/customer.rb +41 -0
  45. data/lib/stripe/resources/customer_balance_transaction.rb +30 -0
  46. data/lib/stripe/{discount.rb → resources/discount.rb} +1 -1
  47. data/lib/stripe/resources/dispute.rb +22 -0
  48. data/lib/stripe/{ephemeral_key.rb → resources/ephemeral_key.rb} +6 -2
  49. data/lib/stripe/{event.rb → resources/event.rb} +2 -1
  50. data/lib/stripe/{exchange_rate.rb → resources/exchange_rate.rb} +2 -1
  51. data/lib/stripe/{file.rb → resources/file.rb} +7 -12
  52. data/lib/stripe/{file_link.rb → resources/file_link.rb} +2 -1
  53. data/lib/stripe/resources/invoice.rb +74 -0
  54. data/lib/stripe/{invoice_item.rb → resources/invoice_item.rb} +2 -1
  55. data/lib/stripe/{invoice_line_item.rb → resources/invoice_line_item.rb} +1 -1
  56. data/lib/stripe/{issuing → resources/issuing}/authorization.rb +14 -5
  57. data/lib/stripe/{issuing → resources/issuing}/card.rb +8 -3
  58. data/lib/stripe/{issuing → resources/issuing}/card_details.rb +1 -1
  59. data/lib/stripe/{issuing → resources/issuing}/cardholder.rb +2 -1
  60. data/lib/stripe/resources/issuing/dispute.rb +25 -0
  61. data/lib/stripe/{issuing → resources/issuing}/transaction.rb +2 -1
  62. data/lib/stripe/resources/line_item.rb +7 -0
  63. data/lib/stripe/resources/login_link.rb +14 -0
  64. data/lib/stripe/resources/mandate.rb +8 -0
  65. data/lib/stripe/resources/order.rb +33 -0
  66. data/lib/stripe/{order_return.rb → resources/order_return.rb} +2 -1
  67. data/lib/stripe/resources/payment_intent.rb +43 -0
  68. data/lib/stripe/{payment_method.rb → resources/payment_method.rb} +14 -5
  69. data/lib/stripe/resources/payout.rb +33 -0
  70. data/lib/stripe/{person.rb → resources/person.rb} +8 -3
  71. data/lib/stripe/{plan.rb → resources/plan.rb} +2 -1
  72. data/lib/stripe/resources/price.rb +12 -0
  73. data/lib/stripe/{product.rb → resources/product.rb} +2 -1
  74. data/lib/stripe/resources/promotion_code.rb +12 -0
  75. data/lib/stripe/resources/radar/early_fraud_warning.rb +12 -0
  76. data/lib/stripe/{radar → resources/radar}/value_list.rb +2 -1
  77. data/lib/stripe/{radar → resources/radar}/value_list_item.rb +2 -1
  78. data/lib/stripe/{recipient.rb → resources/recipient.rb} +2 -5
  79. data/lib/stripe/{recipient_transfer.rb → resources/recipient_transfer.rb} +1 -1
  80. data/lib/stripe/{refund.rb → resources/refund.rb} +2 -1
  81. data/lib/stripe/{reporting → resources/reporting}/report_run.rb +2 -1
  82. data/lib/stripe/{reporting → resources/reporting}/report_type.rb +2 -1
  83. data/lib/stripe/resources/reversal.rb +29 -0
  84. data/lib/stripe/{review.rb → resources/review.rb} +8 -3
  85. data/lib/stripe/resources/setup_attempt.rb +10 -0
  86. data/lib/stripe/resources/setup_intent.rb +33 -0
  87. data/lib/stripe/{sigma → resources/sigma}/scheduled_query_run.rb +2 -1
  88. data/lib/stripe/{sku.rb → resources/sku.rb} +2 -1
  89. data/lib/stripe/{source.rb → resources/source.rb} +22 -15
  90. data/lib/stripe/{source_transaction.rb → resources/source_transaction.rb} +1 -1
  91. data/lib/stripe/{subscription.rb → resources/subscription.rb} +12 -13
  92. data/lib/stripe/resources/subscription_item.rb +26 -0
  93. data/lib/stripe/resources/subscription_schedule.rb +33 -0
  94. data/lib/stripe/resources/tax_id.rb +26 -0
  95. data/lib/stripe/{tax_rate.rb → resources/tax_rate.rb} +2 -1
  96. data/lib/stripe/{terminal → resources/terminal}/connection_token.rb +2 -1
  97. data/lib/stripe/{terminal → resources/terminal}/location.rb +2 -1
  98. data/lib/stripe/{terminal → resources/terminal}/reader.rb +2 -1
  99. data/lib/stripe/{three_d_secure.rb → resources/three_d_secure.rb} +2 -1
  100. data/lib/stripe/{token.rb → resources/token.rb} +2 -1
  101. data/lib/stripe/{topup.rb → resources/topup.rb} +8 -3
  102. data/lib/stripe/resources/transfer.rb +27 -0
  103. data/lib/stripe/resources/usage_record.rb +7 -0
  104. data/lib/stripe/{usage_record_summary.rb → resources/usage_record_summary.rb} +1 -1
  105. data/lib/stripe/{webhook_endpoint.rb → resources/webhook_endpoint.rb} +2 -1
  106. data/lib/stripe/resources.rb +85 -0
  107. data/lib/stripe/singleton_api_resource.rb +3 -1
  108. data/lib/stripe/stripe_client.rb +576 -254
  109. data/lib/stripe/stripe_configuration.rb +194 -0
  110. data/lib/stripe/stripe_object.rb +80 -61
  111. data/lib/stripe/stripe_response.rb +53 -21
  112. data/lib/stripe/util.rb +76 -121
  113. data/lib/stripe/version.rb +1 -1
  114. data/lib/stripe/webhook.rb +43 -10
  115. data/lib/stripe.rb +42 -203
  116. data/stripe.gemspec +23 -7
  117. metadata +107 -284
  118. data/.gitattributes +0 -4
  119. data/.github/ISSUE_TEMPLATE.md +0 -5
  120. data/.gitignore +0 -8
  121. data/.rubocop.yml +0 -32
  122. data/.rubocop_todo.yml +0 -50
  123. data/.travis.yml +0 -42
  124. data/lib/stripe/alipay_account.rb +0 -27
  125. data/lib/stripe/application_fee.rb +0 -23
  126. data/lib/stripe/application_fee_refund.rb +0 -22
  127. data/lib/stripe/bank_account.rb +0 -32
  128. data/lib/stripe/charge.rb +0 -86
  129. data/lib/stripe/credit_note.rb +0 -18
  130. data/lib/stripe/customer.rb +0 -95
  131. data/lib/stripe/dispute.rb +0 -23
  132. data/lib/stripe/invoice.rb +0 -48
  133. data/lib/stripe/issuer_fraud_record.rb +0 -9
  134. data/lib/stripe/login_link.rb +0 -11
  135. data/lib/stripe/order.rb +0 -34
  136. data/lib/stripe/payment_intent.rb +0 -30
  137. data/lib/stripe/payout.rb +0 -24
  138. data/lib/stripe/reversal.rb +0 -22
  139. data/lib/stripe/subscription_item.rb +0 -17
  140. data/lib/stripe/subscription_schedule.rb +0 -35
  141. data/lib/stripe/subscription_schedule_revision.rb +0 -25
  142. data/lib/stripe/tax_id.rb +0 -22
  143. data/lib/stripe/transfer.rb +0 -25
  144. data/lib/stripe/usage_record.rb +0 -14
  145. data/test/api_stub_helpers.rb +0 -1
  146. data/test/openapi/README.md +0 -9
  147. data/test/stripe/account_link_test.rb +0 -18
  148. data/test/stripe/account_test.rb +0 -428
  149. data/test/stripe/alipay_account_test.rb +0 -37
  150. data/test/stripe/api_operations_test.rb +0 -81
  151. data/test/stripe/api_resource_test.rb +0 -526
  152. data/test/stripe/apple_pay_domain_test.rb +0 -46
  153. data/test/stripe/application_fee_refund_test.rb +0 -37
  154. data/test/stripe/application_fee_test.rb +0 -58
  155. data/test/stripe/balance_test.rb +0 -13
  156. data/test/stripe/bank_account_test.rb +0 -36
  157. data/test/stripe/capability_test.rb +0 -45
  158. data/test/stripe/charge_test.rb +0 -80
  159. data/test/stripe/checkout/session_test.rb +0 -41
  160. data/test/stripe/country_spec_test.rb +0 -20
  161. data/test/stripe/coupon_test.rb +0 -61
  162. data/test/stripe/credit_note_test.rb +0 -61
  163. data/test/stripe/customer_card_test.rb +0 -42
  164. data/test/stripe/customer_test.rb +0 -226
  165. data/test/stripe/dispute_test.rb +0 -51
  166. data/test/stripe/ephemeral_key_test.rb +0 -93
  167. data/test/stripe/errors_test.rb +0 -20
  168. data/test/stripe/exchange_rate_test.rb +0 -20
  169. data/test/stripe/file_link_test.rb +0 -41
  170. data/test/stripe/file_test.rb +0 -97
  171. data/test/stripe/file_upload_test.rb +0 -79
  172. data/test/stripe/invoice_item_test.rb +0 -66
  173. data/test/stripe/invoice_line_item_test.rb +0 -8
  174. data/test/stripe/invoice_test.rb +0 -213
  175. data/test/stripe/issuer_fraud_record_test.rb +0 -20
  176. data/test/stripe/issuing/authorization_test.rb +0 -72
  177. data/test/stripe/issuing/card_test.rb +0 -62
  178. data/test/stripe/issuing/cardholder_test.rb +0 -53
  179. data/test/stripe/issuing/dispute_test.rb +0 -45
  180. data/test/stripe/issuing/transaction_test.rb +0 -48
  181. data/test/stripe/list_object_test.rb +0 -156
  182. data/test/stripe/login_link_test.rb +0 -37
  183. data/test/stripe/oauth_test.rb +0 -88
  184. data/test/stripe/order_return_test.rb +0 -21
  185. data/test/stripe/order_test.rb +0 -75
  186. data/test/stripe/payment_intent_test.rb +0 -107
  187. data/test/stripe/payment_method_test.rb +0 -84
  188. data/test/stripe/payout_test.rb +0 -57
  189. data/test/stripe/person_test.rb +0 -46
  190. data/test/stripe/plan_test.rb +0 -98
  191. data/test/stripe/product_test.rb +0 -59
  192. data/test/stripe/radar/value_list_item_test.rb +0 -48
  193. data/test/stripe/radar/value_list_test.rb +0 -61
  194. data/test/stripe/recipient_test.rb +0 -62
  195. data/test/stripe/refund_test.rb +0 -39
  196. data/test/stripe/reporting/report_run_test.rb +0 -33
  197. data/test/stripe/reporting/report_type_test.rb +0 -22
  198. data/test/stripe/reversal_test.rb +0 -43
  199. data/test/stripe/review_test.rb +0 -27
  200. data/test/stripe/sigma/scheduled_query_run_test.rb +0 -22
  201. data/test/stripe/sku_test.rb +0 -60
  202. data/test/stripe/source_test.rb +0 -99
  203. data/test/stripe/source_transaction_test.rb +0 -19
  204. data/test/stripe/stripe_client_test.rb +0 -842
  205. data/test/stripe/stripe_object_test.rb +0 -525
  206. data/test/stripe/stripe_response_test.rb +0 -49
  207. data/test/stripe/subscription_item_test.rb +0 -63
  208. data/test/stripe/subscription_schedule_revision_test.rb +0 -37
  209. data/test/stripe/subscription_schedule_test.rb +0 -116
  210. data/test/stripe/subscription_test.rb +0 -80
  211. data/test/stripe/tax_id_test.rb +0 -31
  212. data/test/stripe/tax_rate_test.rb +0 -43
  213. data/test/stripe/terminal/connection_token_test.rb +0 -16
  214. data/test/stripe/terminal/location_test.rb +0 -68
  215. data/test/stripe/terminal/reader_test.rb +0 -62
  216. data/test/stripe/three_d_secure_test.rb +0 -23
  217. data/test/stripe/topup_test.rb +0 -62
  218. data/test/stripe/transfer_test.rb +0 -88
  219. data/test/stripe/usage_record_summary_test.rb +0 -19
  220. data/test/stripe/usage_record_test.rb +0 -28
  221. data/test/stripe/util_test.rb +0 -402
  222. data/test/stripe/webhook_endpoint_test.rb +0 -59
  223. data/test/stripe/webhook_test.rb +0 -96
  224. data/test/stripe_mock.rb +0 -77
  225. data/test/stripe_test.rb +0 -63
  226. data/test/test_data.rb +0 -61
  227. data/test/test_helper.rb +0 -73
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ # Represents an error object as returned by the API.
5
+ #
6
+ # @see https://stripe.com/docs/api/errors
7
+ class ErrorObject < StripeObject
8
+ # Unlike other objects, we explicitly declare getter methods here. This
9
+ # is because the API doesn't return `null` values for fields on this
10
+ # object, rather the fields are omitted entirely. Not declaring the getter
11
+ # methods would cause users to run into `NoMethodError` exceptions and
12
+ # get in the way of generic error handling.
13
+
14
+ # For card errors, the ID of the failed charge.
15
+ def charge
16
+ @values[:charge]
17
+ end
18
+
19
+ # For some errors that could be handled programmatically, a short string
20
+ # indicating the error code reported.
21
+ def code
22
+ @values[:code]
23
+ end
24
+
25
+ # For card errors resulting from a card issuer decline, a short string
26
+ # indicating the card issuer's reason for the decline if they provide one.
27
+ def decline_code
28
+ @values[:decline_code]
29
+ end
30
+
31
+ # A URL to more information about the error code reported.
32
+ def doc_url
33
+ @values[:doc_url]
34
+ end
35
+
36
+ # A human-readable message providing more details about the error. For card
37
+ # errors, these messages can be shown to your users.
38
+ def message
39
+ @values[:message]
40
+ end
41
+
42
+ # If the error is parameter-specific, the parameter related to the error.
43
+ # For example, you can use this to display a message near the correct form
44
+ # field.
45
+ def param
46
+ @values[:param]
47
+ end
48
+
49
+ # The PaymentIntent object for errors returned on a request involving a
50
+ # PaymentIntent.
51
+ def payment_intent
52
+ @values[:payment_intent]
53
+ end
54
+
55
+ # The PaymentMethod object for errors returned on a request involving a
56
+ # PaymentMethod.
57
+ def payment_method
58
+ @values[:payment_method]
59
+ end
60
+
61
+ # The SetupIntent object for errors returned on a request involving a
62
+ # SetupIntent.
63
+ def setup_intent
64
+ @values[:setup_intent]
65
+ end
66
+
67
+ # The source object for errors returned on a request involving a source.
68
+ def source
69
+ @values[:source]
70
+ end
71
+
72
+ # The type of error returned. One of `api_connection_error`, `api_error`,
73
+ # `authentication_error`, `card_error`, `idempotency_error`,
74
+ # `invalid_request_error`, or `rate_limit_error`.
75
+ def type
76
+ @values[:type]
77
+ end
78
+ end
79
+
80
+ # Represents on OAuth error returned by the OAuth API.
81
+ #
82
+ # @see https://stripe.com/docs/connect/oauth-reference#post-token-errors
83
+ class OAuthErrorObject < StripeObject
84
+ # A unique error code per error type.
85
+ def error
86
+ @values[:error]
87
+ end
88
+
89
+ # A human readable description of the error.
90
+ def error_description
91
+ @values[:error_description]
92
+ end
93
+ end
94
+ end
data/lib/stripe/errors.rb CHANGED
@@ -11,6 +11,7 @@ module Stripe
11
11
  attr_accessor :response
12
12
 
13
13
  attr_reader :code
14
+ attr_reader :error
14
15
  attr_reader :http_body
15
16
  attr_reader :http_headers
16
17
  attr_reader :http_status
@@ -18,15 +19,30 @@ module Stripe
18
19
  attr_reader :request_id
19
20
 
20
21
  # Initializes a StripeError.
21
- def initialize(message = nil, http_status: nil, http_body: nil, json_body: nil,
22
- http_headers: nil, code: nil)
22
+ def initialize(message = nil, http_status: nil, http_body: nil,
23
+ json_body: nil, http_headers: nil, code: nil)
23
24
  @message = message
24
25
  @http_status = http_status
25
26
  @http_body = http_body
26
27
  @http_headers = http_headers || {}
28
+ @idempotent_replayed = @http_headers["idempotent-replayed"] == "true"
27
29
  @json_body = json_body
28
30
  @code = code
29
- @request_id = @http_headers[:request_id]
31
+ @request_id = @http_headers["request-id"]
32
+ @error = construct_error_object
33
+ end
34
+
35
+ def construct_error_object
36
+ return nil if @json_body.nil? || !@json_body.key?(:error)
37
+
38
+ ErrorObject.construct_from(@json_body[:error])
39
+ end
40
+
41
+ # Whether the error was the result of an idempotent replay, meaning that it
42
+ # originally occurred on a previous request and is being replayed back
43
+ # because the user sent the same idempotency key for this one.
44
+ def idempotent_replayed?
45
+ @idempotent_replayed
30
46
  end
31
47
 
32
48
  def to_s
@@ -59,9 +75,8 @@ module Stripe
59
75
  class CardError < StripeError
60
76
  attr_reader :param
61
77
 
62
- # TODO: make code a keyword arg in next major release
63
- def initialize(message, param, code, http_status: nil, http_body: nil, json_body: nil,
64
- http_headers: nil)
78
+ def initialize(message, param, code: nil, http_status: nil, http_body: nil,
79
+ json_body: nil, http_headers: nil)
65
80
  super(message, http_status: http_status, http_body: http_body,
66
81
  json_body: json_body, http_headers: http_headers,
67
82
  code: code)
@@ -79,8 +94,8 @@ module Stripe
79
94
  class InvalidRequestError < StripeError
80
95
  attr_accessor :param
81
96
 
82
- def initialize(message, param, http_status: nil, http_body: nil, json_body: nil,
83
- http_headers: nil, code: nil)
97
+ def initialize(message, param, http_status: nil, http_body: nil,
98
+ json_body: nil, http_headers: nil, code: nil)
84
99
  super(message, http_status: http_status, http_body: http_body,
85
100
  json_body: json_body, http_headers: http_headers,
86
101
  code: code)
@@ -113,12 +128,18 @@ module Stripe
113
128
  module OAuth
114
129
  # OAuthError is raised when the OAuth API returns an error.
115
130
  class OAuthError < StripeError
116
- def initialize(code, description, http_status: nil, http_body: nil, json_body: nil,
117
- http_headers: nil)
131
+ def initialize(code, description, http_status: nil, http_body: nil,
132
+ json_body: nil, http_headers: nil)
118
133
  super(description, http_status: http_status, http_body: http_body,
119
134
  json_body: json_body, http_headers: http_headers,
120
135
  code: code)
121
136
  end
137
+
138
+ def construct_error_object
139
+ return nil if @json_body.nil?
140
+
141
+ OAuthErrorObject.construct_from(@json_body)
142
+ end
122
143
  end
123
144
 
124
145
  # InvalidClientError is raised when the client doesn't belong to you, or
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Stripe
4
+ class Instrumentation
5
+ # Event emitted on `request_begin` callback.
6
+ class RequestBeginEvent
7
+ attr_reader :method
8
+ attr_reader :path
9
+
10
+ # Arbitrary user-provided data in the form of a Ruby hash that's passed
11
+ # from subscribers on `request_begin` to subscribers on `request_end`.
12
+ # `request_begin` subscribers can set keys which will then be available
13
+ # in `request_end`.
14
+ #
15
+ # Note that all subscribers of `request_begin` share the same object, so
16
+ # they must be careful to set unique keys so as to not conflict with data
17
+ # set by other subscribers.
18
+ attr_reader :user_data
19
+
20
+ def initialize(method:, path:, user_data:)
21
+ @method = method
22
+ @path = path
23
+ @user_data = user_data
24
+ freeze
25
+ end
26
+ end
27
+
28
+ # Event emitted on `request_end` callback.
29
+ class RequestEndEvent
30
+ attr_reader :duration
31
+ attr_reader :http_status
32
+ attr_reader :method
33
+ attr_reader :num_retries
34
+ attr_reader :path
35
+
36
+ # Arbitrary user-provided data in the form of a Ruby hash that's passed
37
+ # from subscribers on `request_begin` to subscribers on `request_end`.
38
+ # `request_begin` subscribers can set keys which will then be available
39
+ # in `request_end`.
40
+ attr_reader :user_data
41
+
42
+ def initialize(duration:, http_status:, method:, num_retries:, path:,
43
+ user_data: nil)
44
+ @duration = duration
45
+ @http_status = http_status
46
+ @method = method
47
+ @num_retries = num_retries
48
+ @path = path
49
+ @user_data = user_data
50
+ freeze
51
+ end
52
+ end
53
+
54
+ # This class was renamed for consistency. This alias is here for backwards
55
+ # compatibility.
56
+ RequestEvent = RequestEndEvent
57
+
58
+ # Returns true if there are a non-zero number of subscribers on the given
59
+ # topic, and false otherwise.
60
+ def self.any_subscribers?(topic)
61
+ !subscribers[topic].empty?
62
+ end
63
+
64
+ def self.subscribe(topic, name = rand, &block)
65
+ subscribers[topic][name] = block
66
+ name
67
+ end
68
+
69
+ def self.unsubscribe(topic, name)
70
+ subscribers[topic].delete(name)
71
+ end
72
+
73
+ def self.notify(topic, event)
74
+ subscribers[topic].each_value { |subscriber| subscriber.call(event) }
75
+ end
76
+
77
+ def self.subscribers
78
+ @subscribers ||= Hash.new { |hash, key| hash[key] = {} }
79
+ end
80
+ private_class_method :subscribers
81
+ end
82
+ end
@@ -7,7 +7,7 @@ module Stripe
7
7
  include Stripe::APIOperations::Request
8
8
  include Stripe::APIOperations::Create
9
9
 
10
- OBJECT_NAME = "list".freeze
10
+ OBJECT_NAME = "list"
11
11
 
12
12
  # This accessor allows a `ListObject` to inherit various filters that were
13
13
  # given to a predecessor. This allows for things like consistent limits,
@@ -26,12 +26,16 @@ module Stripe
26
26
  self.filters = {}
27
27
  end
28
28
 
29
- def [](k)
30
- case k
29
+ def [](key)
30
+ case key
31
31
  when String, Symbol
32
32
  super
33
33
  else
34
- raise ArgumentError, "You tried to access the #{k.inspect} index, but ListObject types only support String keys. (HINT: List calls return an object with a 'data' (which is the data array). You likely want to call #data[#{k.inspect}])"
34
+ raise ArgumentError,
35
+ "You tried to access the #{key.inspect} index, but ListObject " \
36
+ "types only support String keys. (HINT: List calls return an " \
37
+ "object with a 'data' (which is the data array). You likely " \
38
+ "want to call #data[#{key.inspect}])"
35
39
  end
36
40
  end
37
41
 
@@ -47,6 +51,16 @@ module Stripe
47
51
  # Iterates through each resource in all pages, making additional fetches to
48
52
  # the API as necessary.
49
53
  #
54
+ # The default iteration direction is forwards according to Stripe's API
55
+ # "natural" ordering direction -- newer objects first, and moving towards
56
+ # older objects.
57
+ #
58
+ # However, if the initial list object was fetched using an `ending_before`
59
+ # cursor (and only `ending_before`, `starting_after` cannot also be
60
+ # included), the method assumes that the user is trying to iterate
61
+ # backwards compared to natural ordering and returns results that way --
62
+ # older objects first, and moving towards newer objects.
63
+ #
50
64
  # Note that this method will make as many API calls as necessary to fetch
51
65
  # all resources. For more granular control, please see +each+ and
52
66
  # +next_page+.
@@ -55,8 +69,18 @@ module Stripe
55
69
 
56
70
  page = self
57
71
  loop do
58
- page.each(&blk)
59
- page = page.next_page
72
+ # Backward iterating activates if we have an `ending_before` constraint
73
+ # and _just_ an `ending_before` constraint. If `starting_after` was
74
+ # also used, we iterate forwards normally.
75
+ if filters.include?(:ending_before) &&
76
+ !filters.include?(:starting_after)
77
+ page.reverse_each(&blk)
78
+ page = page.previous_page
79
+ else
80
+ page.each(&blk)
81
+ page = page.next_page
82
+ end
83
+
60
84
  break if page.empty?
61
85
  end
62
86
  end
@@ -68,7 +92,8 @@ module Stripe
68
92
 
69
93
  def retrieve(id, opts = {})
70
94
  id, retrieve_params = Util.normalize_id(id)
71
- resp, opts = request(:get, "#{resource_url}/#{CGI.escape(id)}", retrieve_params, opts)
95
+ url = "#{resource_url}/#{CGI.escape(id)}"
96
+ resp, opts = execute_resource_request(:get, url, retrieve_params, opts)
72
97
  Util.convert_to_stripe_object(resp.data, opts)
73
98
  end
74
99
 
@@ -78,6 +103,7 @@ module Stripe
78
103
  # was given, the default limit will be fetched again.
79
104
  def next_page(params = {}, opts = {})
80
105
  return self.class.empty_list(opts) unless has_more
106
+
81
107
  last_id = data.last.id
82
108
 
83
109
  params = filters.merge(starting_after: last_id).merge(params)
@@ -90,6 +116,8 @@ module Stripe
90
116
  # This method will try to respect the limit of the current page. If none
91
117
  # was given, the default limit will be fetched again.
92
118
  def previous_page(params = {}, opts = {})
119
+ return self.class.empty_list(opts) unless has_more
120
+
93
121
  first_id = data.first.id
94
122
 
95
123
  params = filters.merge(ending_before: first_id).merge(params)
@@ -101,5 +129,11 @@ module Stripe
101
129
  url ||
102
130
  raise(ArgumentError, "List object does not contain a 'url' field.")
103
131
  end
132
+
133
+ # Iterates through each resource in the page represented by the current
134
+ # `ListObject` in reverse.
135
+ def reverse_each(&blk)
136
+ data.reverse_each(&blk)
137
+ end
104
138
  end
105
139
  end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+ require "tempfile"
5
+
6
+ module Stripe
7
+ # Encodes parameters into a `multipart/form-data` payload as described by RFC
8
+ # 2388:
9
+ #
10
+ # https://tools.ietf.org/html/rfc2388
11
+ #
12
+ # This is most useful for transferring file-like objects.
13
+ #
14
+ # Parameters should be added with `#encode`. When ready, use `#body` to get
15
+ # the encoded result and `#content_type` to get the value that should be
16
+ # placed in the `Content-Type` header of a subsequent request (which includes
17
+ # a boundary value).
18
+ class MultipartEncoder
19
+ MULTIPART_FORM_DATA = "multipart/form-data"
20
+
21
+ # A shortcut for encoding a single set of parameters and finalizing a
22
+ # result.
23
+ #
24
+ # Returns an encoded body and the value that should be set in the content
25
+ # type header of a subsequent request.
26
+ def self.encode(params)
27
+ encoder = MultipartEncoder.new
28
+ encoder.encode(params)
29
+ encoder.close
30
+ [encoder.body, encoder.content_type]
31
+ end
32
+
33
+ # Gets the object's randomly generated boundary string.
34
+ attr_reader :boundary
35
+
36
+ # Initializes a new multipart encoder.
37
+ def initialize
38
+ # Kind of weird, but required by Rubocop because the unary plus operator
39
+ # is considered faster than `Stripe.new`.
40
+ @body = +""
41
+
42
+ # Chose the same number of random bytes that Go uses in its standard
43
+ # library implementation. Easily enough entropy to ensure that it won't
44
+ # be present in a file we're sending.
45
+ @boundary = SecureRandom.hex(30)
46
+
47
+ @closed = false
48
+ @first_field = true
49
+ end
50
+
51
+ # Gets the encoded body. `#close` must be called first.
52
+ def body
53
+ raise "object must be closed before getting body" unless @closed
54
+
55
+ @body
56
+ end
57
+
58
+ # Finalizes the object by writing the final boundary.
59
+ def close
60
+ raise "object already closed" if @closed
61
+
62
+ @body << "\r\n"
63
+ @body << "--#{@boundary}--"
64
+
65
+ @closed = true
66
+
67
+ nil
68
+ end
69
+
70
+ # Gets the value including boundary that should be put into a multipart
71
+ # request's `Content-Type`.
72
+ def content_type
73
+ "#{MULTIPART_FORM_DATA}; boundary=#{@boundary}"
74
+ end
75
+
76
+ # Encodes a set of parameters to the body.
77
+ #
78
+ # Note that parameters are expected to be a hash, but a "flat" hash such
79
+ # that complex substructures like hashes and arrays have already been
80
+ # appropriately Stripe-encoded. Pass a complex structure through
81
+ # `Util.flatten_params` first before handing it off to this method.
82
+ def encode(params)
83
+ raise "no more parameters can be written to closed object" if @closed
84
+
85
+ params.each do |name, val|
86
+ if val.is_a?(::File) || val.is_a?(::Tempfile)
87
+ write_field(name, val.read, filename: ::File.basename(val.path))
88
+ elsif val.respond_to?(:read)
89
+ write_field(name, val.read, filename: "blob")
90
+ else
91
+ write_field(name, val, filename: nil)
92
+ end
93
+ end
94
+
95
+ nil
96
+ end
97
+
98
+ #
99
+ # private
100
+ #
101
+
102
+ # Escapes double quotes so that the given value can be used in a
103
+ # double-quoted string and replaces any linebreak characters with spaces.
104
+ private def escape(str)
105
+ str.gsub('"', "%22").tr("\n", " ").tr("\r", " ")
106
+ end
107
+
108
+ private def write_field(name, data, filename:)
109
+ if !@first_field
110
+ @body << "\r\n"
111
+ else
112
+ @first_field = false
113
+ end
114
+
115
+ @body << "--#{@boundary}\r\n"
116
+
117
+ if filename
118
+ @body << %(Content-Disposition: form-data) +
119
+ %(; name="#{escape(name.to_s)}") +
120
+ %(; filename="#{escape(filename)}"\r\n)
121
+ @body << %(Content-Type: application/octet-stream\r\n)
122
+ else
123
+ @body << %(Content-Disposition: form-data) +
124
+ %(; name="#{escape(name.to_s)}"\r\n)
125
+ end
126
+
127
+ @body << "\r\n"
128
+ @body << data.to_s
129
+ end
130
+ end
131
+ end