Class: ErrorNormalizer::Error
- Inherits:
-
Object
- Object
- ErrorNormalizer::Error
- Defined in:
- lib/error_normalizer/error.rb
Overview
Error struct which makes cosmetic normalization upon calling either #to_hash and #to_json. Supports case equality check (#===) for hash structs.
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(error_key, message: nil, type: 'params', **payload) ⇒ Error
constructor
A new instance of Error.
- #to_hash ⇒ Object
- #to_json ⇒ Object
Constructor Details
#initialize(error_key, message: nil, type: 'params', **payload) ⇒ Error
Returns a new instance of Error.
25 26 27 28 29 30 |
# File 'lib/error_normalizer/error.rb', line 25 def initialize(error_key, message: nil, type: 'params', **payload) @key = error_key @message = @type = type @payload = payload end |
Class Method Details
.===(other) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/error_normalizer/error.rb', line 32 def self.===(other) return true if other.is_a?(Error) return false unless other.is_a?(Hash) h = other.transform_keys(&:to_s) h.key?('key') & h.key?('message') && h.key?('payload') && h.key?('type') end |
Instance Method Details
#to_hash ⇒ Object
40 41 42 43 44 45 46 47 |
# File 'lib/error_normalizer/error.rb', line 40 def to_hash { key: @key, message: , payload: payload, type: @type } end |
#to_json ⇒ Object
49 50 51 |
# File 'lib/error_normalizer/error.rb', line 49 def to_json to_hash.to_json end |