Class: FulfilApi::Resource::Errors
- Inherits:
-
Object
- Object
- FulfilApi::Resource::Errors
- Includes:
- Enumerable
- Defined in:
- lib/fulfil_api/resource/errors.rb
Overview
The Errors class provides a structure to track and manage errors related to a API resource.
Instance Method Summary collapse
-
#add(code:, message:, type:) ⇒ Array<Hash>
Adds a new error to the collection, unless the same error already exists.
-
#added?(code:, type:) ⇒ Boolean
Checks if an error with the specified code and type has already been added.
-
#clear ⇒ Array
Clears all errors from the collection.
-
#full_messages ⇒ Array<String>
Returns an array of the full error messages (just the message field).
-
#initialize(resource_klass) ⇒ Errors
constructor
A new instance of Errors.
-
#messages ⇒ Array<Hash>
Returns the collection of error messages as an array of hashes.
Constructor Details
#initialize(resource_klass) ⇒ Errors
Returns a new instance of Errors.
12 13 14 15 |
# File 'lib/fulfil_api/resource/errors.rb', line 12 def initialize(resource_klass) @errors = [] @resource_klass = resource_klass end |
Instance Method Details
#add(code:, message:, type:) ⇒ Array<Hash>
Adds a new error to the collection, unless the same error already exists.
26 27 28 29 |
# File 'lib/fulfil_api/resource/errors.rb', line 26 def add(code:, message:, type:) @errors << { code: code.to_s, type: type.to_sym, message: } unless added?(code: code, type: type) @errors end |
#added?(code:, type:) ⇒ Boolean
Checks if an error with the specified code and type has already been added.
39 40 41 42 43 |
# File 'lib/fulfil_api/resource/errors.rb', line 39 def added?(code:, type:) @errors.any? do |error| error[:code] == code.to_s && error[:type] == type.to_sym end end |
#clear ⇒ Array
Clears all errors from the collection.
51 52 53 54 |
# File 'lib/fulfil_api/resource/errors.rb', line 51 def clear @errors = [] @errors end |
#full_messages ⇒ Array<String>
Returns an array of the full error messages (just the message field).
62 63 64 |
# File 'lib/fulfil_api/resource/errors.rb', line 62 def @errors.pluck(:message) end |
#messages ⇒ Array<Hash>
Returns the collection of error messages as an array of hashes.
72 73 74 |
# File 'lib/fulfil_api/resource/errors.rb', line 72 def @errors end |