Module: JsonAPI
- Defined in:
- lib/jbuilder/json_api.rb,
lib/jbuilder/json_api/version.rb
Constant Summary collapse
- VERSION =
'1.0.0'
Instance Method Summary collapse
-
#api_format!(resources = nil, errors = nil, meta = nil, options = {}) ⇒ Object
Returns a valid-formatted JSON which follows JSON-API specifications: jsonapi.org/.
Instance Method Details
#api_format!(resources = nil, errors = nil, meta = nil, options = {}) ⇒ Object
Returns a valid-formatted JSON which follows JSON-API specifications: jsonapi.org/
Arguments
-
resources- list of resources to render (may be even one or nil); -
errors- array of hashes in the below format:[{ status: 422, detail: 'This error occurs because...' }, {...}] -
meta- a hash representing any meta (additional) information.
Options
Any information can be passed as options argument; resources’ class methods json_api_attrs, json_api_relations and json_api_meta will be invoked with this argument.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 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 |
# File 'lib/jbuilder/json_api.rb', line 19 def api_format! (resources = nil, errors = nil, = nil, = {}) begin # Firstly, print meta # http://jsonapi.org/format/#document-meta # if && !.empty? end # Secondly, take care of errors. If there are any, # no 'data' section should be represented. # http://jsonapi.org/format/#document-top-level # # Read more at # http://jsonapi.org/format/#errors # if errors && !errors.empty? ignore_nil! true errors errors do |error| id error[:id] status error[:status] detail error[:detail] code error[:code] title error[:title] if error[:source] source do pointer error[:source][:pointer] paramater error[:source][:parameter] end end if error[:links] links do about error[:links][:about] end end end return self end resources = [*resources] # http://jsonapi.org/format/#document-links # if @context links do set! 'self', @context.request.path end end data do resources.blank? ? array! : _api_resource_objects(resources, ) end included = [] resources.each do |resource| next unless resource.respond_to?'json_api_relations' resource.json_api_relations().each do |relationship| included += [*(resource.send(relationship))] end end included.uniq! included do _api_resource_objects(included, , resources) unless included.blank? end self rescue Exception => e @attributes = {} = Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('2.3.0') ? e. : e. return api_format! nil, [{ status: 500, title: e.class.to_s, detail: }] end end |