Class: Zizq::Resources::Page
- 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.
Instance Attribute Summary
Attributes inherited from Resource
Instance Method Summary collapse
-
#each {|arg0| ... } ⇒ Object
Yields each item on this page.
-
#has_next? ⇒ Object
Returns true if there is a next page that can be fetched.
-
#has_prev? ⇒ Object
Returns true if there is a previous page that can be fetched.
-
#items ⇒ Object
Wrapped resource objects for this page.
-
#next_page ⇒ Object
Fetch the next page, or nil if there isn't one.
-
#prev_page ⇒ Object
Fetch the previous page, or nil if there isn't one.
-
#to_h ⇒ Object
Returns the underlying raw response hash.
-
#wrap_page(data) ⇒ Object
Subclasses override to wrap the raw page data in the correct Page type.
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.
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.
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.
50 51 52 |
# File 'lib/zizq/resources/page.rb', line 50 def has_prev? #: () -> bool !!@data.dig("pages", "prev") end |
#items ⇒ Object
Wrapped resource objects for this page.
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_page ⇒ Object
Fetch the next page, or nil if there isn't one.
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_page ⇒ Object
Fetch the previous page, or nil if there isn't one.
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_h ⇒ Object
Returns the underlying raw response hash.
Re-declared here because Enumerable#to_h would otherwise shadow the Resource#to_h definition.
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.
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 |