Class: Zizq::Resources::Page

Inherits:
Resource show all
Includes:
Enumerable, Enumerable[T]
Defined in:
lib/zizq/resources/page.rb,
sig/generated/zizq/resources/page.rbs

Overview

Base class for paginated list responses.

Stores the raw response data and provides navigation helpers that follow pagination links through the Client.

Direct Known Subclasses

ErrorPage, JobPage

Instance Attribute Summary

Attributes inherited from Resource

#client

Instance Method Summary collapse

Methods inherited from Resource

#initialize, #inspect, #ms_to_seconds

Constructor Details

This class inherits a constructor from Zizq::Resources::Resource

Instance Method Details

#each {|arg0| ... } ⇒ Object

Yields each item on this page. Required by Enumerable.

Yields:

Yield Parameters:

  • arg0 (T)

Yield Returns:

  • (void)

Returns:

  • (Object)


39
40
41
# File 'lib/zizq/resources/page.rb', line 39

def each(&block)
  items.each(&block)
end

#has_next?Object

Returns true if there is a next page that can be fetched.

Returns:

  • (Object)


44
45
46
# File 'lib/zizq/resources/page.rb', line 44

def has_next? #: () -> bool
  !!@data.dig("pages", "next")
end

#has_prev?Object

Returns true if there is a previous page that can be fetched.

Returns:

  • (Object)


49
50
51
# File 'lib/zizq/resources/page.rb', line 49

def has_prev? #: () -> bool
  !!@data.dig("pages", "prev")
end

#itemsObject

Wrapped resource objects for this page.

Returns:

  • (Object)


23
24
25
# File 'lib/zizq/resources/page.rb', line 23

def items #: () -> Array[T]
  raise NotImplementedError, "#{self.class.name}#items must be implemented"
end

#next_pageObject

Fetch the next page, or nil if there isn't one.

Returns:

  • (Object)


54
55
56
57
58
59
# File 'lib/zizq/resources/page.rb', line 54

def next_page #: () -> Page?
  path = @data.dig("pages", "next")
  return nil unless path

  wrap_page(client.get_path(path))
end

#prev_pageObject

Fetch the previous page, or nil if there isn't one.

Returns:

  • (Object)


62
63
64
65
66
67
# File 'lib/zizq/resources/page.rb', line 62

def prev_page #: () -> Page?
  path = @data.dig("pages", "prev")
  return nil unless path

  wrap_page(client.get_path(path))
end

#to_hObject

Returns the underlying raw response hash.

Re-declared here because Enumerable#to_h would otherwise shadow the Resource#to_h definition.

Returns:

  • (Object)


31
32
33
# File 'lib/zizq/resources/page.rb', line 31

def to_h #: () -> Hash[String, untyped]
  @data
end

#wrap_page(data) ⇒ Object

Subclasses override to wrap the raw page data in the correct Page type.

Parameters:

  • data (Object)

Returns:

  • (Object)


72
73
74
# File 'lib/zizq/resources/page.rb', line 72

def wrap_page(data) #: (Hash[String, untyped]) -> Page
  self.class.new(client, data)
end