Class: Ballast::Service::Response
- Inherits:
-
Object
- Object
- Ballast::Service::Response
- Defined in:
- lib/ballast/service.rb
Overview
A response to a service invocation.
Instance Attribute Summary (collapse)
-
- (Object) data
readonly
The data returned by the operation.
-
- (Array) errors
readonly
The errors returned by the operation.
-
- (Boolean) success
readonly
Whether the invocation was successful or not.
Instance Method Summary (collapse)
-
- (AjaxResponse) as_ajax_response(transport = nil)
Converts this response to a AJAX response.
-
- (Object) error
Returns the first error returned by the operation.
-
- (Boolean) fail?
(also: #failed?)
Returns whether the invocation failed or not.
-
- (Response) initialize(success = true, data: nil, errors: nil, error: nil)
constructor
Creates a new service response.
-
- (Boolean) success?
(also: #successful?, #succeeded?)
Returns whether the invocation was successful or not.
Constructor Details
- (Response) initialize(success = true, data: nil, errors: nil, error: nil)
Creates a new service response.
29 30 31 32 33 34 35 |
# File 'lib/ballast/service.rb', line 29 def initialize(success = true, data: nil, errors: nil, error: nil) errors ||= error.ensure_array @success = success.to_boolean @data = data @errors = errors.ensure_array(no_duplicates: true, compact: true) end |
Instance Attribute Details
- (Object) data (readonly)
Returns The data returned by the operation.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ballast/service.rb', line 20 class Response attr_reader :success, :data, :errors # Creates a new service response. # # @param success [Boolean] Whether the invocation was successful or not. # @param data [Object|NilClass] The data returned by the operation. # @param errors [Array|NilClass] The errors returned by the operation. # @param error [Object|NilClass] Alias for errors. *Ignored if `errors` is present.* def initialize(success = true, data: nil, errors: nil, error: nil) errors ||= error.ensure_array @success = success.to_boolean @data = data @errors = errors.ensure_array(no_duplicates: true, compact: true) end # Returns whether the invocation was successful or not. # # @return [Boolean] `true` if the service invocation was successful, `false` otherwise. def success? # TODO@PI: Ignore rubocop on this @success end alias_method :successful?, :success? alias_method :succeeded?, :success? # Returns whether the invocation failed or not. # # @return [Boolean] `true` if the service invocation failed, `false` otherwise. def fail? !@success end alias_method :failed?, :fail? # Returns the first error returned by the operation. # # @return [Object] The first error returned by the service. def error @errors.first end # Converts this response to a AJAX response. # # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. # @return [AjaxResponse] The AJAX response, which will include only the first error. def as_ajax_response(transport = nil) status, = if successful? [:ok, nil] elsif error.is_a?(Hash) [error[:status], error[:error]] else [:unknown, error] end AjaxResponse.new(status: status, data: data, error: , transport: transport) end end |
- (Array) errors (readonly)
Returns The errors returned by the operation.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ballast/service.rb', line 20 class Response attr_reader :success, :data, :errors # Creates a new service response. # # @param success [Boolean] Whether the invocation was successful or not. # @param data [Object|NilClass] The data returned by the operation. # @param errors [Array|NilClass] The errors returned by the operation. # @param error [Object|NilClass] Alias for errors. *Ignored if `errors` is present.* def initialize(success = true, data: nil, errors: nil, error: nil) errors ||= error.ensure_array @success = success.to_boolean @data = data @errors = errors.ensure_array(no_duplicates: true, compact: true) end # Returns whether the invocation was successful or not. # # @return [Boolean] `true` if the service invocation was successful, `false` otherwise. def success? # TODO@PI: Ignore rubocop on this @success end alias_method :successful?, :success? alias_method :succeeded?, :success? # Returns whether the invocation failed or not. # # @return [Boolean] `true` if the service invocation failed, `false` otherwise. def fail? !@success end alias_method :failed?, :fail? # Returns the first error returned by the operation. # # @return [Object] The first error returned by the service. def error @errors.first end # Converts this response to a AJAX response. # # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. # @return [AjaxResponse] The AJAX response, which will include only the first error. def as_ajax_response(transport = nil) status, = if successful? [:ok, nil] elsif error.is_a?(Hash) [error[:status], error[:error]] else [:unknown, error] end AjaxResponse.new(status: status, data: data, error: , transport: transport) end end |
- (Boolean) success (readonly)
Returns Whether the invocation was successful or not.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/ballast/service.rb', line 20 class Response attr_reader :success, :data, :errors # Creates a new service response. # # @param success [Boolean] Whether the invocation was successful or not. # @param data [Object|NilClass] The data returned by the operation. # @param errors [Array|NilClass] The errors returned by the operation. # @param error [Object|NilClass] Alias for errors. *Ignored if `errors` is present.* def initialize(success = true, data: nil, errors: nil, error: nil) errors ||= error.ensure_array @success = success.to_boolean @data = data @errors = errors.ensure_array(no_duplicates: true, compact: true) end # Returns whether the invocation was successful or not. # # @return [Boolean] `true` if the service invocation was successful, `false` otherwise. def success? # TODO@PI: Ignore rubocop on this @success end alias_method :successful?, :success? alias_method :succeeded?, :success? # Returns whether the invocation failed or not. # # @return [Boolean] `true` if the service invocation failed, `false` otherwise. def fail? !@success end alias_method :failed?, :fail? # Returns the first error returned by the operation. # # @return [Object] The first error returned by the service. def error @errors.first end # Converts this response to a AJAX response. # # @param transport [Object|NilClass] The transport to use for sending. Must respond to `render`, `params`, `request.format` and `performed?`. # @return [AjaxResponse] The AJAX response, which will include only the first error. def as_ajax_response(transport = nil) status, = if successful? [:ok, nil] elsif error.is_a?(Hash) [error[:status], error[:error]] else [:unknown, error] end AjaxResponse.new(status: status, data: data, error: , transport: transport) end end |
Instance Method Details
- (AjaxResponse) as_ajax_response(transport = nil)
Converts this response to a AJAX response.
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/ballast/service.rb', line 66 def as_ajax_response(transport = nil) status, = if successful? [:ok, nil] elsif error.is_a?(Hash) [error[:status], error[:error]] else [:unknown, error] end AjaxResponse.new(status: status, data: data, error: , transport: transport) end |
- (Object) error
Returns the first error returned by the operation.
58 59 60 |
# File 'lib/ballast/service.rb', line 58 def error @errors.first end |
- (Boolean) fail? Also known as: failed?
Returns whether the invocation failed or not.
50 51 52 |
# File 'lib/ballast/service.rb', line 50 def fail? !@success end |
- (Boolean) success? Also known as: successful?, succeeded?
Returns whether the invocation was successful or not.
40 41 42 43 |
# File 'lib/ballast/service.rb', line 40 def success? # TODO@PI: Ignore rubocop on this @success end |