class Pinterest::Collection

A collection of objects, including pagination data.

@attribute records

@return [Pinterest::Entity] A list of objects.

@attribute limit

@return [Fixnum] The maximum number of results to get from Pinterest API.

@attribute current_cursor

@return [String] The cursor to obtain the current page of results.

@attribute next_cursor

@return [String] The cursor to obtain the next page of results.

Attributes

current_cursor[R]
limit[R]
next_cursor[R]
records[R]

Public Class Methods

new(raw_data, cursor, limit, &record_creator) click to toggle source

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.

# File lib/pinterest/collection.rb, line 26
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

Public Instance Methods

[](index) click to toggle source

Returns a object from the collection.

@param index [Fixnum] The index to get.

# File lib/pinterest/collection.rb, line 39
def [](index)
  records[index]
end
as_json(options = {}) click to toggle source

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.

# File lib/pinterest/collection.rb, line 88
def as_json(options = {})
  {
    records: records.as_json(options),
    limit: limit,
    current_cursor: current_cursor,
    next_cursor: next_cursor
  }
end
count()
Alias for: size
current_page() click to toggle source

Returns the current page cursor.

@return [String] The current page cursor.

# File lib/pinterest/collection.rb, line 56
def current_page
  current_cursor
end
empty?() click to toggle source

Checks if the collection is empty.

@return [Boolean] `true` if the collection is empty, `false` otherwise.

# File lib/pinterest/collection.rb, line 70
def empty?
  records.empty?
end
eof?()
Alias for: next?
length()
Alias for: size
next?() click to toggle source

Checks if the collection has a next page.

@return [Boolean] `true` if the collection has a next page, `false` otherwise.

# File lib/pinterest/collection.rb, line 77
def next?
  !next_cursor.nil?
end
Also aliased as: next_page?, eof?
next_page() click to toggle source

Returns the next page cursor.

@return [String] The next page cursor.

# File lib/pinterest/collection.rb, line 63
def next_page
  next_cursor
end
next_page?()
Alias for: next?
size() click to toggle source

Returns the size of the collection.

@return [Fixnum] The size of the collection.

# File lib/pinterest/collection.rb, line 46
def size
  records.count
end
Also aliased as: count, length