Class: Pinterest::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/pinterest/collection.rb

Overview

A collection of objects, including pagination data.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_data, cursor, limit, &record_creator) ⇒ Collection

Creates a new collection. This class is for internal use.

Parameters:

  • raw_data (Hash)

    A raw response obtained by Pinterest API.

  • cursor (String)

    The current cursor.

  • limit (Fixnum)

    The maximum number of records to obtain from Pinterest API.

  • record_creator (Proc)

    The code to trasform each raw record in a object.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
# 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

Instance Attribute Details

#current_cursorString

Returns The cursor to obtain the current page of results.

Returns:

  • (String)

    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(options = {})
    {
      records: records.as_json(options),
      limit: limit,
      current_cursor: current_cursor,
      next_cursor: next_cursor
    }
  end
end

#limitFixnum

Returns The maximum number of results to get from Pinterest API.

Returns:

  • (Fixnum)

    The maximum number of results to get from Pinterest API.



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(options = {})
    {
      records: records.as_json(options),
      limit: limit,
      current_cursor: current_cursor,
      next_cursor: next_cursor
    }
  end
end

#next_cursorString

Returns The cursor to obtain the next page of results.

Returns:

  • (String)

    The cursor to obtain the next 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(options = {})
    {
      records: records.as_json(options),
      limit: limit,
      current_cursor: current_cursor,
      next_cursor: next_cursor
    }
  end
end

#recordsPinterest::Entity

Returns A list of objects.

Returns:



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(options = {})
    {
      records: records.as_json(options),
      limit: limit,
      current_cursor: current_cursor,
      next_cursor: next_cursor
    }
  end
end

Instance Method Details

#[](index) ⇒ Object

Returns a object from the collection.

Parameters:

  • index (Fixnum)

    The index to get.



39
40
41
# File 'lib/pinterest/collection.rb', line 39

def [](index)
  records[index]
end

#as_json(options = {}) ⇒ Hash

Serialize the collection as a Hash that can be serialized as JSON.

Parameters:

  • options (Hash) (defaults to: {})

    The options to use to serialize.

Returns:

  • (Hash)

    The serialized collection.



88
89
90
91
92
93
94
95
# 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

#current_pageString

Returns the current page cursor.

Returns:

  • (String)

    The current page cursor.



56
57
58
# File 'lib/pinterest/collection.rb', line 56

def current_page
  current_cursor
end

#empty?Boolean

Checks if the collection is empty.

Returns:

  • (Boolean)

    true if the collection is empty, false otherwise.



70
71
72
# File 'lib/pinterest/collection.rb', line 70

def empty?
  records.empty?
end

#next?Boolean Also known as: next_page?, eof?

Checks if the collection has a next page.

Returns:

  • (Boolean)

    true if the collection has a next page, false otherwise.



77
78
79
# File 'lib/pinterest/collection.rb', line 77

def next?
  !next_cursor.nil?
end

#next_pageString

Returns the next page cursor.

Returns:

  • (String)

    The next page cursor.



63
64
65
# File 'lib/pinterest/collection.rb', line 63

def next_page
  next_cursor
end

#sizeFixnum Also known as: count, length

Returns the size of the collection.

Returns:

  • (Fixnum)

    The size of the collection.



46
47
48
# File 'lib/pinterest/collection.rb', line 46

def size
  records.count
end