Class: ServicePattern::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/service_pattern/response.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#errorsObject (readonly)

Returns the value of attribute errors.



2
3
4
# File 'lib/service_pattern/response.rb', line 2

def errors
  @errors
end

#resultObject (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_messagesObject



16
17
18
# File 'lib/service_pattern/response.rb', line 16

def error_messages
  @error_messages ||= @errors.map(&:message)
end

#error_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/service_pattern/response.rb', line 24

def error_type?(type)
  error_types.include?(type)
end

#error_typesObject



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

Returns:

  • (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(error_messages.join(". "))
  error.errors = errors

  raise error
end

#success?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/service_pattern/response.rb', line 41

def success?
  @success
end