class Assembla::PageIterator

Constants

ATTRIBUTES

Setup attribute accesor for all the link types

DEFAULT_SHA

Attributes

current_api[R]

Public Class Methods

new(links, current_api) click to toggle source
# 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

Public Instance Methods

count() click to toggle source
# 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
first() click to toggle source

Perform http get request for the first resource

# 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) click to toggle source

Returns the result for a specific page.

# 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?() click to toggle source
# File lib/assembla_api/page_iterator.rb, line 29
def has_next?
  next_page == 0 || !next_page_uri.nil?
end
last() click to toggle source

Perform http get request for the last resource

# File lib/assembla_api/page_iterator.rb, line 61
def last
  return nil unless last_page_uri
  perform_request(last_page_uri)
end
next() click to toggle source

Perform http get request for the next resource

# File lib/assembla_api/page_iterator.rb, line 47
def next
  return nil unless has_next?
  perform_request(next_page_uri)
end
prev() click to toggle source

Perform http get request for the previous resource

# File lib/assembla_api/page_iterator.rb, line 54
def prev
  return nil unless prev_page_uri
  perform_request(prev_page_uri)
end

Private Instance Methods

perform_request(attribute) click to toggle source
# File lib/assembla_api/page_iterator.rb, line 84
def perform_request(attribute)
  page_uri = URI(attribute)
  params = parse_query(page_uri.query)

  if next_page < 1
    sha = sha(params)
    params['sha'] = sha if sha
  else
    params['page'] = parse_page_number(attribute)
  end
  params['per_page'] = parse_per_page_number(attribute)

  response = page_request(page_uri.path, params)
  update_page_links response.links
  response
end
sha(params) click to toggle source
# File lib/assembla_api/page_iterator.rb, line 101
def sha(params)
  return params['last_sha'] if params.keys.include?('last_sha')
  return DEFAULT_SHA if params.keys.include?('sha')
  nil
end