Class: FastCqrs::Deserializer::JsonApi

Inherits:
Object
  • Object
show all
Defined in:
lib/fast_cqrs/deserializer/json_api.rb

Overview

Transforms the JSON API request body to a flat hash Input: { data: { id: 1, type: ‘foo’, attributes: { bar: ‘bar’ } } } Output: { id: 1, resource_type: ‘foo’, bar: ‘bar’ }

Constant Summary collapse

IncorrectFormat =
Class.new(StandardError)
HashInputRequired =
Class.new(StandardError)

Instance Method Summary collapse

Instance Method Details

#call(params = { data: {} }) ⇒ Object

Converts the json_api (jsonapi.org) hash resource into a flat hash (defaults to ‘{ data: {} }`)

Parameters:

params

A request body in form of JSON API hash with symbolized keys.

Returns:

A hash with ID and all attributes flat

Raises:



25
26
27
28
29
30
31
32
# File 'lib/fast_cqrs/deserializer/json_api.rb', line 25

def call(params = { data: {} })
  raise HashInputRequired unless params.is_a?(Hash)
  raise IncorrectFormat if (data = params[:data]).blank?

  attrs = data[:attributes] || {}

  attrs.merge(id: data[:id], resource_type: data[:type])
end