Class: Pinterest::Entity

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

Overview

Base class for entity objects.

Direct Known Subclasses

Board, Interest, Pin, User

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data) ⇒ Pinterest::Board

Creates a new object.

Parameters:

  • data (Hash)

    The data of the new object.



22
23
24
25
26
# File 'lib/pinterest/models/entity.rb', line 22

def initialize(data)
  data.each do |field, value|
    send("#{field}=", value) if respond_to?(field)
  end
end

Class Method Details

.parse_timestamp(timestamp) ⇒ DateTime

Parses a timestamps.

Parameters:

  • timestamp (String)

    The string to parse.

Returns:

  • (DateTime)

    The parsed timestamp.



13
14
15
16
# File 'lib/pinterest/models/entity.rb', line 13

def self.parse_timestamp(timestamp)
  return nil if !timestamp || timestamp.empty?
  DateTime.parse(timestamp + "+00:00")
end

Instance Method Details

#as_json(fields, options = {}) ⇒ Hash

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

Parameters:

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

    The options to use to serialize.

Returns:

  • (Hash)

    The serialized object.



32
33
34
35
36
37
38
39
40
# File 'lib/pinterest/models/entity.rb', line 32

def as_json(fields, options = {})
  fields.reduce({}) do |accu, field|
    value = send(field)
    value = value.as_json(options) if value.respond_to?(:as_json)

    accu[field.to_sym] = value
    accu
  end
end