module Apes::Concerns::Request

JSON API request handling module.

Constants

CONTENT_TYPE

Valid JSON API content type

Public Instance Methods

request_cast_attributes(target, attributes) click to toggle source

Converts attributes for a target model in the desired types.

@param target [Object] The target model. This is use to obtain types. @param attributes [HashWithIndifferentAccess] The attributes to convert. @return [HashWithIndifferentAccess] The converted attributes.

# 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
request_extract_model(target, type_field: :type, attributes_field: :attributes, relationships_field: :relationships) click to toggle source

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

@param target [Object] The target model. This is use to obtain validations. @param type_field [Symbol] The attribute which contains input type. @param attributes_field [Symbol] The attribute which contains input attributes. @param relationships_field [Symbol] The attribute which contains relationships specifications. @return [HashWithIndifferentAccess] The attributes to create or update a target model.

# 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
request_handle_cors() click to toggle source

Sets headers for CORS handling.

# 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
request_source_host() click to toggle source

Returns the hostname of the client.

@return The hostname of the client.

# File lib/apes/concerns/request.rb, line 37
def request_source_host
  @api_source ||= URI.parse(request.url).host
end
request_valid_content_type() click to toggle source

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

@return [String] valid content type for a JSON API request.

# File lib/apes/concerns/request.rb, line 44
def request_valid_content_type
  Apes::Concerns::Request::CONTENT_TYPE
end
request_validate() click to toggle source

Validates a request according to JSON API.

# 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