checkout-intents 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (137) hide show
  1. checksums.yaml +7 -0
  2. data/.ignore +2 -0
  3. data/CHANGELOG.md +16 -0
  4. data/README.md +358 -0
  5. data/SECURITY.md +27 -0
  6. data/lib/checkout_intents/client.rb +100 -0
  7. data/lib/checkout_intents/errors.rb +228 -0
  8. data/lib/checkout_intents/file_part.rb +58 -0
  9. data/lib/checkout_intents/internal/cursor_pagination.rb +125 -0
  10. data/lib/checkout_intents/internal/transport/base_client.rb +577 -0
  11. data/lib/checkout_intents/internal/transport/pooled_net_requester.rb +210 -0
  12. data/lib/checkout_intents/internal/type/array_of.rb +168 -0
  13. data/lib/checkout_intents/internal/type/base_model.rb +536 -0
  14. data/lib/checkout_intents/internal/type/base_page.rb +55 -0
  15. data/lib/checkout_intents/internal/type/boolean.rb +77 -0
  16. data/lib/checkout_intents/internal/type/converter.rb +327 -0
  17. data/lib/checkout_intents/internal/type/enum.rb +131 -0
  18. data/lib/checkout_intents/internal/type/file_input.rb +111 -0
  19. data/lib/checkout_intents/internal/type/hash_of.rb +188 -0
  20. data/lib/checkout_intents/internal/type/request_parameters.rb +42 -0
  21. data/lib/checkout_intents/internal/type/union.rb +258 -0
  22. data/lib/checkout_intents/internal/type/unknown.rb +81 -0
  23. data/lib/checkout_intents/internal/util.rb +920 -0
  24. data/lib/checkout_intents/internal.rb +20 -0
  25. data/lib/checkout_intents/models/base_checkout_intent.rb +76 -0
  26. data/lib/checkout_intents/models/betas/checkout_session_create_params.rb +148 -0
  27. data/lib/checkout_intents/models/brand_retrieve_params.rb +14 -0
  28. data/lib/checkout_intents/models/brand_retrieve_response.rb +52 -0
  29. data/lib/checkout_intents/models/buyer.rb +69 -0
  30. data/lib/checkout_intents/models/checkout_intent.rb +230 -0
  31. data/lib/checkout_intents/models/checkout_intent_add_payment_params.rb +20 -0
  32. data/lib/checkout_intents/models/checkout_intent_confirm_params.rb +20 -0
  33. data/lib/checkout_intents/models/checkout_intent_create_params.rb +68 -0
  34. data/lib/checkout_intents/models/checkout_intent_list_params.rb +58 -0
  35. data/lib/checkout_intents/models/checkout_intent_purchase_params.rb +74 -0
  36. data/lib/checkout_intents/models/checkout_intent_retrieve_params.rb +14 -0
  37. data/lib/checkout_intents/models/checkout_session.rb +23 -0
  38. data/lib/checkout_intents/models/money.rb +21 -0
  39. data/lib/checkout_intents/models/offer.rb +113 -0
  40. data/lib/checkout_intents/models/payment_method.rb +120 -0
  41. data/lib/checkout_intents/models/variant_selection.rb +33 -0
  42. data/lib/checkout_intents/models.rb +76 -0
  43. data/lib/checkout_intents/request_options.rb +78 -0
  44. data/lib/checkout_intents/resources/betas/checkout_sessions.rb +54 -0
  45. data/lib/checkout_intents/resources/betas.rb +18 -0
  46. data/lib/checkout_intents/resources/brands.rb +37 -0
  47. data/lib/checkout_intents/resources/checkout_intents.rb +173 -0
  48. data/lib/checkout_intents/version.rb +5 -0
  49. data/lib/checkout_intents.rb +77 -0
  50. data/manifest.yaml +17 -0
  51. data/rbi/checkout_intents/client.rbi +74 -0
  52. data/rbi/checkout_intents/errors.rbi +205 -0
  53. data/rbi/checkout_intents/file_part.rbi +37 -0
  54. data/rbi/checkout_intents/internal/cursor_pagination.rbi +74 -0
  55. data/rbi/checkout_intents/internal/transport/base_client.rbi +309 -0
  56. data/rbi/checkout_intents/internal/transport/pooled_net_requester.rbi +84 -0
  57. data/rbi/checkout_intents/internal/type/array_of.rbi +108 -0
  58. data/rbi/checkout_intents/internal/type/base_model.rbi +314 -0
  59. data/rbi/checkout_intents/internal/type/base_page.rbi +43 -0
  60. data/rbi/checkout_intents/internal/type/boolean.rbi +58 -0
  61. data/rbi/checkout_intents/internal/type/converter.rbi +225 -0
  62. data/rbi/checkout_intents/internal/type/enum.rbi +82 -0
  63. data/rbi/checkout_intents/internal/type/file_input.rbi +59 -0
  64. data/rbi/checkout_intents/internal/type/hash_of.rbi +108 -0
  65. data/rbi/checkout_intents/internal/type/request_parameters.rbi +31 -0
  66. data/rbi/checkout_intents/internal/type/union.rbi +134 -0
  67. data/rbi/checkout_intents/internal/type/unknown.rbi +58 -0
  68. data/rbi/checkout_intents/internal/util.rbi +487 -0
  69. data/rbi/checkout_intents/internal.rbi +18 -0
  70. data/rbi/checkout_intents/models/base_checkout_intent.rbi +142 -0
  71. data/rbi/checkout_intents/models/betas/checkout_session_create_params.rbi +281 -0
  72. data/rbi/checkout_intents/models/brand_retrieve_params.rbi +32 -0
  73. data/rbi/checkout_intents/models/brand_retrieve_response.rbi +109 -0
  74. data/rbi/checkout_intents/models/buyer.rbi +92 -0
  75. data/rbi/checkout_intents/models/checkout_intent.rbi +653 -0
  76. data/rbi/checkout_intents/models/checkout_intent_add_payment_params.rbi +59 -0
  77. data/rbi/checkout_intents/models/checkout_intent_confirm_params.rbi +59 -0
  78. data/rbi/checkout_intents/models/checkout_intent_create_params.rbi +141 -0
  79. data/rbi/checkout_intents/models/checkout_intent_list_params.rbi +146 -0
  80. data/rbi/checkout_intents/models/checkout_intent_purchase_params.rbi +165 -0
  81. data/rbi/checkout_intents/models/checkout_intent_retrieve_params.rbi +32 -0
  82. data/rbi/checkout_intents/models/checkout_session.rbi +36 -0
  83. data/rbi/checkout_intents/models/money.rbi +32 -0
  84. data/rbi/checkout_intents/models/offer.rbi +225 -0
  85. data/rbi/checkout_intents/models/payment_method.rbi +292 -0
  86. data/rbi/checkout_intents/models/variant_selection.rbi +55 -0
  87. data/rbi/checkout_intents/models.rbi +40 -0
  88. data/rbi/checkout_intents/request_options.rbi +64 -0
  89. data/rbi/checkout_intents/resources/betas/checkout_sessions.rbi +47 -0
  90. data/rbi/checkout_intents/resources/betas.rbi +15 -0
  91. data/rbi/checkout_intents/resources/brands.rbi +29 -0
  92. data/rbi/checkout_intents/resources/checkout_intents.rbi +165 -0
  93. data/rbi/checkout_intents/version.rbi +5 -0
  94. data/sig/checkout_intents/client.rbs +36 -0
  95. data/sig/checkout_intents/errors.rbs +117 -0
  96. data/sig/checkout_intents/file_part.rbs +21 -0
  97. data/sig/checkout_intents/internal/cursor_pagination.rbs +48 -0
  98. data/sig/checkout_intents/internal/transport/base_client.rbs +133 -0
  99. data/sig/checkout_intents/internal/transport/pooled_net_requester.rbs +48 -0
  100. data/sig/checkout_intents/internal/type/array_of.rbs +48 -0
  101. data/sig/checkout_intents/internal/type/base_model.rbs +104 -0
  102. data/sig/checkout_intents/internal/type/base_page.rbs +24 -0
  103. data/sig/checkout_intents/internal/type/boolean.rbs +26 -0
  104. data/sig/checkout_intents/internal/type/converter.rbs +79 -0
  105. data/sig/checkout_intents/internal/type/enum.rbs +32 -0
  106. data/sig/checkout_intents/internal/type/file_input.rbs +25 -0
  107. data/sig/checkout_intents/internal/type/hash_of.rbs +48 -0
  108. data/sig/checkout_intents/internal/type/request_parameters.rbs +20 -0
  109. data/sig/checkout_intents/internal/type/union.rbs +52 -0
  110. data/sig/checkout_intents/internal/type/unknown.rbs +26 -0
  111. data/sig/checkout_intents/internal/util.rbs +185 -0
  112. data/sig/checkout_intents/internal.rbs +10 -0
  113. data/sig/checkout_intents/models/base_checkout_intent.rbs +88 -0
  114. data/sig/checkout_intents/models/betas/checkout_session_create_params.rbs +172 -0
  115. data/sig/checkout_intents/models/brand_retrieve_params.rbs +15 -0
  116. data/sig/checkout_intents/models/brand_retrieve_response.rbs +43 -0
  117. data/sig/checkout_intents/models/buyer.rbs +67 -0
  118. data/sig/checkout_intents/models/checkout_intent.rbs +324 -0
  119. data/sig/checkout_intents/models/checkout_intent_add_payment_params.rbs +24 -0
  120. data/sig/checkout_intents/models/checkout_intent_confirm_params.rbs +24 -0
  121. data/sig/checkout_intents/models/checkout_intent_create_params.rbs +84 -0
  122. data/sig/checkout_intents/models/checkout_intent_list_params.rbs +77 -0
  123. data/sig/checkout_intents/models/checkout_intent_purchase_params.rbs +89 -0
  124. data/sig/checkout_intents/models/checkout_intent_retrieve_params.rbs +15 -0
  125. data/sig/checkout_intents/models/checkout_session.rbs +13 -0
  126. data/sig/checkout_intents/models/money.rbs +15 -0
  127. data/sig/checkout_intents/models/offer.rbs +135 -0
  128. data/sig/checkout_intents/models/payment_method.rbs +127 -0
  129. data/sig/checkout_intents/models/variant_selection.rbs +30 -0
  130. data/sig/checkout_intents/models.rbs +33 -0
  131. data/sig/checkout_intents/request_options.rbs +36 -0
  132. data/sig/checkout_intents/resources/betas/checkout_sessions.rbs +19 -0
  133. data/sig/checkout_intents/resources/betas.rbs +9 -0
  134. data/sig/checkout_intents/resources/brands.rbs +12 -0
  135. data/sig/checkout_intents/resources/checkout_intents.rbs +54 -0
  136. data/sig/checkout_intents/version.rbs +3 -0
  137. metadata +194 -0
