lib/ably/models/paginated_resource.rb in ably-0.1.6 vs lib/ably/models/paginated_resource.rb in ably-0.2.0
- old
+ new
@@ -1,8 +1,8 @@
module Ably::Models
# 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?}
+ # the array of resources using {#first_page}, {#next_page}, {#first_page?} and {#last_page?}
#
# Paging information is provided by Ably in the LINK HTTP headers
class PaginatedResource
include Enumerable
@@ -11,39 +11,44 @@
# @param [Client] client {Ably::Client} used to make the request to Ably
# @param [Hash] options Options for this paged resource
# @option options [Symbol,String] :coerce_into symbol or string representing class that should be used to create each item in the PaginatedResource
#
# @return [PaginatedResource]
- def initialize(http_response, base_url, client, options = {})
+ def initialize(http_response, base_url, client, options = {}, &each_block)
@http_response = http_response
@client = client
@base_url = "#{base_url.gsub(%r{/[^/]*$}, '')}/"
@coerce_into = options[:coerce_into]
@raw_body = http_response.body
+ @each_block = each_block
@body = if @coerce_into
http_response.body.map do |item|
Kernel.const_get(@coerce_into).new(item)
end
else
http_response.body
end
+
+ @body = @body.map do |resource|
+ each_block.call(resource)
+ end if block_given?
end
# Retrieve the first page of results
#
# @return [PaginatedResource]
def first_page
- PaginatedResource.new(client.get(pagination_url('first')), base_url, client, coerce_into: coerce_into)
+ PaginatedResource.new(client.get(pagination_url('first')), base_url, client, coerce_into: coerce_into, &each_block)
end
# Retrieve the next page of results
#
# @return [PaginatedResource]
def next_page
- raise Ably::Exceptions::InvalidPageError, "There are no more pages" if supports_pagination? && last_page?
- PaginatedResource.new(client.get(pagination_url('next')), base_url, client, coerce_into: coerce_into)
+ raise Ably::Exceptions::InvalidPageError, 'There are no more pages' if supports_pagination? && last_page?
+ PaginatedResource.new(client.get(pagination_url('next')), base_url, client, coerce_into: coerce_into, &each_block)
end
# True if this is the last page in the paged resource set
#
# @return [Boolean]
@@ -98,11 +103,23 @@
# Last item in this page
def last
body.last
end
+ def inspect
+ <<-EOF.gsub(/^ /, '')
+ #<#{self.class.name}:#{self.object_id}
+ @base_url="#{base_url}",
+ @first_page?=#{!!first_page?},
+ @last_page?=#{!!first_page?},
+ @body=
+ #{body.map { |item| item.inspect }.join(",\n ") }
+ >
+ EOF
+ end
+
private
- attr_reader :body, :http_response, :base_url, :client, :coerce_into, :raw_body
+ attr_reader :body, :http_response, :base_url, :client, :coerce_into, :raw_body, :each_block
def pagination_headers
link_regex = %r{<(?<url>[^>]+)>; rel="(?<rel>[^"]+)"}
@pagination_headers ||= begin
# All `Link:` headers are concatenated by Faraday into a comma separated list