Class: Ably::Rest::Models::PagedResource
- Inherits:
-
Object
- Object
- Ably::Rest::Models::PagedResource
- Includes:
- Enumerable
- Defined in:
- lib/ably/rest/models/paged_resource.rb
Overview
Wraps any Ably HTTP response that supports paging and automatically provides methdos to iterated through the array of resources using #first, #next, #last? and #first?
Paging information is provided by Ably in the LINK HTTP headers
Instance Method Summary collapse
-
#[](index) ⇒ Object
Standard Array accessor method.
-
#each(&block) ⇒ Object
Method ensuring this PagedResource is Enumerable.
-
#first_page ⇒ PagedResource
Retrieve the first page of results.
-
#first_page? ⇒ Boolean
True if this is the first page in the paged resource set.
- #initialize(http_response, base_url, client, coerce_into: nil) ⇒ PagedResource constructor
-
#last_page? ⇒ Boolean
True if this is the last page in the paged resource set.
-
#length ⇒ Object
(also: #count, #size)
Returns number of items within this page, not the total number of items in the entire paged resource set.
-
#next_page ⇒ PagedResource
Retrieve the next page of results.
-
#supports_pagination? ⇒ Boolean
True if the HTTP response supports paging with the expected LINK HTTP headers.
Constructor Details
#initialize(http_response, base_url, client, coerce_into: nil) ⇒ PagedResource
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ably/rest/models/paged_resource.rb', line 16 def initialize(http_response, base_url, client, coerce_into: nil) @http_response = http_response @client = client @base_url = "#{base_url.gsub(%r{/[^/]*$}, '')}/" @coerce_into = coerce_into @body = if coerce_into http_response.body.map do |item| Kernel.const_get(coerce_into).new(item) end else http_response.body end end |
Instance Method Details
#[](index) ⇒ Object
Standard Array accessor method
70 71 72 |
# File 'lib/ably/rest/models/paged_resource.rb', line 70 def [](index) body[index] end |
#each(&block) ⇒ Object
Method ensuring this Ably::Rest::Models::PagedResource is Enumerable
82 83 84 85 86 87 88 89 90 |
# File 'lib/ably/rest/models/paged_resource.rb', line 82 def each(&block) body.each do |item| if block_given? block.call item else yield item end end end |
#first_page ⇒ PagedResource
Retrieve the first page of results
34 35 36 |
# File 'lib/ably/rest/models/paged_resource.rb', line 34 def first_page PagedResource.new(client.get(pagination_url('first')), base_url, client, coerce_into: coerce_into) end |
#first_page? ⇒ Boolean
True if this is the first page in the paged resource set
57 58 59 60 |
# File 'lib/ably/rest/models/paged_resource.rb', line 57 def first_page? !supports_pagination? || pagination_header('first') == pagination_header('current') end |
#last_page? ⇒ Boolean
True if this is the last page in the paged resource set
49 50 51 52 |
# File 'lib/ably/rest/models/paged_resource.rb', line 49 def last_page? !supports_pagination? || pagination_header('next').nil? end |
#length ⇒ Object Also known as: count, size
Returns number of items within this page, not the total number of items in the entire paged resource set
75 76 77 |
# File 'lib/ably/rest/models/paged_resource.rb', line 75 def length body.length end |
#next_page ⇒ PagedResource
Retrieve the next page of results
41 42 43 44 |
# File 'lib/ably/rest/models/paged_resource.rb', line 41 def next_page raise Ably::Exceptions::InvalidPageError, "There are no more pages" if supports_pagination? && last_page? PagedResource.new(client.get(pagination_url('next')), base_url, client, coerce_into: coerce_into) end |
#supports_pagination? ⇒ Boolean
True if the HTTP response supports paging with the expected LINK HTTP headers
65 66 67 |
# File 'lib/ably/rest/models/paged_resource.rb', line 65 def supports_pagination? !pagination_headers.empty? end |