Module: JSONAPI::Utils
- Defined in:
- lib/jsonapi/utils.rb,
lib/jsonapi/utils/version.rb
Defined Under Namespace
Modules: Exceptions
Constant Summary collapse
- VERSION =
'0.4.4'
Class Method Summary collapse
Instance Method Summary collapse
- #filter_params ⇒ Object
- #jsonapi_format_errors(exception) ⇒ Object
- #jsonapi_render(json:, status: nil, options: {}) ⇒ Object
- #jsonapi_render_bad_request ⇒ Object
- #jsonapi_render_errors(exception) ⇒ Object
- #jsonapi_render_internal_server_error ⇒ Object
- #jsonapi_render_not_found(exception) ⇒ Object
- #jsonapi_render_not_found_with_null ⇒ Object
- #jsonapi_serialize(records, options = {}) ⇒ Object
- #sort_params ⇒ Object
Class Method Details
.included(base) ⇒ Object
7 8 9 10 11 12 |
# File 'lib/jsonapi/utils.rb', line 7 def self.included(base) if base.respond_to?(:helper_method) base.before_action :setup_request, :check_request base.helper_method :jsonapi_render, :jsonapi_serialize end end |
Instance Method Details
#filter_params ⇒ Object
62 63 64 65 66 67 68 69 |
# File 'lib/jsonapi/utils.rb', line 62 def filter_params @_filter_params ||= if params[:filter].is_a?(Hash) params[:filter].keys.each_with_object({}) do |resource, hash| hash[resource] = params[:filter][resource] end end end |
#jsonapi_format_errors(exception) ⇒ Object
31 32 33 |
# File 'lib/jsonapi/utils.rb', line 31 def jsonapi_format_errors(exception) JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors) end |
#jsonapi_render(json:, status: nil, options: {}) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/jsonapi/utils.rb', line 14 def jsonapi_render(json:, status: nil, options: {}) body = jsonapi_serialize(json, ) render json: body, status: status || @_response_document.status rescue => e handle_exceptions(e) ensure if response.body.size > 0 response.headers['Content-Type'] = JSONAPI::MEDIA_TYPE end end |
#jsonapi_render_bad_request ⇒ Object
39 40 41 |
# File 'lib/jsonapi/utils.rb', line 39 def jsonapi_render_bad_request jsonapi_render_errors(::JSONAPI::Utils::Exceptions::BadRequest.new) end |
#jsonapi_render_errors(exception) ⇒ Object
25 26 27 28 29 |
# File 'lib/jsonapi/utils.rb', line 25 def jsonapi_render_errors(exception) result = jsonapi_format_errors(exception) errors = result.errors render json: { errors: errors }, status: errors.first.status end |
#jsonapi_render_internal_server_error ⇒ Object
35 36 37 |
# File 'lib/jsonapi/utils.rb', line 35 def jsonapi_render_internal_server_error jsonapi_render_errors(::JSONAPI::Utils::Exceptions::InternalServerError.new) end |
#jsonapi_render_not_found(exception) ⇒ Object
43 44 45 46 |
# File 'lib/jsonapi/utils.rb', line 43 def jsonapi_render_not_found(exception) id = exception..match(/=([\w-]+)/).try(:[], 1) || '(no identifier)' jsonapi_render_errors(JSONAPI::Exceptions::RecordNotFound.new(id)) end |
#jsonapi_render_not_found_with_null ⇒ Object
48 49 50 |
# File 'lib/jsonapi/utils.rb', line 48 def jsonapi_render_not_found_with_null render json: { data: nil }, status: 200 end |
#jsonapi_serialize(records, options = {}) ⇒ Object
52 53 54 55 56 57 58 59 60 |
# File 'lib/jsonapi/utils.rb', line 52 def jsonapi_serialize(records, = {}) if records.is_a?(Hash) hash = records.with_indifferent_access records = hash_to_active_record(hash[:data], [:model]) end (params, records) build_response_document(records, ).contents end |
#sort_params ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/jsonapi/utils.rb', line 71 def sort_params @_sort_params ||= if params[:sort].present? params[:sort].split(',').each_with_object({}) do |criteria, hash| order, field = criteria.match(/(\-?)(\w+)/i)[1..2] hash[field] = order == '-' ? :desc : :asc end end end |