Sha256: 19aebdd79f6b09aff0ffa5ffabe3041e265d71d6d5740fa389af53036273540d

Contents?: true

Size: 1.22 KB

Versions: 2

Compression:

Stored size: 1.22 KB

Contents

#
# This file is part of the pinterest-ruby gem. Copyright (C) 2017 and above Shogun <shogun@cowtech.it>.
# Licensed under the MIT license, which can be found at https://choosealicense.com/licenses/mit.
#

module Pinterest
  # Base class for entity objects.
  class Entity
    # Parses a timestamps.
    #
    # @param timestamp [String] The string to parse.
    # @return [DateTime] The parsed timestamp.
    def self.parse_timestamp(timestamp)
      return nil if !timestamp || timestamp.empty?
      DateTime.parse(timestamp + "+00:00")
    end

    # Creates a new object.
    #
    # @param data [Hash] The data of the new object.
    # @return [Pinterest::Board] The new object.
    def initialize(data)
      data.each do |field, value|
        send("#{field}=", value) if respond_to?(field)
      end
    end

    # Serialize the object as a Hash that can be serialized as JSON.
    #
    # @param options [Hash] The options to use to serialize.
    # @return [Hash] The serialized object.
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pinterest-ruby-1.0.2 lib/pinterest/models/entity.rb
pinterest-ruby-1.0.1 lib/pinterest/models/entity.rb