Class: JsonApiServer::Error
- Inherits:
-
Object
- Object
- JsonApiServer::Error
- Includes:
- ApiVersion, Serializer
- Defined in:
- lib/json_api_server/error.rb
Overview
Implements a single error based on spec: jsonapi.org/examples/#error-objects.
Serializes to something like this. Skips attributes that are nil. Ignores non-jsonapi attributes.
error.to_json =>
{
":jsonapi": {
":version": "1.0"
},
":errors": {
":id": 1234
":status": "422",
":code": 5,
":source": {
":pointer": "/data/attributes/first-name"
},
":title": "Invalid Attribute",
":detail": "First name must contain at least three characters.",
":meta": {
":attrs": [1,2,3]
},
":links": {
":self": "http://example.com/user"
}
}
}
Class Attribute Summary collapse
-
.error_attrs ⇒ Object
Allowable error attributes.
Instance Attribute Summary collapse
-
#error ⇒ Object
readonly
Returns the value of attribute error.
Instance Method Summary collapse
-
#as_json ⇒ Object
Object that’s serializable to json.
-
#initialize(attrs = {}) ⇒ Error
constructor
A new instance of Error.
Methods included from ApiVersion
Methods included from Serializer
Constructor Details
#initialize(attrs = {}) ⇒ Error
Returns a new instance of Error.
40 41 42 43 44 45 46 |
# File 'lib/json_api_server/error.rb', line 40 def initialize(attrs = {}) @error = if attrs.respond_to?(:keys) h = attrs.select { |k, _v| self.class.error_attrs.include?(k.to_s) } h.empty? ? nil : h end end |
Class Attribute Details
.error_attrs ⇒ Object
Allowable error attributes.
35 36 37 |
# File 'lib/json_api_server/error.rb', line 35 def error_attrs @error_attrs end |
Instance Attribute Details
#error ⇒ Object (readonly)
Returns the value of attribute error.
48 49 50 |
# File 'lib/json_api_server/error.rb', line 48 def error @error end |
Instance Method Details
#as_json ⇒ Object
Object that’s serializable to json.
51 52 53 54 55 56 |
# File 'lib/json_api_server/error.rb', line 51 def as_json { 'jsonapi' => jsonapi, 'errors' => error_as_array } end |