Class: RapidApi::SerializerAdapters::AmsAdapter

Inherits:
Abstract
  • Object
show all
Defined in:
lib/rapid_api/serializer_adapters/ams_adapter.rb

Instance Attribute Summary

Attributes inherited from Abstract

#klass, #root_key

Instance Method Summary collapse

Methods inherited from Abstract

#initialize

Constructor Details

This class inherits a constructor from RapidApi::SerializerAdapters::Abstract

Instance Method Details

#deserialize_attributes(params, root_key) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rapid_api/serializer_adapters/ams_adapter.rb', line 26

def deserialize_attributes(params, root_key)
  #TODO: test attributes deserialization
  if ActiveModelSerializers.config.adapter == :attributes
    params.require(root_key)
  else
    attributes = params
    if params[:data][:attributes].present?
      attributes = params.require(:data).require(:attributes)
    end
    if params[:data][:relationships].present?
      relationships = params.require(:data).require(:relationships)
      relationships.keys.each do |attribute|
       attributes["#{attribute}_id"] = relationships[attribute][:data].try(:[], :id)
      end
    end
    attributes
  end
end

#deserialize_id(params, root_key) ⇒ Object



45
46
47
# File 'lib/rapid_api/serializer_adapters/ams_adapter.rb', line 45

def deserialize_id(params, root_key)
  params.require(:id)
end

#serialize(member) ⇒ Object



5
6
7
8
# File 'lib/rapid_api/serializer_adapters/ams_adapter.rb', line 5

def serialize(member)
  serializer = klass.new(member)
  ActiveModelSerializers::Adapter.create(serializer).to_json
end

#serialize_collection(collection) ⇒ Object



10
11
12
13
14
15
# File 'lib/rapid_api/serializer_adapters/ams_adapter.rb', line 10

def serialize_collection(collection)
  collection_serializer = ActiveModel::Serializer::CollectionSerializer.new collection, {
                       serializer: klass
                     }
  ActiveModelSerializers::Adapter.create(collection_serializer).to_json
end

#serialize_errors(query_result) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/rapid_api/serializer_adapters/ams_adapter.rb', line 17

def serialize_errors(query_result)
  serializer = ActiveModel::Serializer::ErrorSerializer.new(query_result)
  if ActiveModelSerializers.config.adapter == :attributes
    { errors: serializer.as_json }
  else
    ActiveModelSerializers::Adapter.create(serializer).to_json
  end
end