Class: Blueticks::Types::PaginatedPage

Inherits:
Object
  • Object
show all
Defined in:
lib/blueticks/types/paginated.rb

Overview

Offset-paginated list envelope. Every v1 list endpoint returns this shape inside the success/data wrapper: { data: [...], limit, skip, total }.

Iterate data for the current page; advance by passing skip: skip + limit (or any offset) back on the next list call. total is the full match count across all pages. has_more is populated only by endpoints that echo a hasMore flag (e.g. messages.list); nil elsewhere.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, limit:, skip:, total:, has_more: nil) ⇒ PaginatedPage

Returns a new instance of PaginatedPage.



18
19
20
21
22
23
24
# File 'lib/blueticks/types/paginated.rb', line 18

def initialize(data:, limit:, skip:, total:, has_more: nil)
  @data = data
  @limit = limit
  @skip = skip
  @total = total
  @has_more = has_more
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



16
17
18
# File 'lib/blueticks/types/paginated.rb', line 16

def data
  @data
end

#has_moreObject (readonly)

Returns the value of attribute has_more.



16
17
18
# File 'lib/blueticks/types/paginated.rb', line 16

def has_more
  @has_more
end

#limitObject (readonly)

Returns the value of attribute limit.



16
17
18
# File 'lib/blueticks/types/paginated.rb', line 16

def limit
  @limit
end

#skipObject (readonly)

Returns the value of attribute skip.



16
17
18
# File 'lib/blueticks/types/paginated.rb', line 16

def skip
  @skip
end

#totalObject (readonly)

Returns the value of attribute total.



16
17
18
# File 'lib/blueticks/types/paginated.rb', line 16

def total
  @total
end

Class Method Details

.from_hash(raw, item_type:) ⇒ Object

Build a PaginatedPage from a raw list envelope, decoding each item via item_type.from_hash. Reads data/limit/skip/total (and optional hasMore) off the envelope; ignores the success flag.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/blueticks/types/paginated.rb', line 29

def self.from_hash(raw, item_type:)
  unless raw.is_a?(Hash)
    raise Errors::ValidationError.new(message: "Expected Hash for PaginatedPage, got #{raw.class}")
  end
  unless raw["data"].is_a?(Array)
    raise Errors::ValidationError.new(message: "Field 'data' must be array in PaginatedPage")
  end

  limit = require_integer(raw, "limit")
  skip = require_integer(raw, "skip")
  total = require_integer(raw, "total")

  has_more = raw["hasMore"]
  unless has_more.nil? || [true, false].include?(has_more)
    raise Errors::ValidationError.new(message: "Field 'hasMore' must be boolean or absent in PaginatedPage")
  end

  decoded = raw["data"].each_with_index.map do |item, i|
    item_type.from_hash(item, context: "PaginatedPage.data[#{i}]")
  end

  new(data: decoded, limit: limit, skip: skip, total: total, has_more: has_more)
end

.require_integer(raw, key) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/blueticks/types/paginated.rb', line 53

def self.require_integer(raw, key)
  value = raw[key]
  unless value.is_a?(Integer) && !value.is_a?(TrueClass) && !value.is_a?(FalseClass)
    raise Errors::ValidationError.new(message: "Field '#{key}' must be integer in PaginatedPage")
  end

  value
end

Instance Method Details

#to_hObject



62
63
64
65
66
67
# File 'lib/blueticks/types/paginated.rb', line 62

def to_h
  {
    data: data.map { |d| d.respond_to?(:to_h) ? d.to_h : d },
    limit: limit, skip: skip, total: total, has_more: has_more
  }
end