@@ -0,0 +1,536 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheckoutIntents
4
+ module Internal
5
+ module Type
6
+ # @abstract
7
+ class BaseModel
8
+ extend CheckoutIntents::Internal::Type::Converter
9
+ extend CheckoutIntents::Internal::Util::SorbetRuntimeSupport
10
+
11
+ class << self
12
+ # @api private
13
+ #
14
+ # Assumes superclass fields are totally defined before fields are accessed /
15
+ # defined on subclasses.
16
+ #
17
+ # @param child [Class<CheckoutIntents::Internal::Type::BaseModel>]
18
+ def inherited(child)
19
+ super
20
+ child.known_fields.replace(known_fields.dup)
21
+ end
22
+
23
+ # @api private
24
+ #
25
+ # @return [Hash{Symbol=>Hash{Symbol=>Object}}]
26
+ def known_fields = @known_fields ||= {}
27
+
28
+ # @api private
29
+ #
30
+ # @return [Hash{Symbol=>Hash{Symbol=>Object}}]
31
+ def fields
32
+ known_fields.transform_values do |field|
33
+ {**field.except(:type_fn), type: field.fetch(:type_fn).call}
34
+ end
35
+ end
36
+
37
+ # @api private
38
+ #
39
+ # @param name_sym [Symbol]
40
+ #
41
+ # @param required [Boolean]
42
+ #
43
+ # @param type_info [Hash{Symbol=>Object}, Proc, CheckoutIntents::Internal::Type::Converter, Class]
44
+ #
45
+ # @param spec [Hash{Symbol=>Object}] .
46
+ #
47
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
48
+ #
49
+ # @option spec [Proc] :enum
50
+ #
51
+ # @option spec [Proc] :union
52
+ #
53
+ # @option spec [Boolean] :"nil?"
54
+ private def add_field(name_sym, required:, type_info:, spec:)
55
+ meta = CheckoutIntents::Internal::Type::Converter.meta_info(type_info, spec)
56
+ type_fn, info =
57
+ case type_info
58
+ in Proc | CheckoutIntents::Internal::Type::Converter | Class
59
+ [CheckoutIntents::Internal::Type::Converter.type_info({**spec, union: type_info}), spec]
60
+ in Hash
61
+ [CheckoutIntents::Internal::Type::Converter.type_info(type_info), type_info]
62
+ end
63
+
64
+ setter = :"#{name_sym}="
65
+ api_name = info.fetch(:api_name, name_sym)
66
+ nilable = info.fetch(:nil?, false)
67
+ const = required && !nilable ? info.fetch(:const, CheckoutIntents::Internal::OMIT) : CheckoutIntents::Internal::OMIT
68
+
69
+ [name_sym, setter].each { undef_method(_1) } if known_fields.key?(name_sym)
70
+
71
+ known_fields[name_sym] =
72
+ {
73
+ mode: @mode,
74
+ api_name: api_name,
75
+ required: required,
76
+ nilable: nilable,
77
+ const: const,
78
+ type_fn: type_fn,
79
+ meta: meta
80
+ }
81
+
82
+ define_method(setter) do |value|
83
+ target = type_fn.call
84
+ state = CheckoutIntents::Internal::Type::Converter.new_coerce_state(translate_names: false)
85
+ coerced = CheckoutIntents::Internal::Type::Converter.coerce(target, value, state: state)
86
+ status = @coerced.store(name_sym, state.fetch(:error) || true)
87
+ stored =
88
+ case [target, status]
89
+ in [CheckoutIntents::Internal::Type::Converter | Symbol, true]
90
+ coerced
91
+ else
92
+ value
93
+ end
94
+ @data.store(name_sym, stored)
95
+ end
96
+
97
+ # rubocop:disable Style/CaseEquality
98
+ # rubocop:disable Metrics/BlockLength
99
+ define_method(name_sym) do
100
+ target = type_fn.call
101
+
102
+ case @coerced[name_sym]
103
+ in true | false if CheckoutIntents::Internal::Type::Converter === target
104
+ @data.fetch(name_sym)
105
+ in ::StandardError => e
106
+ raise CheckoutIntents::Errors::ConversionError.new(
107
+ on: self.class,
108
+ method: __method__,
109
+ target: target,
110
+ value: @data.fetch(name_sym),
111
+ cause: e
112
+ )
113
+ else
114
+ Kernel.then do
115
+ value = @data.fetch(name_sym) { const == CheckoutIntents::Internal::OMIT ? nil : const }
116
+ state = CheckoutIntents::Internal::Type::Converter.new_coerce_state(translate_names: false)
117
+ if (nilable || !required) && value.nil?
118
+ nil
119
+ else
120
+ CheckoutIntents::Internal::Type::Converter.coerce(
121
+ target, value, state: state
122
+ )
123
+ end
124
+ rescue StandardError => e
125
+ raise CheckoutIntents::Errors::ConversionError.new(
126
+ on: self.class,
127
+ method: __method__,
128
+ target: target,
129
+ value: value,
130
+ cause: e
131
+ )
132
+ end
133
+ end
134
+ end
135
+ # rubocop:enable Metrics/BlockLength
136
+ # rubocop:enable Style/CaseEquality
137
+ end
138
+
139
+ # @api private
140
+ #
141
+ # @param name_sym [Symbol]
142
+ #
143
+ # @param type_info [Hash{Symbol=>Object}, Proc, CheckoutIntents::Internal::Type::Converter, Class]
144
+ #
145
+ # @param spec [Hash{Symbol=>Object}] .
146
+ #
147
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
148
+ #
149
+ # @option spec [Proc] :enum
150
+ #
151
+ # @option spec [Proc] :union
152
+ #
153
+ # @option spec [Boolean] :"nil?"
154
+ def required(name_sym, type_info, spec = {})
155
+ add_field(name_sym, required: true, type_info: type_info, spec: spec)
156
+ end
157
+
158
+ # @api private
159
+ #
160
+ # @param name_sym [Symbol]
161
+ #
162
+ # @param type_info [Hash{Symbol=>Object}, Proc, CheckoutIntents::Internal::Type::Converter, Class]
163
+ #
164
+ # @param spec [Hash{Symbol=>Object}] .
165
+ #
166
+ # @option spec [NilClass, TrueClass, FalseClass, Integer, Float, Symbol] :const
167
+ #
168
+ # @option spec [Proc] :enum
169
+ #
170
+ # @option spec [Proc] :union
171
+ #
172
+ # @option spec [Boolean] :"nil?"
173
+ def optional(name_sym, type_info, spec = {})
174
+ add_field(name_sym, required: false, type_info: type_info, spec: spec)
175
+ end
176
+
177
+ # @api private
178
+ #
179
+ # `request_only` attributes not excluded from `.#coerce` when receiving responses
180
+ # even if well behaved servers should not send them
181
+ #
182
+ # @param blk [Proc]
183
+ private def request_only(&blk)
184
+ @mode = :dump
185
+ blk.call
186
+ ensure
187
+ @mode = nil
188
+ end
189
+
190
+ # @api private
191
+ #
192
+ # `response_only` attributes are omitted from `.#dump` when making requests
193
+ #
194
+ # @param blk [Proc]
195
+ private def response_only(&blk)
196
+ @mode = :coerce
197
+ blk.call
198
+ ensure
199
+ @mode = nil
200
+ end
201
+
202
+ # @api public
203
+ #
204
+ # @param other [Object]
205
+ #
206
+ # @return [Boolean]
207
+ def ==(other)
208
+ # rubocop:disable Layout/LineLength
209
+ other.is_a?(Class) && other <= CheckoutIntents::Internal::Type::BaseModel && other.fields == fields
210
+ # rubocop:enable Layout/LineLength
211
+ end
212
+
213
+ # @api public
214
+ #
215
+ # @return [Integer]
216
+ def hash = fields.hash
217
+ end
218
+
219
+ # @api public
220
+ #
221
+ # @param other [Object]
222
+ #
223
+ # @return [Boolean]
224
+ def ==(other) = self.class == other.class && @data == other.to_h
225
+
226
+ # @api public
227
+ #
228
+ # @return [Integer]
229
+ def hash = [self.class, @data].hash
230
+
231
+ class << self
232
+ # @api private
233
+ #
234
+ # @param value [CheckoutIntents::Internal::Type::BaseModel, Hash{Object=>Object}, Object]
235
+ #
236
+ # @param state [Hash{Symbol=>Object}] .
237
+ #
238
+ # @option state [Boolean] :translate_names
239
+ #
240
+ # @option state [Boolean] :strictness
241
+ #
242
+ # @option state [Hash{Symbol=>Object}] :exactness
243
+ #
244
+ # @option state [Class<StandardError>] :error
245
+ #
246
+ # @option state [Integer] :branched
247
+ #
248
+ # @return [self, Object]
249
+ def coerce(value, state:)
250
+ exactness = state.fetch(:exactness)
251
+
252
+ if value.is_a?(self)
253
+ exactness[:yes] += 1
254
+ return value
255
+ end
256
+
257
+ unless (val = CheckoutIntents::Internal::Util.coerce_hash(value)).is_a?(Hash)
258
+ exactness[:no] += 1
259
+ state[:error] = TypeError.new("#{value.class} can't be coerced into #{Hash}")
260
+ return value
261
+ end
262
+ exactness[:yes] += 1
263
+
264
+ keys = val.keys.to_set
265
+ instance = new
266
+ data = instance.to_h
267
+ status = instance.instance_variable_get(:@coerced)
268
+
269
+ # rubocop:disable Metrics/BlockLength
270
+ fields.each do |name, field|
271
+ mode, required, target = field.fetch_values(:mode, :required, :type)
272
+ api_name, nilable, const = field.fetch_values(:api_name, :nilable, :const)
273
+ src_name = state.fetch(:translate_names) ? api_name : name
274
+
275
+ unless val.key?(src_name)
276
+ if required && mode != :dump && const == CheckoutIntents::Internal::OMIT
277
+ exactness[nilable ? :maybe : :no] += 1
278
+ else
279
+ exactness[:yes] += 1
280
+ end
281
+ next
282
+ end
283
+
284
+ item = val.fetch(src_name)
285
+ keys.delete(src_name)
286
+
287
+ state[:error] = nil
288
+ converted =
289
+ if item.nil? && (nilable || !required)
290
+ exactness[nilable ? :yes : :maybe] += 1
291
+ nil
292
+ else
293
+ coerced = CheckoutIntents::Internal::Type::Converter.coerce(target, item, state: state)
294
+ case target
295
+ in CheckoutIntents::Internal::Type::Converter | Symbol
296
+ coerced
297
+ else
298
+ item
299
+ end
300
+ end
301
+
302
+ status.store(name, state.fetch(:error) || true)
303
+ data.store(name, converted)
304
+ end
305
+ # rubocop:enable Metrics/BlockLength
306
+
307
+ keys.each { data.store(_1, val.fetch(_1)) }
308
+ instance
309
+ end
310
+
311
+ # @api private
312
+ #
313
+ # @param value [self, Object]
314
+ #
315
+ # @param state [Hash{Symbol=>Object}] .
316
+ #
317
+ # @option state [Boolean] :can_retry
318
+ #
319
+ # @return [Hash{Object=>Object}, Object]
320
+ def dump(value, state:)
321
+ unless (coerced = CheckoutIntents::Internal::Util.coerce_hash(value)).is_a?(Hash)
322
+ return super
323
+ end
324
+
325
+ acc = {}
326
+
327
+ coerced.each do |key, val|
328
+ name = key.is_a?(String) ? key.to_sym : key
329
+ case (field = known_fields[name])
330
+ in nil
331
+ acc.store(name, super(val, state: state))
332
+ else
333
+ api_name, mode, type_fn = field.fetch_values(:api_name, :mode, :type_fn)
334
+ case mode
335
+ in :coerce
336
+ next
337
+ else
338
+ target = type_fn.call
339
+ acc.store(
340
+ api_name,
341
+ CheckoutIntents::Internal::Type::Converter.dump(target, val, state: state)
342
+ )
343
+ end
344
+ end
345
+ end
346
+
347
+ known_fields.each_value do |field|
348
+ api_name, mode, const = field.fetch_values(:api_name, :mode, :const)
349
+ next if mode == :coerce || acc.key?(api_name) || const == CheckoutIntents::Internal::OMIT
350
+ acc.store(api_name, const)
351
+ end
352
+
353
+ acc
354
+ end
355
+
356
+ # @api private
357
+ #
358
+ # @return [Object]
359
+ def to_sorbet_type
360
+ self
361
+ end
362
+ end
363
+
364
+ class << self
365
+ # @api private
366
+ #
367
+ # @param model [CheckoutIntents::Internal::Type::BaseModel]
368
+ # @param convert [Boolean]
369
+ #
370
+ # @return [Hash{Symbol=>Object}]
371
+ def recursively_to_h(model, convert:)
372
+ rec = ->(x) do
373
+ case x
374
+ in CheckoutIntents::Internal::Type::BaseModel
375
+ if convert
376
+ fields = x.class.known_fields
377
+ x.to_h.to_h do |key, val|
378
+ [key, rec.call(fields.key?(key) ? x.public_send(key) : val)]
379
+ rescue CheckoutIntents::Errors::ConversionError
380
+ [key, rec.call(val)]
381
+ end
382
+ else
383
+ rec.call(x.to_h)
384
+ end
385
+ in Hash
386
+ x.transform_values(&rec)
387
+ in Array
388
+ x.map(&rec)
389
+ else
390
+ x
391
+ end
392
+ end
393
+ rec.call(model)
394
+ end
395
+ end
396
+
397
+ # @api public
398
+ #
399
+ # Returns the raw value associated with the given key, if found. Otherwise, nil is
400
+ # returned.
401
+ #
402
+ # It is valid to lookup keys that are not in the API spec, for example to access
403
+ # undocumented features. This method does not parse response data into
404
+ # higher-level types. Lookup by anything other than a Symbol is an ArgumentError.
405
+ #
406
+ # @param key [Symbol]
407
+ #
408
+ # @return [Object, nil]
409
+ def [](key)
410
+ unless key.instance_of?(Symbol)
411
+ raise ArgumentError.new("Expected symbol key for lookup, got #{key.inspect}")
412
+ end
413
+
414
+ @data[key]
415
+ end
416
+
417
+ # @api public
418
+ #
419
+ # Returns a Hash of the data underlying this object. O(1)
420
+ #
421
+ # Keys are Symbols and values are the raw values from the response. The return
422
+ # value indicates which values were ever set on the object. i.e. there will be a
423
+ # key in this hash if they ever were, even if the set value was nil.
424
+ #
425
+ # This method is not recursive. The returned value is shared by the object, so it
426
+ # should not be mutated.
427
+ #
428
+ # @return [Hash{Symbol=>Object}]
429
+ def to_h = @data
430
+
431
+ alias_method :to_hash, :to_h
432
+
433
+ # @api public
434
+ #
435
+ # In addition to the behaviour of `#to_h`, this method will recursively call
436
+ # `#to_h` on nested models.
437
+ #
438
+ # @return [Hash{Symbol=>Object}]
439
+ def deep_to_h = self.class.recursively_to_h(@data, convert: false)
440
+
441
+ # @param keys [Array<Symbol>, nil]
442
+ #
443
+ # @return [Hash{Symbol=>Object}]
444
+ #
445
+ # @example
446
+ # # `base_checkout_intent` is a `CheckoutIntents::BaseCheckoutIntent`
447
+ # base_checkout_intent => {
448
+ # id: id,
449
+ # buyer: buyer,
450
+ # created_at: created_at
451
+ # }
452
+ def deconstruct_keys(keys)
453
+ (keys || self.class.known_fields.keys)
454
+ .filter_map do |k|
455
+ unless self.class.known_fields.key?(k)
456
+ next
457
+ end
458
+
459
+ [k, public_send(k)]
460
+ end
461
+ .to_h
462
+ end
463
+
464
+ # @api public
465
+ #
466
+ # @param a [Object]
467
+ #
468
+ # @return [String]
469
+ def to_json(*a) = CheckoutIntents::Internal::Type::Converter.dump(self.class, self).to_json(*a)
470
+
471
+ # @api public
472
+ #
473
+ # @param a [Object]
474
+ #
475
+ # @return [String]
476
+ def to_yaml(*a) = CheckoutIntents::Internal::Type::Converter.dump(self.class, self).to_yaml(*a)
477
+
478
+ # Create a new instance of a model.
479
+ #
480
+ # @param data [Hash{Symbol=>Object}, self]
481
+ def initialize(data = {})
482
+ @data = {}
483
+ @coerced = {}
484
+ CheckoutIntents::Internal::Util.coerce_hash!(data).each do
485
+ if self.class.known_fields.key?(_1)
486
+ public_send(:"#{_1}=", _2)
487
+ else
488
+ @data.store(_1, _2)
489
+ @coerced.store(_1, false)
490
+ end
491
+ end
492
+ end
493
+
494
+ class << self
495
+ # @api private
496
+ #
497
+ # @param depth [Integer]
498
+ #
499
+ # @return [String]
500
+ def inspect(depth: 0)
501
+ return super() if depth.positive?
502
+
503
+ depth = depth.succ
504
+ deferred = fields.transform_values do |field|
505
+ type, required, nilable = field.fetch_values(:type, :required, :nilable)
506
+ inspected = [
507
+ CheckoutIntents::Internal::Type::Converter.inspect(type, depth: depth),
508
+ !required || nilable ? "nil" : nil
509
+ ].compact.join(" | ")
510
+ -> { inspected }.tap { _1.define_singleton_method(:inspect) { call } }
511
+ end
512
+
513
+ "#{name}[#{deferred.inspect}]"
514
+ end
515
+ end
516
+
517
+ # @api public
518
+ #
519
+ # @return [String]
520
+ def to_s = deep_to_h.to_s
521
+
522
+ # @api private
523
+ #
524
+ # @return [String]
525
+ def inspect
526
+ converted = self.class.recursively_to_h(self, convert: true)
527
+ "#<#{self.class}:0x#{object_id.to_s(16)} #{converted}>"
528
+ end
529
+
530
+ define_sorbet_constant!(:KnownField) do
531
+ T.type_alias { {mode: T.nilable(Symbol), required: T::Boolean, nilable: T::Boolean} }
532
+ end
533
+ end
534
+ end
535
+ end
536
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheckoutIntents
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ #
8
+ # @generic Elem
9
+ #
10
+ # This module provides a base implementation for paginated responses in the SDK.
11
+ module BasePage
12
+ # rubocop:disable Lint/UnusedMethodArgument
13
+
14
+ # @api public
15
+ #
16
+ # @return [Boolean]
17
+ def next_page? = (raise NotImplementedError)
18
+
19
+ # @api public
20
+ #
21
+ # @raise [CheckoutIntents::Errors::APIError]
22
+ # @return [self]
23
+ def next_page = (raise NotImplementedError)
24
+
25
+ # @api public
26
+ #
27
+ # @param blk [Proc]
28
+ #
29
+ # @yieldparam [generic<Elem>]
30
+ # @return [void]
31
+ def auto_paging_each(&blk) = (raise NotImplementedError)
32
+
33
+ # @return [Enumerable<generic<Elem>>]
34
+ def to_enum = super(:auto_paging_each)
35
+
36
+ alias_method :enum_for, :to_enum
37
+
38
+ # @api private
39
+ #
40
+ # @param client [CheckoutIntents::Internal::Transport::BaseClient]
41
+ # @param req [Hash{Symbol=>Object}]
42
+ # @param headers [Hash{String=>String}]
43
+ # @param page_data [Object]
44
+ def initialize(client:, req:, headers:, page_data:)
45
+ @client = client
46
+ @req = req
47
+ @model = req.fetch(:model)
48
+ super()
49
+ end
50
+
51
+ # rubocop:enable Lint/UnusedMethodArgument
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheckoutIntents
4
+ module Internal
5
+ module Type
6
+ # @api private
7
+ #
8
+ # @abstract
9
+ #
10
+ # Ruby has no Boolean class; this is something for models to refer to.
11
+ class Boolean
12
+ extend CheckoutIntents::Internal::Type::Converter
13
+ extend CheckoutIntents::Internal::Util::SorbetRuntimeSupport
14
+
15
+ private_class_method :new
16
+
17
+ # @api public
18
+ #
19
+ # @param other [Object]
20
+ #
21
+ # @return [Boolean]
22
+ def self.===(other) = other == true || other == false
23
+
24
+ # @api public
25
+ #
26
+ # @param other [Object]
27
+ #
28
+ # @return [Boolean]
29
+ def self.==(other) = other.is_a?(Class) && other <= CheckoutIntents::Internal::Type::Boolean
30
+
31
+ class << self
32
+ # @api private
33
+ #
34
+ # Coerce value to Boolean if possible, otherwise return the original value.
35
+ #
36
+ # @param value [Boolean, Object]
37
+ #
38
+ # @param state [Hash{Symbol=>Object}] .
39
+ #
40
+ # @option state [Boolean] :translate_names
41
+ #
42
+ # @option state [Boolean] :strictness
43
+ #
44
+ # @option state [Hash{Symbol=>Object}] :exactness
45
+ #
46
+ # @option state [Class<StandardError>] :error
47
+ #
48
+ # @option state [Integer] :branched
49
+ #
50
+ # @return [Boolean, Object]
51
+ def coerce(value, state:)
52
+ state.fetch(:exactness)[value == true || value == false ? :yes : :no] += 1
53
+ value
54
+ end
55
+
56
+ # @!method dump(value, state:)
57
+ # @api private
58
+ #
59
+ # @param value [Boolean, Object]
60
+ #
61
+ # @param state [Hash{Symbol=>Object}] .
62
+ #
63
+ # @option state [Boolean] :can_retry
64
+ #
65
+ # @return [Boolean, Object]
66
+
67
+ # @api private
68
+ #
69
+ # @return [Object]
70
+ def to_sorbet_type
71
+ T::Boolean
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end