class TheCity::Collection
Utility class for collections with paged responses
Attributes
Public Class Methods
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
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
# File lib/the_city/collection.rb, line 102 def [](n) @collection[n] end
# File lib/the_city/collection.rb, line 61 def current_cursor @current_cursor end
# File lib/the_city/collection.rb, line 85 def current_page @current_page end
@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
@return [Boolean]
# File lib/the_city/collection.rb, line 76 def first? previous_cursor.zero? end
# File lib/the_city/collection.rb, line 106 def last @collection.last end
@return [Boolean]
# File lib/the_city/collection.rb, line 81 def last? current_cursor >= total_entries end
# File lib/the_city/collection.rb, line 93 def last_page? current_page == @total_pages end
# File lib/the_city/collection.rb, line 65 def next_cursur (current_cursor + 1) || -1 end
# File lib/the_city/collection.rb, line 89 def next_page current_page + 1 end
# File lib/the_city/collection.rb, line 70 def previous_cursor current_cursor - 1 end
# File lib/the_city/collection.rb, line 97 def total_entries @total_entries.is_a?(Array) ? @total_entries.first : @total_entries end
Private Instance Methods
# 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
# 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