class JSONApi::ObjectSerializer

Public Instance Methods

hashify(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 11
def hashify(object, **options)
  hash = { data: data_for(object, **options) }

  if options[:include].is_a?(Array)
    hash[:included] = options[:include]
  end

  if options[:meta].is_a?(Hash)
    hash[:meta] = options[:meta]
  end

  hash
end
serialize(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 7
def serialize(object, **options)
  ActiveSupport::JSON.encode(hashify(object, **options))
end

Private Instance Methods

attributes_for(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 80
def attributes_for(object, **options)
  options[:attributes].each_with_object({}) do |attribute, hash|
    attribute_key = Utils.canonicalize_attribute_name(attribute)
    hash[attribute_key] = object.send(attribute)
  end
end
canonicalize_id(id) click to toggle source
# File lib/json_api/object_serializer.rb, line 104
def canonicalize_id(id)
  Utils.canonicalize_id(id)
end
canonicalize_type_name(type_name) click to toggle source
# File lib/json_api/object_serializer.rb, line 108
def canonicalize_type_name(type_name)
  Utils.canonicalize_type_name(type_name)
end
data_for(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 27
def data_for(object, **options)
  if object.is_a? Array
    object.map { |o| resource_object_for(o, options) }
  else
    resource_object_for(object, options)
  end
end
id_for(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 94
def id_for(object, **options)
  id_attribute = options[:id_attribute] || :id
  canonicalize_id(object.send(id_attribute))
end
relationships_for(object, options) click to toggle source
# File lib/json_api/object_serializer.rb, line 65
def relationships_for(object, options)
  relationship_serializer = RelationshipSerializer.new

  defaults = {
    parent_type: type_for(object, options),
    base_url:    options[:base_url]
  }

  options[:relationships].each_with_object({}) do |relationship, hash|
    relationship_key = Utils.canonicalize_attribute_name(relationship[:name])
    data = relationship_serializer.as_json(object, defaults.merge(relationship))
    hash[relationship_key] = data unless data.nil?
  end
end
resource_identifier_for(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 53
def resource_identifier_for(object, **options)
  resource_identifier = {
    type: type_for(object, options)
  }

  unless options[:new_record] == true
    resource_identifier[:id] = id_for(object, options)
  end

  resource_identifier
end
resource_object_for(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 35
def resource_object_for(object, **options)
  result = resource_identifier_for(object, options)

  if options[:attributes] && options[:attributes].any?
    result[:attributes] = attributes_for(object, options)
  end

  if options[:relationships] && options[:relationships].any?
    result[:relationships] = relationships_for(object, options)
  end

  if options[:links] == true
    result[:links] = links_for(object, options)
  end

  result
end
type_for(object, **options) click to toggle source
# File lib/json_api/object_serializer.rb, line 99
def type_for(object, **options)
  type_name = options[:type] || object.class.name
  canonicalize_type_name(type_name)
end