class ElvantoAPI::Pager

Constants

DEFAULT_LIMIT
DEFAULT_SEP

Attributes

href[RW]
options[RW]

Public Class Methods

new(href, options = {}) click to toggle source

A pager for paginating through resource records.

@param [String] uri the uri of the resource @param [Hash] options @option options [Integer] limit @option options [Integer] offset @option options [Integer] per an alias for the :limit option

# File lib/elvanto/pager.rb, line 20
def initialize(href, options = {})
  @href = href
  @options = options
  @page = nil
  @resource_class = nil
end

Public Instance Methods

all(options = {}) click to toggle source
# File lib/elvanto/pager.rb, line 158
def all(options = {})
  paginate(options).to_a
end
current_page() click to toggle source
# File lib/elvanto/pager.rb, line 68
def current_page
  @page[@resource_class.collection_name][:page]
end
each() { |construct_from_response| ... } click to toggle source

@return [Array] Iterates through the current page of records. @yield [record]

# File lib/elvanto/pager.rb, line 80
def each
  return enum_for :each unless block_given?

  load! unless @page
  loop do
    items.each do |r|
      envelope = {
        @resource_class.member_name.to_sym => [r]
      }
      yield resource_class.construct_from_response(envelope)
    end
    raise StopIteration if last_page?
    self.next
  end
end
fetch_each() { |record| ... } click to toggle source

@return [nil] @see Resource.fetch_each @yield [record]

# File lib/elvanto/pager.rb, line 107
def fetch_each
  return enum_for :fetch_each unless block_given?
  begin
    each { |record| yield record }
  end while self.next
end
first() click to toggle source
# File lib/elvanto/pager.rb, line 33
def first
  load! unless @page
  if items.first.nil?
    nil
  else
    envelope = {
      :meta => @page[:meta],
      :links => @page[:links],
      @resource_class.collection_name.to_sym => [items.first]
    }
    resource_class.construct_from_response(envelope)
  end
end
first_page?() click to toggle source
# File lib/elvanto/pager.rb, line 100
def first_page?
  current_page == 1
end
items() click to toggle source
# File lib/elvanto/pager.rb, line 59
def items
  load! unless @page
  if @resource_class.nil?
    []
  else
    @page[@resource_class.collection_name][@resource_class.member_name]
  end
end
last_page?() click to toggle source
# File lib/elvanto/pager.rb, line 96
def last_page?
  current_page == num_pages
end
limit() click to toggle source
# File lib/elvanto/pager.rb, line 52
def limit
  load! unless @page
  @page[@resource_class.collection_name][:per_page]
end
Also aliased as: limit_value
limit_value()
Alias for: limit
load!() click to toggle source

@return [Array, nil] Load (or reload) the pager's collection from the original, supplied options.

# File lib/elvanto/pager.rb, line 141
def load!
  load_from @href, @options
end
Also aliased as: reload
next() click to toggle source

@return [Array, nil] Refreshes the pager's collection of records with the next page.

# File lib/elvanto/pager.rb, line 116
def next
  load! unless @page

  new_options = @options.merge({page: current_page + 1})
  load_from @href, new_options unless last_page?
end
num_pages() click to toggle source
# File lib/elvanto/pager.rb, line 72
def num_pages
  num = total / limit
  num += 1 if total % limit > 0
  num
end
paginate(options = {}) click to toggle source

@return [Pager] Duplicates the pager, updating it with the options supplied. Useful for resource scopes. @see initialize

# File lib/elvanto/pager.rb, line 149
def paginate(options = {})
  dup.instance_eval {
    @page = nil
    @options.update options and self
  }
end
Also aliased as: scoped, where
prev() click to toggle source

@return [Array, nil] Refreshes the pager's collection of records with the previous page.

# File lib/elvanto/pager.rb, line 125
def prev
  load! unless @page
  new_options = @options.merge({page: current_page - 1})
  load_from @href, new_options unless first_page?
end
reload()
Alias for: load!
resource_class() click to toggle source
# File lib/elvanto/pager.rb, line 27
def resource_class
  return @resource_class unless @resource_class.nil?
  load! unless @page
  @resource_class
end
scoped(options = {})
Alias for: paginate
start() click to toggle source

@return [Array, nil] Refreshes the pager's collection of records with the first page.

# File lib/elvanto/pager.rb, line 133
def start
  load! unless @page
  new_options = @options.merge({page: 1})
  load_from @href, new_options
end
total() click to toggle source
# File lib/elvanto/pager.rb, line 47
def total
  load! unless @page
  @page[@resource_class.collection_name][:total]
end
where(options = {})
Alias for: paginate

Private Instance Methods

load_from(uri, params) click to toggle source
# File lib/elvanto/pager.rb, line 165
def load_from(uri, params)
  parsed_uri = URI.parse(uri)

  params ||= {}
  params = params.dup

  unless parsed_uri.query.nil?
    # The reason we don't use CGI::parse here is because
    # the ElvantoAPI api currently can't handle variable[]=value.
    # Faraday ends up encoding a simple query string like:
    # {"limit"=>["10"], "offset"=>["0"]}
    # to limit[]=10&offset[]=0 and that's cool, but
    # we have to make sure ElvantoAPI supports it.
    query_params = parse_query(parsed_uri.query)
    params.merge! query_params
    parsed_uri.query = nil
  end

  response = ElvantoAPI.post parsed_uri.to_s, params
  @page = ElvantoAPI::Utils.indifferent_read_access response.body

  #@href = @page[:meta][:href]
  # resource_class?
  hypermedia_key = (@page.keys.map{|k| k.to_sym } - [:generated_in, :status]).first
  
  unless hypermedia_key.nil?
    @resource_class = ("ElvantoAPI::" + ElvantoAPI::Utils.classify(hypermedia_key)).constantize
  end
  
  @page
end
parse_query(qs, d = nil) click to toggle source

Stolen from Mongrel, with some small modifications: Parses a query string by breaking it up at the '&' and ';' characters. You can also use this to parse cookies by changing the characters used in the second parameter (which defaults to '&;').

# File lib/elvanto/pager.rb, line 203
def parse_query(qs, d = nil)
  params = {}

  (qs || '').split(d ? /[#{d}] */n : DEFAULT_SEP).each do |p|
    k, v = p.split('=', 2).map { |x| CGI::unescape(x) }
    if (cur = params[k])
      if cur.class == Array
        params[k] << v
      else
        params[k] = [cur, v]
      end
    else
      params[k] = v
    end
  end

  params
end