Method: Pinterest::Collection#current_cursor
- Defined in:
-
lib/pinterest/collection.rb,
lib/pinterest/collection.rb
Returns The cursor to obtain the current page of results.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/pinterest/collection.rb', line 17 class Collection attr_reader :records, :limit, :current_cursor, :next_cursor # Creates a new collection. This class is for internal use. # # @param raw_data [Hash] A raw response obtained by Pinterest API. # @param cursor [String] The current cursor. # @param limit [Fixnum] The maximum number of records to obtain from Pinterest API. # @param record_creator [Proc] The code to trasform each raw record in a object. def initialize(raw_data, cursor, limit, &record_creator) raise(ArgumentError, "raw_data must be an Hash.") unless raw_data.is_a?(Hash) record_creator ||= ->(record) { record } @limit = limit @current_cursor = cursor @next_cursor = raw_data["page"]["cursor"] if raw_data["page"] && raw_data["page"]["cursor"] @records = raw_data.fetch("data", []).map(&record_creator) end # Returns a object from the collection. # # @param index [Fixnum] The index to get. def [](index) records[index] end # Returns the size of the collection. # # @return [Fixnum] The size of the collection. def size records.count end alias_method :count, :size alias_method :length, :size # Returns the current page cursor. # # @return [String] The current page cursor. def current_page current_cursor end # Returns the next page cursor. # # @return [String] The next page cursor. def next_page next_cursor end # Checks if the collection is empty. # # @return [Boolean] `true` if the collection is empty, `false` otherwise. def empty? records.empty? end # Checks if the collection has a next page. # # @return [Boolean] `true` if the collection has a next page, `false` otherwise. def next? !next_cursor.nil? end alias_method :next_page?, :next? alias_method :eof?, :next? # Serialize the collection as a Hash that can be serialized as JSON. # # @param options [Hash] The options to use to serialize. # @return [Hash] The serialized collection. def as_json( = {}) { records: records.as_json(), limit: limit, current_cursor: current_cursor, next_cursor: next_cursor } end end |