Class: ServicePattern::Response
- Inherits:
-
Object
- Object
- ServicePattern::Response
- Defined in:
- lib/service_pattern/response.rb
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#result ⇒ Object
readonly
Returns the value of attribute result.
Class Method Summary collapse
Instance Method Summary collapse
- #error_messages ⇒ Object
- #error_type?(type) ⇒ Boolean
- #error_types ⇒ Object
-
#initialize(errors: [], result: nil) ⇒ Response
constructor
A new instance of Response.
- #only_error_type?(type) ⇒ Boolean
- #raise_error! ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(errors: [], result: nil) ⇒ Response
Returns a new instance of Response.
10 11 12 13 14 |
# File 'lib/service_pattern/response.rb', line 10 def initialize(errors: [], result: nil) @errors = ServicePattern::Service.convert_errors(errors) @result = result @success = !errors || errors.empty? end |
Instance Attribute Details
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
2 3 4 |
# File 'lib/service_pattern/response.rb', line 2 def errors @errors end |
#result ⇒ Object (readonly)
Returns the value of attribute result.
2 3 4 |
# File 'lib/service_pattern/response.rb', line 2 def result @result end |
Class Method Details
.check_response!(service, response) ⇒ Object
4 5 6 7 8 |
# File 'lib/service_pattern/response.rb', line 4 def self.check_response!(service, response) return if response.is_a?(ServicePattern::Response) raise ServicePattern::InvalidResponseError, "Expected a ServicePattern::Response from #{service.class.name} but it was instead: #{response.class.name}" end |
Instance Method Details
#error_messages ⇒ Object
16 17 18 |
# File 'lib/service_pattern/response.rb', line 16 def @error_messages ||= @errors.map(&:message) end |
#error_type?(type) ⇒ Boolean
24 25 26 |
# File 'lib/service_pattern/response.rb', line 24 def error_type?(type) error_types.include?(type) end |
#error_types ⇒ Object
20 21 22 |
# File 'lib/service_pattern/response.rb', line 20 def error_types @error_types ||= @errors.map(&:type).reject(&:blank?) end |
#only_error_type?(type) ⇒ Boolean
28 29 30 |
# File 'lib/service_pattern/response.rb', line 28 def only_error_type?(type) error_types.length == 1 && error_type?(type) end |
#raise_error! ⇒ Object
32 33 34 35 36 37 38 39 |
# File 'lib/service_pattern/response.rb', line 32 def raise_error! return if errors.empty? error = ServicePattern::FailedError.new(.join(". ")) error.errors = errors raise error end |
#success? ⇒ Boolean
41 42 43 |
# File 'lib/service_pattern/response.rb', line 41 def success? @success end |