Sha256: 947d327d16f269e67683eeb2d9d8968dff2a51d5b7d4a695270c26e04cce5a95

Contents?: true

Size: 928 Bytes

Versions: 1

Compression:

Stored size: 928 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[:meta] = data[:meta] if data[:meta]
      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.11 lib/dato/json_api_deserializer.rb