perata-sdk 0.1.1
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.
- checksums.yaml +7 -0
- data/README.md +51 -0
- data/lib/perata/client.rb +91 -0
- data/lib/perata/error.rb +43 -0
- data/lib/perata/model.rb +45 -0
- data/lib/perata/models.rb +527 -0
- data/lib/perata/page.rb +34 -0
- data/lib/perata/params.rb +200 -0
- data/lib/perata/services.rb +295 -0
- data/lib/perata/version.rb +15 -0
- data/lib/perata/webhooks.rb +87 -0
- data/lib/perata.rb +37 -0
- metadata +52 -0
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Code generated by sdk/generator. DO NOT EDIT.
|
|
4
|
+
|
|
5
|
+
module Perata
|
|
6
|
+
# Card is a resource of Peráta's API.
|
|
7
|
+
class Card < Model
|
|
8
|
+
MEMBERS = %i[brand exp_month exp_year last4].freeze
|
|
9
|
+
|
|
10
|
+
# @return [String]
|
|
11
|
+
attr_reader :brand
|
|
12
|
+
|
|
13
|
+
# @return [Integer, nil]
|
|
14
|
+
attr_reader :exp_month
|
|
15
|
+
|
|
16
|
+
# @return [Integer, nil]
|
|
17
|
+
attr_reader :exp_year
|
|
18
|
+
|
|
19
|
+
# @return [String, nil]
|
|
20
|
+
attr_reader :last4
|
|
21
|
+
|
|
22
|
+
def initialize(brand:, exp_month:, exp_year:, last4:)
|
|
23
|
+
@brand = brand
|
|
24
|
+
@exp_month = exp_month
|
|
25
|
+
@exp_year = exp_year
|
|
26
|
+
@last4 = last4
|
|
27
|
+
freeze
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Builds a Card from the API's JSON.
|
|
31
|
+
def self.from_hash(data)
|
|
32
|
+
new(
|
|
33
|
+
brand: data.fetch("brand"),
|
|
34
|
+
exp_month: data["exp_month"],
|
|
35
|
+
exp_year: data["exp_year"],
|
|
36
|
+
last4: data["last4"]
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Checkout is a resource of Peráta's API.
|
|
42
|
+
class Checkout < Model
|
|
43
|
+
MEMBERS = %i[id price_id product_id url].freeze
|
|
44
|
+
|
|
45
|
+
# @return [String]
|
|
46
|
+
attr_reader :id
|
|
47
|
+
|
|
48
|
+
# @return [String]
|
|
49
|
+
attr_reader :price_id
|
|
50
|
+
|
|
51
|
+
# @return [String]
|
|
52
|
+
attr_reader :product_id
|
|
53
|
+
|
|
54
|
+
# Endereço para onde o comprador é enviado.
|
|
55
|
+
# @return [String]
|
|
56
|
+
attr_reader :url
|
|
57
|
+
|
|
58
|
+
def initialize(id:, price_id:, product_id:, url:)
|
|
59
|
+
@id = id
|
|
60
|
+
@price_id = price_id
|
|
61
|
+
@product_id = product_id
|
|
62
|
+
@url = url
|
|
63
|
+
freeze
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# Builds a Checkout from the API's JSON.
|
|
67
|
+
def self.from_hash(data)
|
|
68
|
+
new(
|
|
69
|
+
id: data.fetch("id"),
|
|
70
|
+
price_id: data.fetch("price_id"),
|
|
71
|
+
product_id: data.fetch("product_id"),
|
|
72
|
+
url: data.fetch("url")
|
|
73
|
+
)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# Customer is a resource of Peráta's API.
|
|
78
|
+
class Customer < Model
|
|
79
|
+
MEMBERS = %i[created_at email external_id id name status].freeze
|
|
80
|
+
|
|
81
|
+
# @return [String]
|
|
82
|
+
attr_reader :created_at
|
|
83
|
+
|
|
84
|
+
# @return [String]
|
|
85
|
+
attr_reader :email
|
|
86
|
+
|
|
87
|
+
# @return [String]
|
|
88
|
+
attr_reader :external_id
|
|
89
|
+
|
|
90
|
+
# @return [String]
|
|
91
|
+
attr_reader :id
|
|
92
|
+
|
|
93
|
+
# @return [String]
|
|
94
|
+
attr_reader :name
|
|
95
|
+
|
|
96
|
+
# @return [String ("active", "archived")]
|
|
97
|
+
attr_reader :status
|
|
98
|
+
|
|
99
|
+
def initialize(created_at:, email:, external_id:, id:, name:, status:)
|
|
100
|
+
@created_at = created_at
|
|
101
|
+
@email = email
|
|
102
|
+
@external_id = external_id
|
|
103
|
+
@id = id
|
|
104
|
+
@name = name
|
|
105
|
+
@status = status
|
|
106
|
+
freeze
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Builds a Customer from the API's JSON.
|
|
110
|
+
def self.from_hash(data)
|
|
111
|
+
new(
|
|
112
|
+
created_at: data.fetch("created_at"),
|
|
113
|
+
email: data.fetch("email"),
|
|
114
|
+
external_id: data.fetch("external_id"),
|
|
115
|
+
id: data.fetch("id"),
|
|
116
|
+
name: data.fetch("name"),
|
|
117
|
+
status: data.fetch("status")
|
|
118
|
+
)
|
|
119
|
+
end
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
# Order is a resource of Peráta's API.
|
|
123
|
+
class Order < Model
|
|
124
|
+
MEMBERS = %i[amount_cents currency customer_id id paid_at price_id product_id status].freeze
|
|
125
|
+
|
|
126
|
+
# @return [Integer]
|
|
127
|
+
attr_reader :amount_cents
|
|
128
|
+
|
|
129
|
+
# @return [String ("BRL")]
|
|
130
|
+
attr_reader :currency
|
|
131
|
+
|
|
132
|
+
# @return [String]
|
|
133
|
+
attr_reader :customer_id
|
|
134
|
+
|
|
135
|
+
# @return [String]
|
|
136
|
+
attr_reader :id
|
|
137
|
+
|
|
138
|
+
# @return [String, nil]
|
|
139
|
+
attr_reader :paid_at
|
|
140
|
+
|
|
141
|
+
# @return [String]
|
|
142
|
+
attr_reader :price_id
|
|
143
|
+
|
|
144
|
+
# @return [String]
|
|
145
|
+
attr_reader :product_id
|
|
146
|
+
|
|
147
|
+
# @return [String ("open", "confirmed", "succeeded", "failed", "expired")]
|
|
148
|
+
attr_reader :status
|
|
149
|
+
|
|
150
|
+
def initialize(amount_cents:, currency:, customer_id:, id:, paid_at:, price_id:, product_id:, status:)
|
|
151
|
+
@amount_cents = amount_cents
|
|
152
|
+
@currency = currency
|
|
153
|
+
@customer_id = customer_id
|
|
154
|
+
@id = id
|
|
155
|
+
@paid_at = paid_at
|
|
156
|
+
@price_id = price_id
|
|
157
|
+
@product_id = product_id
|
|
158
|
+
@status = status
|
|
159
|
+
freeze
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Builds a Order from the API's JSON.
|
|
163
|
+
def self.from_hash(data)
|
|
164
|
+
new(
|
|
165
|
+
amount_cents: data.fetch("amount_cents"),
|
|
166
|
+
currency: data.fetch("currency"),
|
|
167
|
+
customer_id: data.fetch("customer_id"),
|
|
168
|
+
id: data.fetch("id"),
|
|
169
|
+
paid_at: data["paid_at"],
|
|
170
|
+
price_id: data.fetch("price_id"),
|
|
171
|
+
product_id: data.fetch("product_id"),
|
|
172
|
+
status: data.fetch("status")
|
|
173
|
+
)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
# Pagination is a resource of Peráta's API.
|
|
178
|
+
class Pagination < Model
|
|
179
|
+
MEMBERS = %i[max_page total_count].freeze
|
|
180
|
+
|
|
181
|
+
# @return [Integer]
|
|
182
|
+
attr_reader :max_page
|
|
183
|
+
|
|
184
|
+
# @return [Integer]
|
|
185
|
+
attr_reader :total_count
|
|
186
|
+
|
|
187
|
+
def initialize(max_page:, total_count:)
|
|
188
|
+
@max_page = max_page
|
|
189
|
+
@total_count = total_count
|
|
190
|
+
freeze
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Builds a Pagination from the API's JSON.
|
|
194
|
+
def self.from_hash(data)
|
|
195
|
+
new(
|
|
196
|
+
max_page: data.fetch("max_page"),
|
|
197
|
+
total_count: data.fetch("total_count")
|
|
198
|
+
)
|
|
199
|
+
end
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
# Payment is a resource of Peráta's API.
|
|
203
|
+
class Payment < Model
|
|
204
|
+
MEMBERS = %i[amount_cents captured_cents checkout_id created_at currency customer_id id method refunded_cents state].freeze
|
|
205
|
+
|
|
206
|
+
# @return [Integer]
|
|
207
|
+
attr_reader :amount_cents
|
|
208
|
+
|
|
209
|
+
# @return [Integer]
|
|
210
|
+
attr_reader :captured_cents
|
|
211
|
+
|
|
212
|
+
# @return [String]
|
|
213
|
+
attr_reader :checkout_id
|
|
214
|
+
|
|
215
|
+
# @return [String]
|
|
216
|
+
attr_reader :created_at
|
|
217
|
+
|
|
218
|
+
# @return [String ("BRL")]
|
|
219
|
+
attr_reader :currency
|
|
220
|
+
|
|
221
|
+
# @return [String]
|
|
222
|
+
attr_reader :customer_id
|
|
223
|
+
|
|
224
|
+
# @return [String]
|
|
225
|
+
attr_reader :id
|
|
226
|
+
|
|
227
|
+
# @return [String ("card", "pix", "boleto")]
|
|
228
|
+
attr_reader :method
|
|
229
|
+
|
|
230
|
+
# @return [Integer]
|
|
231
|
+
attr_reader :refunded_cents
|
|
232
|
+
|
|
233
|
+
# @return [String ("unknown", "pending", "authorized", "captured", "refused", "canceled", "refunded", "partially_refunded")]
|
|
234
|
+
attr_reader :state
|
|
235
|
+
|
|
236
|
+
def initialize(amount_cents:, captured_cents:, checkout_id:, created_at:, currency:, customer_id:, id:, method:, refunded_cents:, state:)
|
|
237
|
+
@amount_cents = amount_cents
|
|
238
|
+
@captured_cents = captured_cents
|
|
239
|
+
@checkout_id = checkout_id
|
|
240
|
+
@created_at = created_at
|
|
241
|
+
@currency = currency
|
|
242
|
+
@customer_id = customer_id
|
|
243
|
+
@id = id
|
|
244
|
+
@method = method
|
|
245
|
+
@refunded_cents = refunded_cents
|
|
246
|
+
@state = state
|
|
247
|
+
freeze
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Builds a Payment from the API's JSON.
|
|
251
|
+
def self.from_hash(data)
|
|
252
|
+
new(
|
|
253
|
+
amount_cents: data.fetch("amount_cents"),
|
|
254
|
+
captured_cents: data.fetch("captured_cents"),
|
|
255
|
+
checkout_id: data.fetch("checkout_id"),
|
|
256
|
+
created_at: data.fetch("created_at"),
|
|
257
|
+
currency: data.fetch("currency"),
|
|
258
|
+
customer_id: data.fetch("customer_id"),
|
|
259
|
+
id: data.fetch("id"),
|
|
260
|
+
method: data.fetch("method"),
|
|
261
|
+
refunded_cents: data.fetch("refunded_cents"),
|
|
262
|
+
state: data.fetch("state")
|
|
263
|
+
)
|
|
264
|
+
end
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
# PaymentMethod is a resource of Peráta's API.
|
|
268
|
+
class PaymentMethod < Model
|
|
269
|
+
MEMBERS = %i[card created_at customer_id id type].freeze
|
|
270
|
+
|
|
271
|
+
# @return [Card]
|
|
272
|
+
attr_reader :card
|
|
273
|
+
|
|
274
|
+
# @return [String]
|
|
275
|
+
attr_reader :created_at
|
|
276
|
+
|
|
277
|
+
# @return [String]
|
|
278
|
+
attr_reader :customer_id
|
|
279
|
+
|
|
280
|
+
# @return [String]
|
|
281
|
+
attr_reader :id
|
|
282
|
+
|
|
283
|
+
# @return [String ("card")]
|
|
284
|
+
attr_reader :type
|
|
285
|
+
|
|
286
|
+
def initialize(card:, created_at:, customer_id:, id:, type:)
|
|
287
|
+
@card = card
|
|
288
|
+
@created_at = created_at
|
|
289
|
+
@customer_id = customer_id
|
|
290
|
+
@id = id
|
|
291
|
+
@type = type
|
|
292
|
+
freeze
|
|
293
|
+
end
|
|
294
|
+
|
|
295
|
+
# Builds a PaymentMethod from the API's JSON.
|
|
296
|
+
def self.from_hash(data)
|
|
297
|
+
new(
|
|
298
|
+
card: Card.from_hash(data.fetch("card")),
|
|
299
|
+
created_at: data.fetch("created_at"),
|
|
300
|
+
customer_id: data.fetch("customer_id"),
|
|
301
|
+
id: data.fetch("id"),
|
|
302
|
+
type: data.fetch("type")
|
|
303
|
+
)
|
|
304
|
+
end
|
|
305
|
+
end
|
|
306
|
+
|
|
307
|
+
# PaymentMethodDetached is a resource of Peráta's API.
|
|
308
|
+
class PaymentMethodDetached < Model
|
|
309
|
+
MEMBERS = %i[detached id].freeze
|
|
310
|
+
|
|
311
|
+
# @return [Boolean]
|
|
312
|
+
attr_reader :detached
|
|
313
|
+
|
|
314
|
+
# @return [String]
|
|
315
|
+
attr_reader :id
|
|
316
|
+
|
|
317
|
+
def initialize(detached:, id:)
|
|
318
|
+
@detached = detached
|
|
319
|
+
@id = id
|
|
320
|
+
freeze
|
|
321
|
+
end
|
|
322
|
+
|
|
323
|
+
# Builds a PaymentMethodDetached from the API's JSON.
|
|
324
|
+
def self.from_hash(data)
|
|
325
|
+
new(
|
|
326
|
+
detached: data.fetch("detached"),
|
|
327
|
+
id: data.fetch("id")
|
|
328
|
+
)
|
|
329
|
+
end
|
|
330
|
+
end
|
|
331
|
+
|
|
332
|
+
# Price is a resource of Peráta's API.
|
|
333
|
+
class Price < Model
|
|
334
|
+
MEMBERS = %i[amount_cents currency id interval product_id status type].freeze
|
|
335
|
+
|
|
336
|
+
# Valor em centavos.
|
|
337
|
+
# @return [Integer]
|
|
338
|
+
attr_reader :amount_cents
|
|
339
|
+
|
|
340
|
+
# @return [String ("BRL")]
|
|
341
|
+
attr_reader :currency
|
|
342
|
+
|
|
343
|
+
# @return [String]
|
|
344
|
+
attr_reader :id
|
|
345
|
+
|
|
346
|
+
# @return [String ("none", "month", "year")]
|
|
347
|
+
attr_reader :interval
|
|
348
|
+
|
|
349
|
+
# @return [String]
|
|
350
|
+
attr_reader :product_id
|
|
351
|
+
|
|
352
|
+
# @return [String ("active", "archived")]
|
|
353
|
+
attr_reader :status
|
|
354
|
+
|
|
355
|
+
# @return [String ("one_time", "recurring")]
|
|
356
|
+
attr_reader :type
|
|
357
|
+
|
|
358
|
+
def initialize(amount_cents:, currency:, id:, interval:, product_id:, status:, type:)
|
|
359
|
+
@amount_cents = amount_cents
|
|
360
|
+
@currency = currency
|
|
361
|
+
@id = id
|
|
362
|
+
@interval = interval
|
|
363
|
+
@product_id = product_id
|
|
364
|
+
@status = status
|
|
365
|
+
@type = type
|
|
366
|
+
freeze
|
|
367
|
+
end
|
|
368
|
+
|
|
369
|
+
# Builds a Price from the API's JSON.
|
|
370
|
+
def self.from_hash(data)
|
|
371
|
+
new(
|
|
372
|
+
amount_cents: data.fetch("amount_cents"),
|
|
373
|
+
currency: data.fetch("currency"),
|
|
374
|
+
id: data.fetch("id"),
|
|
375
|
+
interval: data.fetch("interval"),
|
|
376
|
+
product_id: data.fetch("product_id"),
|
|
377
|
+
status: data.fetch("status"),
|
|
378
|
+
type: data.fetch("type")
|
|
379
|
+
)
|
|
380
|
+
end
|
|
381
|
+
end
|
|
382
|
+
|
|
383
|
+
# Product is a resource of Peráta's API.
|
|
384
|
+
class Product < Model
|
|
385
|
+
MEMBERS = %i[created_at description id name status].freeze
|
|
386
|
+
|
|
387
|
+
# @return [String]
|
|
388
|
+
attr_reader :created_at
|
|
389
|
+
|
|
390
|
+
# @return [String]
|
|
391
|
+
attr_reader :description
|
|
392
|
+
|
|
393
|
+
# @return [String]
|
|
394
|
+
attr_reader :id
|
|
395
|
+
|
|
396
|
+
# @return [String]
|
|
397
|
+
attr_reader :name
|
|
398
|
+
|
|
399
|
+
# @return [String ("active", "archived")]
|
|
400
|
+
attr_reader :status
|
|
401
|
+
|
|
402
|
+
def initialize(created_at:, description:, id:, name:, status:)
|
|
403
|
+
@created_at = created_at
|
|
404
|
+
@description = description
|
|
405
|
+
@id = id
|
|
406
|
+
@name = name
|
|
407
|
+
@status = status
|
|
408
|
+
freeze
|
|
409
|
+
end
|
|
410
|
+
|
|
411
|
+
# Builds a Product from the API's JSON.
|
|
412
|
+
def self.from_hash(data)
|
|
413
|
+
new(
|
|
414
|
+
created_at: data.fetch("created_at"),
|
|
415
|
+
description: data.fetch("description"),
|
|
416
|
+
id: data.fetch("id"),
|
|
417
|
+
name: data.fetch("name"),
|
|
418
|
+
status: data.fetch("status")
|
|
419
|
+
)
|
|
420
|
+
end
|
|
421
|
+
end
|
|
422
|
+
|
|
423
|
+
# Refund is a resource of Peráta's API.
|
|
424
|
+
class Refund < Model
|
|
425
|
+
MEMBERS = %i[amount_cents id payment_id status].freeze
|
|
426
|
+
|
|
427
|
+
# @return [Integer]
|
|
428
|
+
attr_reader :amount_cents
|
|
429
|
+
|
|
430
|
+
# @return [String]
|
|
431
|
+
attr_reader :id
|
|
432
|
+
|
|
433
|
+
# @return [String]
|
|
434
|
+
attr_reader :payment_id
|
|
435
|
+
|
|
436
|
+
# @return [String ("requested", "pending", "unknown", "succeeded", "failed", "canceled")]
|
|
437
|
+
attr_reader :status
|
|
438
|
+
|
|
439
|
+
def initialize(amount_cents:, id:, payment_id:, status:)
|
|
440
|
+
@amount_cents = amount_cents
|
|
441
|
+
@id = id
|
|
442
|
+
@payment_id = payment_id
|
|
443
|
+
@status = status
|
|
444
|
+
freeze
|
|
445
|
+
end
|
|
446
|
+
|
|
447
|
+
# Builds a Refund from the API's JSON.
|
|
448
|
+
def self.from_hash(data)
|
|
449
|
+
new(
|
|
450
|
+
amount_cents: data.fetch("amount_cents"),
|
|
451
|
+
id: data.fetch("id"),
|
|
452
|
+
payment_id: data.fetch("payment_id"),
|
|
453
|
+
status: data.fetch("status")
|
|
454
|
+
)
|
|
455
|
+
end
|
|
456
|
+
end
|
|
457
|
+
|
|
458
|
+
# Subscription is a resource of Peráta's API.
|
|
459
|
+
class Subscription < Model
|
|
460
|
+
MEMBERS = %i[amount_cents cancel_at_period_end currency current_period_end current_period_start customer_id id interval price_id product_id status].freeze
|
|
461
|
+
|
|
462
|
+
# @return [Integer]
|
|
463
|
+
attr_reader :amount_cents
|
|
464
|
+
|
|
465
|
+
# @return [Boolean]
|
|
466
|
+
attr_reader :cancel_at_period_end
|
|
467
|
+
|
|
468
|
+
# @return [String ("BRL")]
|
|
469
|
+
attr_reader :currency
|
|
470
|
+
|
|
471
|
+
# @return [String]
|
|
472
|
+
attr_reader :current_period_end
|
|
473
|
+
|
|
474
|
+
# @return [String]
|
|
475
|
+
attr_reader :current_period_start
|
|
476
|
+
|
|
477
|
+
# @return [String]
|
|
478
|
+
attr_reader :customer_id
|
|
479
|
+
|
|
480
|
+
# @return [String]
|
|
481
|
+
attr_reader :id
|
|
482
|
+
|
|
483
|
+
# @return [String]
|
|
484
|
+
attr_reader :interval
|
|
485
|
+
|
|
486
|
+
# @return [String]
|
|
487
|
+
attr_reader :price_id
|
|
488
|
+
|
|
489
|
+
# @return [String]
|
|
490
|
+
attr_reader :product_id
|
|
491
|
+
|
|
492
|
+
# @return [String ("trialing", "active", "past_due", "paused", "canceled")]
|
|
493
|
+
attr_reader :status
|
|
494
|
+
|
|
495
|
+
def initialize(amount_cents:, cancel_at_period_end:, currency:, current_period_end:, current_period_start:, customer_id:, id:, interval:, price_id:, product_id:, status:)
|
|
496
|
+
@amount_cents = amount_cents
|
|
497
|
+
@cancel_at_period_end = cancel_at_period_end
|
|
498
|
+
@currency = currency
|
|
499
|
+
@current_period_end = current_period_end
|
|
500
|
+
@current_period_start = current_period_start
|
|
501
|
+
@customer_id = customer_id
|
|
502
|
+
@id = id
|
|
503
|
+
@interval = interval
|
|
504
|
+
@price_id = price_id
|
|
505
|
+
@product_id = product_id
|
|
506
|
+
@status = status
|
|
507
|
+
freeze
|
|
508
|
+
end
|
|
509
|
+
|
|
510
|
+
# Builds a Subscription from the API's JSON.
|
|
511
|
+
def self.from_hash(data)
|
|
512
|
+
new(
|
|
513
|
+
amount_cents: data.fetch("amount_cents"),
|
|
514
|
+
cancel_at_period_end: data.fetch("cancel_at_period_end"),
|
|
515
|
+
currency: data.fetch("currency"),
|
|
516
|
+
current_period_end: data.fetch("current_period_end"),
|
|
517
|
+
current_period_start: data.fetch("current_period_start"),
|
|
518
|
+
customer_id: data.fetch("customer_id"),
|
|
519
|
+
id: data.fetch("id"),
|
|
520
|
+
interval: data.fetch("interval"),
|
|
521
|
+
price_id: data.fetch("price_id"),
|
|
522
|
+
product_id: data.fetch("product_id"),
|
|
523
|
+
status: data.fetch("status")
|
|
524
|
+
)
|
|
525
|
+
end
|
|
526
|
+
end
|
|
527
|
+
end
|
data/lib/perata/page.rb
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Perata
|
|
4
|
+
# One page of a list, and where the list ends.
|
|
5
|
+
class Page
|
|
6
|
+
attr_reader :items, :pagination
|
|
7
|
+
|
|
8
|
+
def initialize(items:, pagination:)
|
|
9
|
+
@items = items.freeze
|
|
10
|
+
@pagination = pagination
|
|
11
|
+
freeze
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
# Builds a page from the API's hash, each item through the block.
|
|
15
|
+
def self.from_hash(data, &item)
|
|
16
|
+
new(items: (data["items"] || []).map(&item), pagination: Pagination.from_hash(data.fetch("pagination")))
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Walks pages from +first+ until the last one, as Polar's SDKs do, yielding
|
|
20
|
+
# every item; +fetch+ answers the page it is given.
|
|
21
|
+
def self.iterate(first, &fetch)
|
|
22
|
+
Enumerator.new do |yielder|
|
|
23
|
+
page = [first || 1, 1].max
|
|
24
|
+
loop do
|
|
25
|
+
result = fetch.call(page)
|
|
26
|
+
result.items.each { |item| yielder << item }
|
|
27
|
+
break if page >= result.pagination.max_page
|
|
28
|
+
|
|
29
|
+
page += 1
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
end
|