Class: Ably::Models::PaginatedResult
- Inherits:
-
Object
- Object
- Ably::Models::PaginatedResult
- Includes:
- Ably::Modules::AsyncWrapper
- Defined in:
- lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb
Overview
Contains a page of results for message or presence history, stats, or REST presence requests. A PaginatedResult response from a REST API paginated query is also accompanied by metadata that indicates the relative queries available to the PaginatedResult object.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#items ⇒ Array
readonly
Contains the current page of results; for example, an array of Message or PresenceMessage objects for a channel history request.
Instance Method Summary collapse
-
#first(&success_callback) ⇒ PaginatedResult, Ably::Util::SafeDeferrable
Returns a new PaginatedResult for the first page of results.
-
#has_next? ⇒ Boolean
Returns true if there are more pages available by calling next and returns false if this page is the last page available.
- #initialize(http_response, base_url, client, options = {}) {|Object| ... } ⇒ PaginatedResult constructor
- #inspect ⇒ Object
-
#last? ⇒ Boolean
Returns true if this page is the last page and returns false if there are more pages available by calling next available.
-
#next(&success_callback) ⇒ PaginatedResult, Ably::Util::SafeDeferrable
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, options = {}) {|Object| ... } ⇒ PaginatedResult
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 28 def initialize(http_response, base_url, client, = {}, &each_block) @http_response = http_response @client = client @base_url = "#{base_url.gsub(%r{/[^/]*$}, '')}/" @coerce_into = [:coerce_into] @raw_body = http_response.body @each_block = each_block @make_async = .fetch(:async_blocking_operations, false) @items = http_response.body if @items.nil? || @items.to_s.strip.empty? @items = [] end @items = [@items] if @items.kind_of?(Hash) @items = coerce_items_into(items, @coerce_into) if @coerce_into @items = items.map { |item| yield item } if block_given? end |
Instance Attribute Details
#items ⇒ Array (readonly)
Contains the current page of results; for example, an array of Message or Ably::Models::PresenceMessage objects for a channel history request.
16 17 18 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 16 def items @items end |
Instance Method Details
#first(&success_callback) ⇒ PaginatedResult, Ably::Util::SafeDeferrable
Returns a new PaginatedResult for the first page of results.
53 54 55 56 57 58 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 53 def first(&success_callback) async_wrap_if_realtime(success_callback) do return nil unless supports_pagination? PaginatedResult.new(client.get(pagination_url('first')), base_url, client, , &each_block) end end |
#has_next? ⇒ Boolean
Returns true if there are more pages available by calling next and returns false if this page is the last page available.
93 94 95 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 93 def has_next? supports_pagination? && !last? end |
#inspect ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 105 def inspect <<-EOF.gsub(/^ /, '') #<#{self.class.name}:#{self.object_id} @base_url="#{base_url}", @last?=#{!!last?}, @has_next?=#{!!has_next?}, @items= #{items.map { |item| item.inspect }.join(",\n ") } > EOF end |
#last? ⇒ Boolean
Returns true if this page is the last page and returns false if there are more pages available by calling next available.
82 83 84 85 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 82 def last? !supports_pagination? || pagination_header('next').nil? end |
#next(&success_callback) ⇒ PaginatedResult, Ably::Util::SafeDeferrable
Retrieve the next page of results.
When used as part of the Realtime library, it will return a Util::SafeDeferrable object, and allows an optional success callback block to be provided.
69 70 71 72 73 74 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 69 def next(&success_callback) async_wrap_if_realtime(success_callback) do return nil unless has_next? PaginatedResult.new(client.get(pagination_url('next')), base_url, client, , &each_block) end end |
#supports_pagination? ⇒ Boolean
True if the HTTP response supports paging with the expected LINK HTTP headers
101 102 103 |
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 101 def supports_pagination? !pagination_headers.empty? end |