Sha256: be88f2c0be2644a70f050f894b5e211bc877a0d0553c368e62b5efb946cdfafc

Contents?: true

Size: 879 Bytes

Versions: 1

Compression:

Stored size: 879 Bytes

Contents

# frozen_string_literal: true

module Dato
  class JsonApiDeserializer
    def deserialize(data)
      data = data[:data]

      if data.is_a? Array
        data.map { |resource| deserialize_resource(resource) }
      else
        deserialize_resource(data)
      end
    end

    def deserialize_resource(data)
      result = { id: data[:id] }

      result.merge!(data[:attributes]) if data[:attributes]

      relationships = data.delete(:relationships)

      if relationships
        relationships.each do |key, handle|
          handle_data = handle['data']
          value = if handle_data.is_a? Array
                    handle_data.map { |ref| ref['id'] }
                  elsif handle_data.is_a? Hash
                    handle_data[:id]
                  end
          result[key] = value
        end
      end

      result.with_indifferent_access
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dato-0.6.10 lib/dato/json_api_deserializer.rb