Module: Apes::Concerns::Request

Included in:
Apes::Controller
Defined in:
lib/apes/concerns/request.rb

Overview

JSON API request handling module.

Constant Summary

CONTENT_TYPE =

Valid JSON API content type

"application/vnd.api+json".freeze

Instance Method Summary (collapse)

Instance Method Details

- (HashWithIndifferentAccess) request_cast_attributes(target, attributes)

Converts attributes for a target model in the desired types.

Parameters:

  • target (Object)

    The target model. This is use to obtain types.

  • attributes (HashWithIndifferentAccess)

    The attributes to convert.

Returns:

  • (HashWithIndifferentAccess)

    The converted attributes.



77
78
79
80
81
82
83
84
85
# File 'lib/apes/concerns/request.rb', line 77

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

- (HashWithIndifferentAccess) request_extract_model(target, type_field: :type, attributes_field: :attributes, relationships_field: :relationships)

Extract all attributes from input data making they are all valid and present.

Parameters:

  • target (Object)

    The target model. This is use to obtain validations.

  • type_field (Symbol)

    The attribute which contains input type.

  • attributes_field (Symbol)

    The attribute which contains input attributes.

  • relationships_field (Symbol)

    The attribute which contains relationships specifications.

Returns:

  • (HashWithIndifferentAccess)

    The attributes to create or update a target model.



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/apes/concerns/request.rb', line 55

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

- (Object) request_handle_cors

Sets headers for CORS handling.



14
15
16
17
18
19
# File 'lib/apes/concerns/request.rb', line 14

def request_handle_cors
  headers["Access-Control-Allow-Origin"] = request.headers["Origin"] || Apes::RuntimeConfiguration.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

- (Object) request_source_host

Returns the hostname of the client.

Returns:

  • The hostname of the client.



37
38
39
# File 'lib/apes/concerns/request.rb', line 37

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

- (String) request_valid_content_type

Returns the valid content type for a non GET JSON API request.

Returns:

  • (String)

    valid content type for a JSON API request.



44
45
46
# File 'lib/apes/concerns/request.rb', line 44

def request_valid_content_type
  Apes::Concerns::Request::CONTENT_TYPE
end

- (Object) request_validate

Validates a request according to JSON API.



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/apes/concerns/request.rb', line 22

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

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

  params[:data] ||= HashWithIndifferentAccess.new

  validate_data(content_type)
end