Module: JsonapiUtils
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/jsonapi_utils.rb,
lib/jsonapi_utils/version.rb
Constant Summary collapse
- VERSION =
"0.1.0"
Instance Method Summary collapse
- #jsonapi_error(exception) ⇒ Object
- #jsonapi_render(options) ⇒ Object
- #jsonapi_serialize(records, options = {}) ⇒ Object
Instance Method Details
#jsonapi_error(exception) ⇒ Object
33 34 35 |
# File 'lib/jsonapi_utils.rb', line 33 def jsonapi_error(exception) JSONAPI::ErrorsOperationResult.new(exception.errors[0].code, exception.errors).as_json end |
#jsonapi_render(options) ⇒ Object
10 11 12 13 14 15 |
# File 'lib/jsonapi_utils.rb', line 10 def jsonapi_render() if .has_key?(:json) response_body = jsonapi_serialize([:json], [:options] || {}) render json: response_body, status: ([:status] || :ok) end end |
#jsonapi_serialize(records, options = {}) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jsonapi_utils.rb', line 17 def jsonapi_serialize(records, = {}) (params, records) results = JSONAPI::OperationResults.new if records.respond_to?(:to_ary) records = fix_when_hash(records, ) if records.all? { |e| e.is_a?(Hash) } @resources = build_collection(records) results.add_result(JSONAPI::ResourcesOperationResult.new(:ok, @resources, ())) else @resource = turn_into_resource(records) results.add_result(JSONAPI::ResourceOperationResult.new(:ok, @resource)) end create_response_document(results).contents end |