Class: Ballast::Service::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ballast/service.rb

Overview

A response to a service invocation.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(success = true, data: nil, errors: nil, error: nil) ⇒ Response

Creates a new service response.

Parameters:

  • success (Boolean) (defaults to: true)

    Whether the invocation was successful or not.

  • data (Object|NilClass) (defaults to: nil)

    The data returned by the operation.

  • errors (Array|NilClass) (defaults to: nil)

    The errors returned by the operation.

  • error (Object|NilClass) (defaults to: nil)

    Alias for errors. Ignored if errors is present.



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

#dataObject (readonly)

Returns The data returned by the operation.

Returns:

  • (Object)

    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, 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
end

#errorsArray (readonly)

Returns The errors returned by the operation.

Returns:

  • (Array)

    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, 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
end

#successBoolean (readonly)

Returns Whether the invocation was successful or not.

Returns:

  • (Boolean)

    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, 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
end

Instance Method Details

#as_ajax_response(transport = nil) ⇒ AjaxResponse

Converts this response to a AJAX response.

Parameters:

  • transport (Object|NilClass) (defaults to: nil)

    The transport to use for sending. Must respond to render, params, request.format and performed?.

Returns:

  • (AjaxResponse)

    The AJAX response, which will include only the first error.



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, 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

#errorObject

Returns the first error returned by the operation.

Returns:

  • (Object)

    The first error returned by the service.



58
59
60
# File 'lib/ballast/service.rb', line 58

def error
  @errors.first
end

#fail?Boolean Also known as: failed?

Returns whether the invocation failed or not.

Returns:

  • (Boolean)

    true if the service invocation failed, false otherwise.



50
51
52
# File 'lib/ballast/service.rb', line 50

def fail?
  !@success
end

#success?Boolean Also known as: successful?, succeeded?

Returns whether the invocation was successful or not.

Returns:

  • (Boolean)

    true if the service invocation was successful, false otherwise.



40
41
42
43
# File 'lib/ballast/service.rb', line 40

def success?
  # TODO@PI: Ignore rubocop on this
  @success
end