Class: Substation::Response Abstract

Inherits:
Object
  • Object
show all
Includes:
AbstractType, Adamantium::Flat
Defined in:
lib/substation/response.rb,
lib/substation/response/api.rb,
lib/substation/response/failure.rb,
lib/substation/response/success.rb,
lib/substation/response/exception.rb,
lib/substation/response/exception/output.rb

Overview

This class is abstract.

Base class for action responses

The following code illustrates context and serves as setup for all instance method doc examples

module App
  class Environment
    def initialize(storage, logger)
      @storage, @logger = storage, logger
    end
  end

  class SuccessfulAction
    def self.call(request)
      data = perform_work
      request.success(data)
    end
  end

  class FailingAction
    def self.call(request)
      error = perform_work
      request.error(error)
    end
  end
end

storage    = SomeStorageAbstraction.new
env        = App::Environment.new(storage, Logger.new($stdout))
dispatcher = Substation::Dispatcher.coerce({
  :successful_action => { :action => App::SuccessfulAction },
  :failing_action    => { :action => App::FailingAction }
}, env)

Direct Known Subclasses

Exception, Failure, Success

Defined Under Namespace

Modules: API Classes: Exception, Failure, Success

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_request, _output) ⇒ undefined

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initialize a new instance

Parameters:

  • request (Request)

    the request passed to the action that returned this response

  • output (Object)

    the data returned from the action that returned this response



85
86
87
88
89
# File 'lib/substation/response.rb', line 85

def initialize(_request, _output)
  super
  @env   = request.env
  @input = request.input
end

Instance Attribute Details

#envObject (readonly)

The application environment used within an action

Examples:


response = dispatcher.call(:successful_action, :some_input)
response.env # => env passed to Substation::Dispatcher.coerce(config, env)

Returns:

  • (Object)


58
59
60
# File 'lib/substation/response.rb', line 58

def env
  @env
end

#inputObject (readonly)

The request model instance passed into an action

Examples:


response = dispatcher.call(:successful_action, :some_input)
response.input # => :some_input

Returns:

  • (Object)

See Also:



72
73
74
# File 'lib/substation/response.rb', line 72

def input
  @input
end

Instance Method Details

#exception?true

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Test wether processing raised an exception

Returns:

  • (true)


114
115
116
# File 'lib/substation/response.rb', line 114

def exception?
  false
end

#success?Boolean

This method is abstract.

Indicates wether this is a successful response or not

Examples:


response = dispatcher.call(:successful_action, :some_input)
response.success? # => true

Returns:

  • (Boolean)

    true if successful, false otherwise

See Also:



107
# File 'lib/substation/response.rb', line 107

abstract_method :success?

#to_request(new_input = output) ⇒ Request

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return a Substation::Request instance built upon this response

Returns:



123
124
125
# File 'lib/substation/response.rb', line 123

def to_request(new_input = output)
  Request.new(request.name, env, new_input)
end