Class: JsendWrapper::ErrorRenderer
- Defined in:
- lib/jsend_wrapper/renderers/error_renderer.rb
Overview
Wraps the given message in a JSend Error. JSend Errors have two required elements (status, message) and two optional elements (code, data).
Instance Attribute Summary collapse
-
#code ⇒ Object
readonly
Returns the value of attribute code.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#has_code ⇒ Object
(also: #code?)
readonly
Returns the value of attribute has_code.
-
#has_data ⇒ Object
(also: #data?)
readonly
Returns the value of attribute has_data.
-
#message ⇒ Object
readonly
Returns the value of attribute message.
Instance Method Summary collapse
-
#initialize(message, optional = {}) ⇒ ErrorRenderer
constructor
A new instance of ErrorRenderer.
- #to_h ⇒ Object
-
#to_s ⇒ String
The rendered JSON.
Constructor Details
#initialize(message, optional = {}) ⇒ ErrorRenderer
Returns a new instance of ErrorRenderer.
32 33 34 35 36 37 38 39 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 32 def initialize(, optional={}) @message = .to_s @has_code = optional.key? :code @has_data = optional.key? :data @code = parse_code optional[:code] if code? @data = optional[:data] if data? end |
Instance Attribute Details
#code ⇒ Object (readonly)
Returns the value of attribute code.
22 23 24 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 22 def code @code end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 22 def data @data end |
#has_code ⇒ Object (readonly) Also known as: code?
Returns the value of attribute has_code.
22 23 24 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 22 def has_code @has_code end |
#has_data ⇒ Object (readonly) Also known as: data?
Returns the value of attribute has_data.
22 23 24 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 22 def has_data @has_data end |
#message ⇒ Object (readonly)
Returns the value of attribute message.
22 23 24 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 22 def @message end |
Instance Method Details
#to_h ⇒ Object
47 48 49 50 51 52 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 47 def to_h {status: 'error', message: }.tap do |hash| hash[:code] = code if code? hash[:data] = data if data? end end |
#to_s ⇒ String
Returns the rendered JSON.
43 44 45 |
# File 'lib/jsend_wrapper/renderers/error_renderer.rb', line 43 def to_s %[{"status":"error","message":#{.inspect}#{optional}}] end |