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,228 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheckoutIntents
4
+ module Errors
5
+ class Error < StandardError
6
+ # @!attribute cause
7
+ #
8
+ # @return [StandardError, nil]
9
+ end
10
+
11
+ class ConversionError < CheckoutIntents::Errors::Error
12
+ # @return [StandardError, nil]
13
+ def cause = @cause.nil? ? super : @cause
14
+
15
+ # @api private
16
+ #
17
+ # @param on [Class<StandardError>]
18
+ # @param method [Symbol]
19
+ # @param target [Object]
20
+ # @param value [Object]
21
+ # @param cause [StandardError, nil]
22
+ def initialize(on:, method:, target:, value:, cause: nil)
23
+ cls = on.name.split("::").last
24
+
25
+ message = [
26
+ "Failed to parse #{cls}.#{method} from #{value.class} to #{target.inspect}.",
27
+ "To get the unparsed API response, use #{cls}[#{method.inspect}].",
28
+ cause && "Cause: #{cause.message}"
29
+ ].filter(&:itself).join(" ")
30
+
31
+ @cause = cause
32
+ super(message)
33
+ end
34
+ end
35
+
36
+ class APIError < CheckoutIntents::Errors::Error
37
+ # @return [URI::Generic]
38
+ attr_accessor :url
39
+
40
+ # @return [Integer, nil]
41
+ attr_accessor :status
42
+
43
+ # @return [Hash{String=>String}, nil]
44
+ attr_accessor :headers
45
+
46
+ # @return [Object, nil]
47
+ attr_accessor :body
48
+
49
+ # @api private
50
+ #
51
+ # @param url [URI::Generic]
52
+ # @param status [Integer, nil]
53
+ # @param headers [Hash{String=>String}, nil]
54
+ # @param body [Object, nil]
55
+ # @param request [nil]
56
+ # @param response [nil]
57
+ # @param message [String, nil]
58
+ def initialize(url:, status: nil, headers: nil, body: nil, request: nil, response: nil, message: nil)
59
+ @url = url
60
+ @status = status
61
+ @headers = headers
62
+ @body = body
63
+ @request = request
64
+ @response = response
65
+ super(message)
66
+ end
67
+ end
68
+
69
+ class APIConnectionError < CheckoutIntents::Errors::APIError
70
+ # @!attribute status
71
+ #
72
+ # @return [nil]
73
+
74
+ # @!attribute body
75
+ #
76
+ # @return [nil]
77
+
78
+ # @api private
79
+ #
80
+ # @param url [URI::Generic]
81
+ # @param status [nil]
82
+ # @param headers [Hash{String=>String}, nil]
83
+ # @param body [nil]
84
+ # @param request [nil]
85
+ # @param response [nil]
86
+ # @param message [String, nil]
87
+ def initialize(
88
+ url:,
89
+ status: nil,
90
+ headers: nil,
91
+ body: nil,
92
+ request: nil,
93
+ response: nil,
94
+ message: "Connection error."
95
+ )
96
+ super
97
+ end
98
+ end
99
+
100
+ class APITimeoutError < CheckoutIntents::Errors::APIConnectionError
101
+ # @api private
102
+ #
103
+ # @param url [URI::Generic]
104
+ # @param status [nil]
105
+ # @param headers [Hash{String=>String}, nil]
106
+ # @param body [nil]
107
+ # @param request [nil]
108
+ # @param response [nil]
109
+ # @param message [String, nil]
110
+ def initialize(
111
+ url:,
112
+ status: nil,
113
+ headers: nil,
114
+ body: nil,
115
+ request: nil,
116
+ response: nil,
117
+ message: "Request timed out."
118
+ )
119
+ super
120
+ end
121
+ end
122
+
123
+ class APIStatusError < CheckoutIntents::Errors::APIError
124
+ # @api private
125
+ #
126
+ # @param url [URI::Generic]
127
+ # @param status [Integer]
128
+ # @param headers [Hash{String=>String}, nil]
129
+ # @param body [Object, nil]
130
+ # @param request [nil]
131
+ # @param response [nil]
132
+ # @param message [String, nil]
133
+ #
134
+ # @return [self]
135
+ def self.for(url:, status:, headers:, body:, request:, response:, message: nil)
136
+ kwargs =
137
+ {
138
+ url: url,
139
+ status: status,
140
+ headers: headers,
141
+ body: body,
142
+ request: request,
143
+ response: response,
144
+ message: message
145
+ }
146
+
147
+ case status
148
+ in 400
149
+ CheckoutIntents::Errors::BadRequestError.new(**kwargs)
150
+ in 401
151
+ CheckoutIntents::Errors::AuthenticationError.new(**kwargs)
152
+ in 403
153
+ CheckoutIntents::Errors::PermissionDeniedError.new(**kwargs)
154
+ in 404
155
+ CheckoutIntents::Errors::NotFoundError.new(**kwargs)
156
+ in 409
157
+ CheckoutIntents::Errors::ConflictError.new(**kwargs)
158
+ in 422
159
+ CheckoutIntents::Errors::UnprocessableEntityError.new(**kwargs)
160
+ in 429
161
+ CheckoutIntents::Errors::RateLimitError.new(**kwargs)
162
+ in (500..)
163
+ CheckoutIntents::Errors::InternalServerError.new(**kwargs)
164
+ else
165
+ CheckoutIntents::Errors::APIStatusError.new(**kwargs)
166
+ end
167
+ end
168
+
169
+ # @!parse
170
+ # # @return [Integer]
171
+ # attr_accessor :status
172
+
173
+ # @api private
174
+ #
175
+ # @param url [URI::Generic]
176
+ # @param status [Integer]
177
+ # @param headers [Hash{String=>String}, nil]
178
+ # @param body [Object, nil]
179
+ # @param request [nil]
180
+ # @param response [nil]
181
+ # @param message [String, nil]
182
+ def initialize(url:, status:, headers:, body:, request:, response:, message: nil)
183
+ message ||= {url: url.to_s, status: status, body: body}
184
+ super(
185
+ url: url,
186
+ status: status,
187
+ headers: headers,
188
+ body: body,
189
+ request: request,
190
+ response: response,
191
+ message: message&.to_s
192
+ )
193
+ end
194
+ end
195
+
196
+ class BadRequestError < CheckoutIntents::Errors::APIStatusError
197
+ HTTP_STATUS = 400
198
+ end
199
+
200
+ class AuthenticationError < CheckoutIntents::Errors::APIStatusError
201
+ HTTP_STATUS = 401
202
+ end
203
+
204
+ class PermissionDeniedError < CheckoutIntents::Errors::APIStatusError
205
+ HTTP_STATUS = 403
206
+ end
207
+
208
+ class NotFoundError < CheckoutIntents::Errors::APIStatusError
209
+ HTTP_STATUS = 404
210
+ end
211
+
212
+ class ConflictError < CheckoutIntents::Errors::APIStatusError
213
+ HTTP_STATUS = 409
214
+ end
215
+
216
+ class UnprocessableEntityError < CheckoutIntents::Errors::APIStatusError
217
+ HTTP_STATUS = 422
218
+ end
219
+
220
+ class RateLimitError < CheckoutIntents::Errors::APIStatusError
221
+ HTTP_STATUS = 429
222
+ end
223
+
224
+ class InternalServerError < CheckoutIntents::Errors::APIStatusError
225
+ HTTP_STATUS = (500..)
226
+ end
227
+ end
228
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheckoutIntents
4
+ class FilePart
5
+ # @return [Pathname, StringIO, IO, String]
6
+ attr_reader :content
7
+
8
+ # @return [String, nil]
9
+ attr_reader :content_type
10
+
11
+ # @return [String, nil]
12
+ attr_reader :filename
13
+
14
+ # @api private
15
+ #
16
+ # @return [String]
17
+ private def read
18
+ case content
19
+ in Pathname
20
+ content.read(binmode: true)
21
+ in StringIO
22
+ content.string
23
+ in IO
24
+ content.read
25
+ in String
26
+ content
27
+ end
28
+ end
29
+
30
+ # @param a [Object]
31
+ #
32
+ # @return [String]
33
+ def to_json(*a) = read.to_json(*a)
34
+
35
+ # @param a [Object]
36
+ #
37
+ # @return [String]
38
+ def to_yaml(*a) = read.to_yaml(*a)
39
+
40
+ # @param content [Pathname, StringIO, IO, String]
41
+ # @param filename [Pathname, String, nil]
42
+ # @param content_type [String, nil]
43
+ def initialize(content, filename: nil, content_type: nil)
44
+ @content_type = content_type
45
+ @filename =
46
+ case [filename, (@content = content)]
47
+ in [String | Pathname, _]
48
+ ::File.basename(filename)
49
+ in [nil, Pathname]
50
+ content.basename.to_path
51
+ in [nil, IO]
52
+ content.to_path
53
+ else
54
+ filename
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CheckoutIntents
4
+ module Internal
5
+ # @generic Elem
6
+ #
7
+ # @example
8
+ # if cursor_pagination.has_next?
9
+ # cursor_pagination = cursor_pagination.next_page
10
+ # end
11
+ #
12
+ # @example
13
+ # cursor_pagination.auto_paging_each do |checkout_intent|
14
+ # puts(checkout_intent)
15
+ # end
16
+ class CursorPagination
17
+ include CheckoutIntents::Internal::Type::BasePage
18
+
19
+ # @return [Array<generic<Elem>>, nil]
20
+ attr_accessor :data
21
+
22
+ # @return [PageInfo]
23
+ attr_accessor :page_info
24
+
25
+ # @return [Boolean]
26
+ def next_page?
27
+ !data.to_a.empty? && (!page_info&.start_cursor.to_s.empty? || !page_info&.end_cursor.to_s.empty?)
28
+ end
29
+
30
+ # @raise [CheckoutIntents::HTTP::Error]
31
+ # @return [self]
32
+ def next_page
33
+ unless next_page?
34
+ message = "No more pages available. Please check #next_page? before calling ##{__method__}"
35
+ raise RuntimeError.new(message)
36
+ end
37
+
38
+ req = CheckoutIntents::Internal::Util.deep_merge(
39
+ @req,
40
+ {query: page_info&.start_cursor.nil? ? {after: page_info&.end_cursor} : {before: page_info&.start_cursor}}
41
+ )
42
+ @client.request(req)
43
+ end
44
+
45
+ # @param blk [Proc]
46
+ #
47
+ # @yieldparam [generic<Elem>]
48
+ def auto_paging_each(&blk)
49
+ unless block_given?
50
+ raise ArgumentError.new("A block must be given to ##{__method__}")
51
+ end
52
+
53
+ page = self
54
+ loop do
55
+ page.data&.each(&blk)
56
+
57
+ break unless page.next_page?
58
+ page = page.next_page
59
+ end
60
+ end
61
+
62
+ # @api private
63
+ #
64
+ # @param client [CheckoutIntents::Internal::Transport::BaseClient]
65
+ # @param req [Hash{Symbol=>Object}]
66
+ # @param headers [Hash{String=>String}]
67
+ # @param page_data [Hash{Symbol=>Object}]
68
+ def initialize(client:, req:, headers:, page_data:)
69
+ super
70
+
71
+ case page_data
72
+ in {data: Array => data}
73
+ @data = data.map { CheckoutIntents::Internal::Type::Converter.coerce(@model, _1) }
74
+ else
75
+ end
76
+ case page_data
77
+ in {pageInfo: Hash | nil => page_info}
78
+ @page_info =
79
+ CheckoutIntents::Internal::Type::Converter.coerce(
80
+ CheckoutIntents::Internal::CursorPagination::PageInfo,
81
+ page_info
82
+ )
83
+ else
84
+ end
85
+ end
86
+
87
+ # @api private
88
+ #
89
+ # @return [String]
90
+ def inspect
91
+ model = CheckoutIntents::Internal::Type::Converter.inspect(@model, depth: 1)
92
+
93
+ "#<#{self.class}[#{model}]:0x#{object_id.to_s(16)}>"
94
+ end
95
+
96
+ class PageInfo < CheckoutIntents::Internal::Type::BaseModel
97
+ # @!attribute end_cursor
98
+ #
99
+ # @return [String, nil]
100
+ optional :end_cursor, String, api_name: :endCursor, nil?: true
101
+
102
+ # @!attribute has_next_page
103
+ #
104
+ # @return [Boolean, nil]
105
+ optional :has_next_page, CheckoutIntents::Internal::Type::Boolean, api_name: :hasNextPage
106
+
107
+ # @!attribute has_previous_page
108
+ #
109
+ # @return [Boolean, nil]
110
+ optional :has_previous_page, CheckoutIntents::Internal::Type::Boolean, api_name: :hasPreviousPage
111
+
112
+ # @!attribute start_cursor
113
+ #
114
+ # @return [String, nil]
115
+ optional :start_cursor, String, api_name: :startCursor, nil?: true
116
+
117
+ # @!method initialize(end_cursor: nil, has_next_page: nil, has_previous_page: nil, start_cursor: nil)
118
+ # @param end_cursor [String, nil]
119
+ # @param has_next_page [Boolean]
120
+ # @param has_previous_page [Boolean]
121
+ # @param start_cursor [String, nil]
122
+ end
123
+ end
124
+ end
125
+ end