Module: JsonAPI
- Defined in:
- lib/jbuilder/json_api.rb,
lib/jbuilder/json_api/version.rb
Constant Summary collapse
- VERSION =
'0.0.1'
Instance Method Summary collapse
-
#api_format!(resources = nil, errors = nil, options = {}) ⇒ Object
Returns a valid-formatted JSON which follows JSON-API specifications: jsonapi.org/.
Instance Method Details
#api_format!(resources = nil, errors = 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 errors in below format:
[{ status: 422, detail: 'This error occurs because...' }, {...}]
Options:
:access_level: - access level, e.g. nil, :user, :admin
:meta: - a hash representing (additional) information
17 18 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 |
# File 'lib/jbuilder/json_api.rb', line 17 def api_format! (resources = nil, errors = nil, = {}) .merge access_level: nil .merge meta: nil # Firstly, print meta # http://jsonapi.org/format/#document-meta # if [:meta] && ![:meta].empty? [:meta] 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! (@ignore_nil.nil? ? true : @ignore_nil) errors errors do |error| id error[:id] status error[:status] detail error[:detail] code error[:code] title error[:title] source do pointer error[:pointer] paramater error[:parameter] end links do about error[:about] end end return self end resources = ::Kernel::Array resources # http://jsonapi.org/format/#document-links # links do begin set! 'self', @context.request.path rescue # No @context given, cannot find path end end data do resources.blank? ? array! : _api_resource_objects(resources, [:access_level]) end included = [] resources.each do |resource| next unless resource.respond_to?'json_api_relations' resource.json_api_relations([:access_level]).each do |relationship| included += ::Kernel::Array(resource.send(relationship)) end end included.uniq! included do _api_resource_objects(included, [:access_level], resources) unless included.blank? end self end |