Class: Zizq::Resources::Page
- Includes:
- Enumerable
- Defined in:
- lib/zizq/resources/page.rb
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(&block) ⇒ Object
Yields each item on this page.
-
#has_next? ⇒ Boolean
Returns true if there is a next page that can be fetched.
-
#has_prev? ⇒ Boolean
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.
Methods inherited from Resource
Constructor Details
This class inherits a constructor from Zizq::Resources::Resource
Instance Method Details
#each(&block) ⇒ Object
Yields each item on this page. Required by Enumerable.
39 40 41 |
# File 'lib/zizq/resources/page.rb', line 39 def each(&block) items.each(&block) end |
#has_next? ⇒ Boolean
Returns true if there is a next page that can be fetched.
44 45 46 |
# File 'lib/zizq/resources/page.rb', line 44 def has_next? #: () -> bool !!@data.dig("pages", "next") end |
#has_prev? ⇒ Boolean
Returns true if there is a previous page that can be fetched.
49 50 51 |
# File 'lib/zizq/resources/page.rb', line 49 def has_prev? #: () -> bool !!@data.dig("pages", "prev") end |
#items ⇒ Object
Wrapped resource objects for this page.
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_page ⇒ Object
Fetch the next page, or nil if there isn’t one.
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_page ⇒ Object
Fetch the previous page, or nil if there isn’t one.
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_h ⇒ Object
Returns the underlying raw response hash.
Re-declared here because Enumerable#to_h would otherwise shadow the Resource#to_h definition.
31 32 33 |
# File 'lib/zizq/resources/page.rb', line 31 def to_h #: () -> Hash[String, untyped] @data end |