Sha256: ee7969f3c41a86f705250433d24fe241759709aec264d1d6c6bd788f703da472

Contents?: true

Size: 896 Bytes

Versions: 17

Compression:

Stored size: 896 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] }

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

      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

17 entries across 17 versions & 1 rubygems

Version Path
dato-0.6.9 lib/dato/json_api_deserializer.rb
dato-0.6.8 lib/dato/json_api_deserializer.rb
dato-0.6.7 lib/dato/json_api_deserializer.rb
dato-0.6.6 lib/dato/json_api_deserializer.rb
dato-0.6.5 lib/dato/json_api_deserializer.rb
dato-0.6.3 lib/dato/json_api_deserializer.rb
dato-0.6.3.beta lib/dato/json_api_deserializer.rb
dato-0.6.2 lib/dato/json_api_deserializer.rb
dato-0.6.1 lib/dato/json_api_deserializer.rb
dato-0.6.0 lib/dato/json_api_deserializer.rb
dato-0.5.1 lib/dato/json_api_deserializer.rb
dato-0.5.0 lib/dato/json_api_deserializer.rb
dato-0.4.3 lib/dato/json_api_deserializer.rb
dato-0.4.2 lib/dato/json_api_deserializer.rb
dato-0.4.1 lib/dato/json_api_deserializer.rb
dato-0.4.0 lib/dato/json_api_deserializer.rb
dato-0.4.0.pre lib/dato/json_api_deserializer.rb