Module: BitBucket::Pagination

Includes:
Constants
Included in:
ResponseWrapper, Result
Defined in:
lib/bitbucket_rest_api/pagination.rb

Overview

A module that decorates response with pagination helpers

Constant Summary

Constants included from Constants

Constants::ACCEPT, Constants::ACCEPT_CHARSET, Constants::CACHE_CONTROL, Constants::CONTENT_LENGTH, Constants::CONTENT_TYPE, Constants::DATE, Constants::ETAG, Constants::LOCATION, Constants::META_FIRST, Constants::META_LAST, Constants::META_NEXT, Constants::META_PREV, Constants::META_REL, Constants::PARAM_PAGE, Constants::PARAM_START_PAGE, Constants::QUERY_STR_SEP, Constants::RATELIMIT_LIMIT, Constants::RATELIMIT_REMAINING, Constants::SERVER, 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.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/bitbucket_rest_api/pagination.rb', line 23

def auto_paginate(auto=false)
  if paginated? and (current_api.auto_pagination? || auto)
    resources_bodies = []
    each_page do |resource|
      if resource.body.respond_to?(:values) and resource.body[:values]
        resources_bodies += resource.body[:values].collect {|value| ::Hashie::Mash.new(value) }
      else
        resources_bodies += Array(resource.body)
      end
    end
    self.body = resources_bodies
  end
  self
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/bitbucket_rest_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/bitbucket_rest_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)


85
86
87
# File 'lib/bitbucket_rest_api/pagination.rb', line 85

def has_next_page?
  page_iterator.has_next?
end

Return page links



13
14
15
# File 'lib/bitbucket_rest_api/pagination.rb', line 13

def links
  @links = BitBucket::PageLinks.new(self.response)
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/bitbucket_rest_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 BitBucket API error. Consequently, if there is only one page, this method returns nil



77
78
79
80
81
# File 'lib/bitbucket_rest_api/pagination.rb', line 77

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

#paginated?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/bitbucket_rest_api/pagination.rb', line 8

def paginated?
  body.is_a?(Hash) and !body[PARAM_PAGE].nil?
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/bitbucket_rest_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