module Ballast::Concerns::JSONApi::RequestHandling
A concern to handle JSON API requests.
Constants
- CONTENT_TYPE
The default JSON API Content-Type.
Public Instance Methods
Casts attributes according to the target object definitions.
@param target [Object] The target object. @param attributes [Hash] The attributes to cast. @return [Hash] The casted attributes.
# 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
Extract a model attributes from request data
@param target [Object] The target object. @param type_field [Symbol] The field of the request data which contains the data type. @param attributes_field [Object] The field of the request data which contains the data attributes. @param relationships_field [Object] The field of the request data which contains the data relationships. @return [Hash] The model attributes.
# 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
Adds Cross Origin Request (CORS) headers.
# 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
Returns the current source host.
@return [String] The current source host.
# File lib/ballast/concerns/json_api/request_handling.rb, line 36 def request_source_host @api_source ||= URI.parse(request.url).host end
Returns the valid Content-Type for JSON API requests.
@return [String] The valid Content-Type for JSON API requests.
# File lib/ballast/concerns/json_api/request_handling.rb, line 43 def request_valid_content_type Ballast::Concerns::JSONApi::RequestHandling::CONTENT_TYPE end
Validates a request.
@return [Object] The request data
# 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