module Served::Serializers::JsonApi

JSON API serializing

Public Class Methods

dump(_resource, attributes) click to toggle source
# File lib/served/serializers/json_api.rb, line 46
def self.dump(_resource, attributes)
  attributes.to_json
end
error_message(error) click to toggle source

Fetches the error message from either detail or title if both are nil a custom message is returned

# File lib/served/serializers/json_api.rb, line 35
def self.error_message(error)
  error.detail || error.title || 'Error, but no error message found'
end
load(_resource, response) click to toggle source
# File lib/served/serializers/json_api.rb, line 7
def self.load(_resource, response)
  if (200..299).cover?(response.code)
    data = JSON.parse(response.body)['data']
    included = JSON.parse(response.body)['included']
    if data.is_a?(Array)
      data.map { |d| normalize_and_restructure(d, included) }
    else
      normalize_and_restructure(data, included)
    end
  else
    Served::Serializers::JsonApi::Errors.new(response)
  end
end
merge_relationships(restructured, data, included) click to toggle source
# File lib/served/serializers/json_api.rb, line 70
def self.merge_relationships(restructured, data, included)
  data['relationships'].each_key do |relationship|
    rel = data['relationships'][relationship]
    next unless rel && rel['data']
    rel_data = rel['data']

    relationship_attributes = if rel_data.is_a?(Array)
                                rel_data.inject([]) do |ary, r|
                                  ary << restructure_relationship(r, included)
                                end
                              else
                                restructure_relationship(rel_data, included)
                              end
    restructured.merge!(relationship => relationship_attributes)
  end
  restructured
end
normalize_and_restructure(data, included) click to toggle source
# File lib/served/serializers/json_api.rb, line 39
def self.normalize_and_restructure(data, included)
  data = normalize_keys(data)
  attributes = restructure_json(data)
  merge_relationships(attributes, data, included) if data['relationships']
  attributes
end
normalize_keys(params) click to toggle source
# File lib/served/serializers/json_api.rb, line 50
def self.normalize_keys(params)
  case params
  when Hash
    Hash[params.map { |k, v| [k.to_s.tr('-', '_'), normalize_keys(v)] }]
  when Array
    params.map { |v| normalize_keys(v) }
  else
    params
  end
end
parse_errors(result, resource) click to toggle source
# File lib/served/serializers/json_api.rb, line 21
def self.parse_errors(result, resource)
  result.each do |error|
    if error.source_parameter &&
        resource.attributes.keys.include?(error.source_parameter.to_sym)
      resource.errors.add(error.source_parameter.to_sym, error_message(error))
    else
      resource.errors.add(:base, error_message(error))
    end
  end
  resource
end
restructure_json(data) click to toggle source

Restructure JSON API structure into parseable hash

# File lib/served/serializers/json_api.rb, line 89
def self.restructure_json(data)
  data['attributes'].merge('id' => data['id'])
end
restructure_relationship(resource, included) click to toggle source
# File lib/served/serializers/json_api.rb, line 93
def self.restructure_relationship(resource, included)
  relationship = included.find do |r|
    resource['id'] == r['id'] && resource['type'] == r['type']
  end

  relationship['attributes'].merge('id' => resource['id']) if relationship
end
serialize_individual_error(error) click to toggle source
# File lib/served/serializers/json_api.rb, line 61
def self.serialize_individual_error(error)
  {
    json_api: error[:title],
    exception: error[:detail],
    backtrace: error[:source],
    code: error[:code]
  }
end