class TheCity::Collection

Utility class for collections with paged responses

Attributes

attrs[R]
to_h[R]
to_hash[R]
to_hsh[R]

Public Class Methods

from_response(response, key, klass, client, request_method, path, options) click to toggle source

Construct a new Collection object from a response hash

@param response [Hash] @param key [String, Symbol] The key to fetch the data from the response @param klass [Class] The class to instantiate objects in the response @param client [TheCity::API::Client] @param request_method [String, Symbol] @param path [String] @param options [Hash] @return [TheCity::Collection]

# File lib/the_city/collection.rb, line 20
def self.from_response(response, key, klass, client, request_method, path, options)
  new(response[:body], key, klass, client, request_method, path, options)
end
new(attrs, key, klass, client, request_method, path, options) click to toggle source

Initializes a new Collection

@param attrs [Hash] @param key [String, Symbol] The key to fetch the data from the response @param klass [Class] The class to instantiate objects in the response @param client [TheCity::API::Client] @param request_method [String, Symbol] @param path [String] @param options [Hash] @return [TheCity::Collection]

# File lib/the_city/collection.rb, line 34
def initialize(attrs, key, klass, client, request_method, path, options)
  @key = key.to_sym
  @klass = klass
  @client = client
  @request_method = request_method.to_sym
  @path = path
  @options = options
  @collection = []
  set_attrs(attrs)
end

Public Instance Methods

[](n) click to toggle source
# File lib/the_city/collection.rb, line 102
def [](n)
  @collection[n]
end
current_cursor() click to toggle source
# File lib/the_city/collection.rb, line 61
def current_cursor
  @current_cursor
end
current_page() click to toggle source
# File lib/the_city/collection.rb, line 85
def current_page
  @current_page
end
each(start = 0) { |element| ... } click to toggle source

@return [Enumerator]

# File lib/the_city/collection.rb, line 46
def each(start = 0, &block)
  return to_enum(:each) unless block_given?
  for element in Array(@collection[start..-1])
    yield element
    @current_cursor += 1
  end
  unless last?
    start = [@collection.size, start].max
    fetch_next_page unless last_page?
    each(start, &block)
  end
  @current_cursor = 1
  self
end
first?() click to toggle source

@return [Boolean]

# File lib/the_city/collection.rb, line 76
def first?
  previous_cursor.zero?
end
last() click to toggle source
# File lib/the_city/collection.rb, line 106
def last
  @collection.last
end
last?() click to toggle source

@return [Boolean]

# File lib/the_city/collection.rb, line 81
def last?
  current_cursor >= total_entries
end
last_page?() click to toggle source
# File lib/the_city/collection.rb, line 93
def last_page?
  current_page == @total_pages
end
next()
Alias for: next_cursur
next_cursur() click to toggle source
# File lib/the_city/collection.rb, line 65
def next_cursur
  (current_cursor + 1) || -1
end
Also aliased as: next
next_page() click to toggle source
# File lib/the_city/collection.rb, line 89
def next_page
  current_page + 1
end
previous()
Alias for: previous_cursor
previous_cursor() click to toggle source
# File lib/the_city/collection.rb, line 70
def previous_cursor
  current_cursor - 1
end
Also aliased as: previous
total()
Alias for: total_entries
total_entries() click to toggle source
# File lib/the_city/collection.rb, line 97
def total_entries
  @total_entries.is_a?(Array) ? @total_entries.first : @total_entries
end
Also aliased as: total

Private Instance Methods

fetch_next_page() click to toggle source
# File lib/the_city/collection.rb, line 112
def fetch_next_page
  response = @client.send(@request_method, @path, @options.merge(:page => next_page, :per_page => @per_page))
  set_attrs(response[:body])
end
set_attrs(attrs) click to toggle source
# File lib/the_city/collection.rb, line 117
def set_attrs(attrs)
  @attrs = attrs
  for element in Array(attrs[@key])
    @collection << (@klass ? @klass.new(element) : element)
  end
  @total_entries = attrs[:total_entries],
  @current_page = attrs[:current_page],
  @total_pages = attrs[:total_pages],
  @per_page = attrs[:per_page],
  @current_cursor = ((@current_page - 1) * @per_page) + 1
end