Class: SimpleJsonapi::Rails::ActionController::JsonapiHelper
- Inherits:
-
Object
- Object
- SimpleJsonapi::Rails::ActionController::JsonapiHelper
- Defined in:
- lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#pointers ⇒ Object
readonly
Returns the value of attribute pointers.
-
#request_validator ⇒ Object
readonly
Returns the value of attribute request_validator.
Instance Method Summary collapse
-
#deserialize(jsonapi_data) ⇒ Object
def url_helpers ::Rails.application.routes.url_helpers end.
- #fields_params ⇒ Object
- #filter_param(param_name) ⇒ Object
- #filter_param_list(param_name) ⇒ Object
- #include_params ⇒ Object
-
#initialize(controller) ⇒ JsonapiHelper
constructor
A new instance of JsonapiHelper.
- #render_bad_request(message) ⇒ Object
- #render_model_errors(model) ⇒ Object
- #render_record_not_found(error) ⇒ Object
- #sort_related_params ⇒ Object
- #validate_jsonapi_request_body ⇒ Object
- #validate_jsonapi_request_headers ⇒ Object
Constructor Details
#initialize(controller) ⇒ JsonapiHelper
Returns a new instance of JsonapiHelper.
11 12 13 14 15 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 11 def initialize(controller) @controller = controller @pointers = {} @request_validator = RequestValidator.new(request, params) end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
7 8 9 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 7 def controller @controller end |
#pointers ⇒ Object (readonly)
Returns the value of attribute pointers.
7 8 9 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 7 def pointers @pointers end |
#request_validator ⇒ Object (readonly)
Returns the value of attribute request_validator.
7 8 9 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 7 def request_validator @request_validator end |
Instance Method Details
#deserialize(jsonapi_data) ⇒ Object
def url_helpers
::Rails.application.routes.url_helpers
end
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 61 def deserialize(jsonapi_data) jsonapi_hash = case jsonapi_data when String then JSON.parse(jsonapi_data).deep_symbolize_keys when Hash then jsonapi_data.deep_symbolize_keys else jsonapi_data end data = jsonapi_hash[:data] return unless data result = {} pointers = {} result[:type] = data[:type] result[:id] = data[:id] pointers[:type] = "/data/type" pointers[:id] = "/data/id" if data[:attributes].present? data[:attributes].each do |name, value| result[name] = value pointers[name] = "/data/attributes/#{name}" end end if data[:relationships].present? data[:relationships].each do |name, value| = value[:data] if .is_a?(Array) singular_name = name.to_s.singularize result[:"#{singular_name}_types"] = .pluck(:type) result[:"#{singular_name}_ids"] = .pluck(:id) pointers[:"#{singular_name}_types"] = "/data/relationships/#{name}" pointers[:"#{name}_ids"] = "/data/relationships/#{name}" elsif .is_a?(Hash) result[:"#{name}_type"] = [:type] result[:"#{name}_id"] = [:id] pointers[:"#{name}_type"] = "/data/relationships/#{name}" pointers[:"#{name}_id"] = "/data/relationships/#{name}" end end end @pointers = pointers result end |
#fields_params ⇒ Object
21 22 23 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 21 def fields_params (params[:fields] || {}).transform_values { |f| f.split(/,/) } end |
#filter_param(param_name) ⇒ Object
25 26 27 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 25 def filter_param(param_name) (params[:filter] || {})[param_name] end |
#filter_param_list(param_name) ⇒ Object
29 30 31 32 33 34 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 29 def filter_param_list(param_name) param_value = filter_param(param_name) return nil unless param_value param_value.split(/,/) end |
#include_params ⇒ Object
17 18 19 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 17 def include_params params[:include].to_s.split(/,/).presence end |
#render_bad_request(message) ⇒ Object
50 51 52 53 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 50 def render_bad_request() error = SimpleJsonapi::Errors::BadRequest.new(detail: ) render jsonapi_errors: [error], status: :bad_request end |
#render_model_errors(model) ⇒ Object
45 46 47 48 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 45 def render_model_errors(model) errors = SimpleJsonapi::Errors::ActiveModelError.from_errors(model.errors, pointers) render jsonapi_errors: errors, status: :unprocessable_entity end |
#render_record_not_found(error) ⇒ Object
41 42 43 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 41 def render_record_not_found(error) render jsonapi_errors: error, status: :not_found end |
#sort_related_params ⇒ Object
36 37 38 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 36 def (params[:sort_related] || {}).transform_values { |f| f.split(/,/) } end |
#validate_jsonapi_request_body ⇒ Object
114 115 116 117 118 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 114 def validate_jsonapi_request_body unless request_validator.valid_request_body? raise InvalidJsonStructureError, "Not a valid jsonapi request body" end end |
#validate_jsonapi_request_headers ⇒ Object
109 110 111 112 |
# File 'lib/simple_jsonapi/rails/action_controller/jsonapi_helper.rb', line 109 def validate_jsonapi_request_headers return head :unsupported_media_type unless request_validator.valid_content_type_header? head :not_acceptable unless request_validator.valid_accept_header? end |