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.

RBS:

  • generic T < Resource

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.

RBS:

  • &block: (T) -> void

  • return: Enumerator[T, void] | void

Yields:

Yield Parameters:

  • arg0 (T)

Yield Returns:

  • (void)

Returns:

  • (Object)


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

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

#has_next?Object

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

Returns:

  • (Object)


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

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)


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

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

#itemsObject

Wrapped resource objects for this page.

Returns:

  • (Object)


23
24
25
26
# 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)


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

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)


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

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)


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

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)


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

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