Class: Blueticks::Types::Page

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

Overview

Cursor-paginated list envelope. Every v1 list endpoint returns this shape. Iterate ‘data` for the current page; pass `next_cursor` back as the `cursor:` keyword on the next list call to continue.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data:, has_more:, next_cursor: nil) ⇒ Page

Returns a new instance of Page.



14
15
16
17
18
# File 'lib/blueticks/types/page.rb', line 14

def initialize(data:, has_more:, next_cursor: nil)
  @data = data
  @has_more = has_more
  @next_cursor = next_cursor
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



12
13
14
# File 'lib/blueticks/types/page.rb', line 12

def data
  @data
end

#has_moreObject (readonly)

Returns the value of attribute has_more.



12
13
14
# File 'lib/blueticks/types/page.rb', line 12

def has_more
  @has_more
end

#next_cursorObject (readonly)

Returns the value of attribute next_cursor.



12
13
14
# File 'lib/blueticks/types/page.rb', line 12

def next_cursor
  @next_cursor
end

Class Method Details

.from_hash(raw, item_type:) ⇒ Object

Build a Page from a raw response, decoding each item via item_type.from_hash.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/blueticks/types/page.rb', line 21

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

  cursor = raw["next_cursor"]
  unless cursor.nil? || cursor.is_a?(String)
    raise Errors::ValidationError.new(message: "Field 'next_cursor' must be string or null in Page")
  end

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

  new(data: decoded, has_more: raw["has_more"], next_cursor: cursor)
end

Instance Method Details

#to_hObject



40
41
42
# File 'lib/blueticks/types/page.rb', line 40

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