Module: Ballast::Concerns::JSONApi::RequestHandling

Defined in:
lib/ballast/concerns/json_api/request_handling.rb

Overview

A concern to handle JSON API requests.

Constant Summary collapse

CONTENT_TYPE =

The default JSON API Content-Type.

"application/vnd.api+json".freeze

Instance Method Summary collapse

Instance Method Details

#request_cast_attributes(target, attributes) ⇒ Hash

Casts attributes according to the target object definitions.

Parameters:

  • target (Object)

    The target object.

  • attributes (Hash)

    The attributes to cast.

Returns:

  • (Hash)

    The casted attributes.



76
77
78
79
80
81
82
83
84
# File 'lib/ballast/concerns/json_api/request_handling.rb', line 76

def request_cast_attributes(target, attributes)
  types = target.class.column_types

  attributes.each do |k, v|
    request_cast_attribute(target, attributes, types, k, v)
  end

  attributes
end

#request_extract_model(target, type_field: :type, attributes_field: :attributes, relationships_field: :relationships) ⇒ Hash

Extract a model attributes from request data

Parameters:

  • target (Object)

    The target object.

  • type_field (Symbol) (defaults to: :type)

    The field of the request data which contains the data type.

  • attributes_field (Object) (defaults to: :attributes)

    The field of the request data which contains the data attributes.

  • relationships_field (Object) (defaults to: :relationships)

    The field of the request data which contains the data relationships.

Returns:

  • (Hash)

    The model attributes.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ballast/concerns/json_api/request_handling.rb', line 54

def request_extract_model(target, type_field: :type, attributes_field: :attributes, relationships_field: :relationships)
  data = params[:data]

  request_validate_model_type(target, data, type_field)

  data = data[attributes_field]
  fail_request!(:bad_request, "Missing attributes in the \"attributes\" field.") if data.blank?

  # Extract attributes using strong parameters
  data = unembed_relationships(validate_attributes(data, target), target, relationships_field)

  # Extract relationships
  data.merge!(validate_relationships(params[:data], target, relationships_field))

  data
end

#request_handle_corsObject

Adds Cross Origin Request (CORS) headers.



11
12
13
14
15
16
# File 'lib/ballast/concerns/json_api/request_handling.rb', line 11

def request_handle_cors
  headers["Access-Control-Allow-Origin"] = Rails.env.development? ? "http://#{request_source_host}:4200" : Rails.application.secrets.cors_source
  headers["Access-Control-Allow-Methods"] = "POST, GET, PUT, DELETE, OPTIONS"
  headers["Access-Control-Allow-Headers"] = "Content-Type, X-User-Email, X-User-Token"
  headers["Access-Control-Max-Age"] = 1.year.to_i.to_s
end

#request_source_hostString

Returns the current source host.

Returns:

  • (String)

    The current source host.



36
37
38
# File 'lib/ballast/concerns/json_api/request_handling.rb', line 36

def request_source_host
  @api_source ||= URI.parse(request.url).host
end

#request_valid_content_typeString

Returns the valid Content-Type for JSON API requests.

Returns:

  • (String)

    The valid Content-Type for JSON API requests.



43
44
45
# File 'lib/ballast/concerns/json_api/request_handling.rb', line 43

def request_valid_content_type
  Ballast::Concerns::JSONApi::RequestHandling::CONTENT_TYPE
end

#request_validateObject

Validates a request.

Returns:

  • (Object)

    The request data



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ballast/concerns/json_api/request_handling.rb', line 21

def request_validate
  content_type = request_valid_content_type
  request.format = :json
  response.content_type = content_type unless Rails.env.development? && params["json"]

  @cursor = PaginationCursor.new(params, :page)

  params[:data] ||= HashWithIndifferentAccess.new

  validate_data(content_type)
end