Class: Ably::Models::PaginatedResult

Inherits:
Object
  • Object
show all
Includes:
Ably::Modules::AsyncWrapper
Defined in:
lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb

Overview

Wraps any Ably HTTP response that supports paging and provides methods to iterate through the pages using #first, #next, #has_next? and #last?

All items in the HTTP response are available in the Array returned from #items

Paging information is provided by Ably in the LINK HTTP headers

Direct Known Subclasses

HttpPaginatedResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, base_url, client, options = {}) {|Object| ... } ⇒ PaginatedResult

Parameters:

  • http_response (Faraday::Response)

    Initial HTTP response from an Ably request to a paged resource

  • base_url (String)

    Base URL for request that generated the http_response so that subsequent paged requests can be made

  • client (Client)

    Client used to make the request to Ably

  • options (Hash) (defaults to: {})

    Options for this paged resource

Options Hash (options):

  • :coerce_into (Symbol, String)

    symbol or string representing class that should be used to create each item in the PaginatedResult

Yields:

  • (Object)

    block will be called for each resource object for the current page. This is a useful way to apply a transformation to any page resources after they are retrieved



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 25

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
  @make_async    = options.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

#itemsArray (readonly)

The items contained within this Ably::Models::PaginatedResult

Returns:

  • (Array)


14
15
16
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 14

def items
  @items
end

Instance Method Details

#first(&success_callback) ⇒ PaginatedResult, Ably::Util::SafeDeferrable

Retrieve the first 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.


49
50
51
52
53
54
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 49

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, pagination_options, &each_block)
  end
end

#has_next?Boolean

True if there is a subsequent page in this paginated set available with #next

Returns:

  • (Boolean)


79
80
81
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 79

def has_next?
  supports_pagination? && !last?
end

#inspectObject



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 90

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

True if this is the last page in the paged resource set

Returns:

  • (Boolean)


71
72
73
74
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 71

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.


61
62
63
64
65
66
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 61

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, pagination_options, &each_block)
  end
end

#supports_pagination?Boolean

True if the HTTP response supports paging with the expected LINK HTTP headers

Returns:

  • (Boolean)


86
87
88
# File 'lib/submodules/ably-ruby/lib/ably/models/paginated_result.rb', line 86

def supports_pagination?
  !pagination_headers.empty?
end