Module: Nimbu::Pagination

Includes:
Utils::Constants
Included in:
Response::Wrapper
Defined in:
lib/nimbu-api/pagination.rb,
lib/nimbu-api/pagination/page_links.rb,
lib/nimbu-api/pagination/pagination.rb,
lib/nimbu-api/pagination/page_iterator.rb,
lib/nimbu-api/pagination/paged_request.rb

Overview

A module that decorates response with pagination helpers

Defined Under Namespace

Modules: PagedRequest Classes: PageIterator, PageLinks

Constant Summary

Constants included from Utils::Constants

Utils::Constants::ACCEPT, Utils::Constants::ACCEPTED_OAUTH_SCOPES, Utils::Constants::ACCEPT_CHARSET, Utils::Constants::CACHE_CONTROL, Utils::Constants::CONTENT_LENGTH, Utils::Constants::CONTENT_TYPE, Utils::Constants::DATE, Utils::Constants::ETAG, Utils::Constants::HEADER_LAST, Utils::Constants::HEADER_LINK, Utils::Constants::HEADER_NEXT, Utils::Constants::LOCATION, Utils::Constants::META_FIRST, Utils::Constants::META_LAST, Utils::Constants::META_NEXT, Utils::Constants::META_PREV, Utils::Constants::META_REL, Utils::Constants::NIMBU_SITE, Utils::Constants::OAUTH_SCOPES, Utils::Constants::PARAM_PAGE, Utils::Constants::PARAM_PER_PAGE, Utils::Constants::PARAM_START_PAGE, Utils::Constants::RATELIMIT_LIMIT, Utils::Constants::RATELIMIT_REMAINING, Utils::Constants::SERVER, Utils::Constants::USER_AGENT

Instance Method Summary collapse

Instance Method Details

#auto_paginate(auto = false) ⇒ Object

Iterate over results set pages by automatically calling ‘next_page` until all pages are exhausted. Caution needs to be exercised when using this feature - 100 pages iteration will perform 100 API calls. By default this is off. You can set it on the client, individual API instances or just per given request.



29
30
31
32
33
34
35
36
# File 'lib/nimbu-api/pagination.rb', line 29

def auto_paginate(auto=false)
  if links.any? && (current_api.auto_pagination? || auto)
    resources_bodies = []
    each_page { |resource| resources_bodies += resource.body }
    self.body = resources_bodies
  end
  self
end

#count_pagesObject

Retrive number of total pages base on current :per_page parameter



19
20
21
# File 'lib/nimbu-api/pagination.rb', line 19

def count_pages
  page_iterator.count.to_i
end

#each_page {|_self| ... } ⇒ Object

Iterator like each for response pages. If there are no pages to iterate over this method will return current page.

Yields:

  • (_self)

Yield Parameters:



40
41
42
43
44
45
# File 'lib/nimbu-api/pagination.rb', line 40

def each_page
  yield self
  while page_iterator.has_next?
    yield next_page
  end
end

#first_pageObject

Retrives the result of the first page. Returns nil if there is no first page - either because you are already on the first page or there are no pages at all in the result.



50
51
52
53
54
# File 'lib/nimbu-api/pagination.rb', line 50

def first_page
  first_request = page_iterator.first
  self.instance_eval { @env = first_request.env } if first_request
  first_request
end

#has_next_page?Boolean

Returns true if there is another page in the result set, otherwise false

Returns:

  • (Boolean)


94
95
96
# File 'lib/nimbu-api/pagination.rb', line 94

def has_next_page?
  page_iterator.has_next?
end

#last_pageObject

Retrives the result of the last page. Returns nil if there is no last page - either because you are already on the last page, there is only one page or there are no pages at all in the result.



76
77
78
79
80
# File 'lib/nimbu-api/pagination.rb', line 76

def last_page
  last_request = page_iterator.last
  self.instance_eval { @env = last_request.env } if last_request
  last_request
end

Return page links



14
15
16
# File 'lib/nimbu-api/pagination.rb', line 14

def links
  @links = Nimbu::Pagination::PageLinks.new(env[:response_headers])
end

#next_pageObject

Retrives the result of the next page. Returns nil if there is no next page or no pages at all.



58
59
60
61
62
# File 'lib/nimbu-api/pagination.rb', line 58

def next_page
  next_request = page_iterator.next
  self.instance_eval { @env = next_request.env } if next_request
  next_request
end

#page(page_number) ⇒ Object

Retrives a specific result for a page given page number. The page_number parameter is not validate, hitting a page that does not exist will return Nimbu API error. Consequently, if there is only one page, this method returns nil



86
87
88
89
90
# File 'lib/nimbu-api/pagination.rb', line 86

def page(page_number)
  request = page_iterator.get_page(page_number)
  self.instance_eval { @env = request.env } if request
  request
end

#prev_pageObject Also known as: previous_page

Retrives the result of the previous page. Returns nil if there is no previous page or no pages at all.



66
67
68
69
70
# File 'lib/nimbu-api/pagination.rb', line 66

def prev_page
  prev_request = page_iterator.prev
  self.instance_eval { @env = prev_request.env } if prev_request
  prev_request
end