Class: Ably::Rest::Models::PagedResource

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(http_response, base_url, client, coerce_into: nil) ⇒ PagedResource

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)

    Options for this paged resource



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



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_pagePagedResource

Retrieve the first page of results

Returns:



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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/ably/rest/models/paged_resource.rb', line 49

def last_page?
  !supports_pagination? ||
    pagination_header('next').nil?
end

#lengthObject 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_pagePagedResource

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

Returns:

  • (Boolean)


65
66
67
# File 'lib/ably/rest/models/paged_resource.rb', line 65

def supports_pagination?
  !pagination_headers.empty?
end