Class: Assembla::PageIterator

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

Constant Summary collapse

ATTRIBUTES =

Setup attribute accesor for all the link types

[ META_FIRST, META_NEXT, META_PREV, META_LAST ]
DEFAULT_SHA =
'master'

Constants included from PagedRequest

Assembla::PagedRequest::FIRST_PAGE, Assembla::PagedRequest::NOT_FOUND, Assembla::PagedRequest::PER_PAGE

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

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, #default_page_size, #page_request

Methods included from Utils::Url

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

Constructor Details

#initialize(links, current_api) ⇒ PageIterator

Returns a new instance of PageIterator.



23
24
25
26
27
# File 'lib/assembla_api/page_iterator.rb', line 23

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.



21
22
23
# File 'lib/assembla_api/page_iterator.rb', line 21

def current_api
  @current_api
end

Instance Method Details

#countObject



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

def count
  return nil unless last_page_uri
  parse_query(URI(last_page_uri).query)['page']
end

#firstObject

Perform http get request for the first resource



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

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.



68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/assembla_api/page_iterator.rb', line 68

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

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

#has_next?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/assembla_api/page_iterator.rb', line 29

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

#lastObject

Perform http get request for the last resource



61
62
63
64
# File 'lib/assembla_api/page_iterator.rb', line 61

def last
  return nil unless last_page_uri
  perform_request(last_page_uri)
end

#nextObject

Perform http get request for the next resource



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

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

#prevObject

Perform http get request for the previous resource



54
55
56
57
# File 'lib/assembla_api/page_iterator.rb', line 54

def prev
  return nil unless prev_page_uri
  perform_request(prev_page_uri)
end