Class: BitBucket::PageIterator

Inherits:
Object
  • Object
show all
Includes:
Constants, PagedRequest, Utils::Url
Defined in:
lib/bitbucket_rest_api/page_iterator.rb

Constant Summary collapse

ATTRIBUTES =

Setup attribute accesor for all the link types

[ META_FIRST, META_NEXT, META_PREV ]

Constants included from PagedRequest

BitBucket::PagedRequest::FIRST_PAGE, BitBucket::PagedRequest::NOT_FOUND

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

Constants included from Utils::Url

Utils::Url::DEFAULT_QUERY_SEP, Utils::Url::KEY_VALUE_SEP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PagedRequest

#default_page, #page_request

Methods included from Normalizer

#normalize!

Methods included from Utils::Url

#build_query, #escape, #parse_query, #parse_query_for_param, #unescape

Constructor Details

#initialize(links, current_api) ⇒ PageIterator

Returns a new instance of PageIterator.



21
22
23
24
25
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 21

def initialize(links, current_api)
  @links        = links
  @current_api = current_api
  update_page_links @links
end

Instance Attribute Details

#current_apiObject (readonly)

Returns the value of attribute current_api.



19
20
21
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 19

def current_api
  @current_api
end

Instance Method Details

#firstObject

Perform http get request for the first resource



33
34
35
36
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 33

def first
  return nil unless first_page_uri
  perform_request(first_page_uri)
end

#get_page(page_number) ⇒ Object

Returns the result for a specific page.



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 54

def get_page(page_number)
  # Find URI that we can work with, if we cannot get the first
  # page URI then there is only one page.
  return nil unless first_page_uri
  params = parse_query URI(first_page_uri).query
  params['page']     = page_number

  response = page_request URI(first_page_uri).path, params
  update_page_links response.links
  response
end

#has_next?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 27

def has_next?
  next_page == 0 || !next_page_uri.nil?
end

#nextObject

Perform http get request for the next resource



40
41
42
43
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 40

def next
  return nil unless has_next?
  perform_request(next_page_uri)
end

#prevObject

Perform http get request for the previous resource



47
48
49
50
# File 'lib/bitbucket_rest_api/page_iterator.rb', line 47

def prev
  return nil unless prev_page_uri
  perform_request(prev_page_uri)
end