Module: Assembla::Pagination
Overview
A module that decorates response with pagination helpers
Constant Summary
Constants included from Constants
Constants::ACCEPT, Constants::ACCEPTED_OAUTH_SCOPES, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::HEADER_LAST, Constants::HEADER_LINK, Constants::HEADER_NEXT, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::OAUTH_SCOPES, Constants::PARAM_PAGE, Constants::PARAM_PER_PAGE, Constants::PARAM_START_PAGE, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::SERVER, Constants::USER_AGENT
Instance Method Summary collapse
-
#auto_paginate(auto = false) ⇒ Object
Iterate over results set pages by automatically calling ‘next_page` until all pages are exhausted.
-
#count_pages ⇒ Object
Retrive number of total pages base on current :per_page parameter.
-
#each_page {|_self| ... } ⇒ Object
Iterator like each for response pages.
-
#first_page ⇒ Object
Retrives the result of the first page.
-
#has_next_page? ⇒ Boolean
Returns
true
if there is another page in the result set, otherwisefalse
. -
#last_page ⇒ Object
Retrives the result of the last page.
-
#links ⇒ Object
Return page links.
-
#next_page ⇒ Object
Retrives the result of the next page.
-
#page(page_number) ⇒ Object
Retrives a specific result for a page given page number.
-
#prev_page ⇒ Object
(also: #previous_page)
Retrives the result of the previous page.
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.
25 26 27 28 29 30 31 32 |
# File 'lib/assembla_api/pagination.rb', line 25 def auto_paginate(auto=false) if (current_api.auto_pagination? || auto) && self.body.is_a?(Array) resources_bodies = [] each_page { |resource| resources_bodies += resource.body } self.body = resources_bodies end self end |
#count_pages ⇒ Object
Retrive number of total pages base on current :per_page parameter
15 16 17 |
# File 'lib/assembla_api/pagination.rb', line 15 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.
36 37 38 39 40 41 |
# File 'lib/assembla_api/pagination.rb', line 36 def each_page yield self while page_iterator.has_next? yield next_page end end |
#first_page ⇒ Object
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.
46 47 48 49 50 |
# File 'lib/assembla_api/pagination.rb', line 46 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
90 91 92 |
# File 'lib/assembla_api/pagination.rb', line 90 def has_next_page? page_iterator.has_next? end |
#last_page ⇒ Object
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.
72 73 74 75 76 |
# File 'lib/assembla_api/pagination.rb', line 72 def last_page last_request = page_iterator.last self.instance_eval { @env = last_request.env } if last_request last_request end |
#links ⇒ Object
Return page links
10 11 12 |
# File 'lib/assembla_api/pagination.rb', line 10 def links @links = Assembla::PageLinks.new(env[:response_headers]) end |
#next_page ⇒ Object
Retrives the result of the next page. Returns nil
if there is no next page or no pages at all.
54 55 56 57 58 |
# File 'lib/assembla_api/pagination.rb', line 54 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 Assembla API error. Consequently, if there is only one page, this method returns nil
82 83 84 85 86 |
# File 'lib/assembla_api/pagination.rb', line 82 def page(page_number) request = page_iterator.get_page(page_number) self.instance_eval { @env = request.env } if request request end |
#prev_page ⇒ Object 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.
62 63 64 65 66 |
# File 'lib/assembla_api/pagination.rb', line 62 def prev_page prev_request = page_iterator.prev self.instance_eval { @env = prev_request.env } if prev_request prev_request end |