Class: Shamu::JsonApi::Response
- Inherits:
-
BaseBuilder
- Object
- BaseBuilder
- Shamu::JsonApi::Response
- Defined in:
- lib/shamu/json_api/response.rb
Overview
Build a JSON API response from one or more resources.
Instance Method Summary collapse
-
#as_json ⇒ Hash
The compiled resources.
-
#collection(collection, presenter = nil) {|builder, resource| ... } ⇒ self
Output a multiple resources as the response data.
-
#compile ⇒ Hash
The results output as JSON safe hash.
- #error(error = nil) {|builder| ... } ⇒ self
-
#resource(resource, presenter = nil) {|builder| ... } ⇒ self
Output a single resource as the response data.
- #to_json ⇒ String
- #to_s ⇒ String
-
#validation_errors(errors) {|builder, attr, message| ... } ⇒ self
Write ActiveModel validation errors to the response.
Methods inherited from BaseBuilder
Methods included from BuilderMethods::Meta
Methods included from BuilderMethods::Link
Constructor Details
This class inherits a constructor from Shamu::JsonApi::BaseBuilder
Instance Method Details
#as_json ⇒ Hash
Returns the compiled resources.
103 104 105 |
# File 'lib/shamu/json_api/response.rb', line 103 def as_json( * ) compile.as_json end |
#collection(collection, presenter = nil) {|builder, resource| ... } ⇒ self
Output a multiple resources as the response data.
29 30 31 32 33 34 35 |
# File 'lib/shamu/json_api/response.rb', line 29 def collection( collection, presenter = nil, &block ) output[:data] = collection.map do |resource| build_resource resource, presenter, &block end self end |
#compile ⇒ Hash
Returns the results output as JSON safe hash.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/shamu/json_api/response.rb', line 82 def compile @compiled ||= begin compiled = output.dup compiled[:jsonapi] = { version: "1.0" } if = context. compiled[:meta] ||= {} compiled[:meta].reverse_merge!( ) end while context.included_resources? included = ( compiled[ :included ] ||= [] ) context.collect_included_resources.each do |resource, | included << build_resource( resource, [:presenter], &[:block] ) end end compiled end end |
#error(exception) ⇒ self #error {|builder| ... } ⇒ self
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/shamu/json_api/response.rb', line 44 def error( error = nil, &block ) builder = ErrorBuilder.new if error.is_a?( Exception ) builder.exception( error ) elsif error builder.title error end yield builder if block_given? errors = ( output[:errors] ||= [] ) errors << builder.compile self end |
#resource(resource, presenter = nil) {|builder| ... } ⇒ self
Output a single resource as the response data.
15 16 17 18 |
# File 'lib/shamu/json_api/response.rb', line 15 def resource( resource, presenter = nil, &block ) output[:data] = build_resource( resource, presenter, &block ) self end |
#to_json ⇒ String
108 109 110 |
# File 'lib/shamu/json_api/response.rb', line 108 def to_json( * ) compile.to_json end |
#to_s ⇒ String
113 114 115 |
# File 'lib/shamu/json_api/response.rb', line 113 def to_s to_json end |
#validation_errors(errors) {|builder, attr, message| ... } ⇒ self
Write ActiveModel validation errors to the response.
69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/shamu/json_api/response.rb', line 69 def validation_errors( errors, &block ) errors.each do |attr, | error do |builder| path = "/data" path << "/attributes/#{ attr }" unless attr == :base builder.pointer path yield builder, attr, if block_given? end end end |