class Ballast::Service::Response
A response to a service invocation.
@attribute [r] success
@return [Boolean] Whether the invocation was successful or not.
@attribute [r] data
@return [Object] The data returned by the operation.
@attribute [r] errors
@return [Array] The errors returned by the operation.
Attributes
Public Class Methods
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.*
# 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
Public Instance Methods
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.
# File lib/ballast/service.rb, line 66 def as_ajax_response(transport = nil) status, error_message = if successful? [:ok, nil] elsif error.is_a?(Hash) [error[:status], error[:error]] else [:unknown, error] end AjaxResponse.new(status: status, data: data, error: error_message, transport: transport) end
Returns the first error returned by the operation.
@return [Object] The first error returned by the service.
# File lib/ballast/service.rb, line 58 def error @errors.first end
Returns whether the invocation failed or not.
@return [Boolean] `true` if the service invocation failed, `false` otherwise.
# File lib/ballast/service.rb, line 50 def fail? !@success end
Returns whether the invocation was successful or not.
@return [Boolean] `true` if the service invocation was successful, `false` otherwise.
# File lib/ballast/service.rb, line 40 def success? # TODO@PI: Ignore rubocop on this @success end