Class: Sipity::Runners::BaseRunner

Inherits:
Hesburgh::Lib::Runner
  • Object
show all
Defined in:
app/runners/sipity/runners/base_runner.rb

Overview

Note:

You will need to define the #run method on any subclasses.

A simple insulating layer between this application and Hesburgh::Lib

Defines how the application layer interacts with the repository layer.

The primary purpose of the Runner is to offload much of the processing decisions from the controller. This instead lets the controller worry about generating the correct response (e.g. render a template or redirect to another URI) based on the results of the Runner.

In offloading the processing from the controller, the runner can, with minimal adjustments, operate in a different context. In other words, a Runner could be used to build a suite of command-line commands.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, options = {}, &block) ⇒ BaseRunner

Note:

By convention, the Rails application will instantiate this object without passing any options; Thus the class configuration options will be used. However, it is possible that another application (i.e. a command- line application) would opt to instead instantiate the object directly.

Returns a new instance of BaseRunner

Parameters:

  • context (#current_user, #repository)

    The containing context in which the runner is acting. This is likely an ApplicationController.

  • options (Hash) (defaults to: {})

    configuration options

Options Hash (options):

  • :authentication_layer (#call(context), false, :default, :none)

    defines how authentication will or will not be enforced/verified.

  • :authorization_layer (#enforce!)

    What are the authorization_layer that should be in effect for this instance?


49
50
51
52
53
54
# File 'app/runners/sipity/runners/base_runner.rb', line 49

def initialize(context, options = {}, &block)
  super(context, &block)
  self.authentication_layer = options.fetch(:authentication_layer) { self.class.authentication_layer }
  enforce_authentication!
  self.authorization_layer = options.fetch(:authorization_layer) { self.class.authorization_layer }
end

Class Attribute Details

.authentication_layer#call(context)

Returns:

  • (#call(context))

See Also:

  • BaseRunner#authentication_layer=

# File 'app/runners/sipity/runners/base_runner.rb', line 33

.authorization_layer#enforce!(question/entity pairs)

Returns:

  • (#enforce!(question/entity pairs))

See Also:

  • BaseRunner#authorization_layer=

# File 'app/runners/sipity/runners/base_runner.rb', line 33

Instance Attribute Details

#authentication_layerObject (private)

Returns the value of attribute authentication_layer


57
58
59
# File 'app/runners/sipity/runners/base_runner.rb', line 57

def authentication_layer
  @authentication_layer
end

#authorization_layerObject (private)

Returns the value of attribute authorization_layer


57
58
59
# File 'app/runners/sipity/runners/base_runner.rb', line 57

def authorization_layer
  @authorization_layer
end

Instance Method Details

#authentication_layer_that_authenticates_anythingObject (private)


85
86
87
# File 'app/runners/sipity/runners/base_runner.rb', line 85

def authentication_layer_that_authenticates_anything
  -> (*) { true }
end

#authentication_layer_that_uses_context_authenticationObject (private)


89
90
91
92
# File 'app/runners/sipity/runners/base_runner.rb', line 89

def authentication_layer_that_uses_context_authentication
  # Devise provides helpful authentication options; I'm using those.
  ->(context) { context.authenticate_user! }
end

#authorization_layer_that_authorizes_everythingObject (private)


120
121
122
# File 'app/runners/sipity/runners/base_runner.rb', line 120

def authorization_layer_that_authorizes_everything
  Services::AuthorizationLayer::AuthorizeEverything.new(self)
end

#authorization_layer_with_enforcementObject (private)


116
117
118
# File 'app/runners/sipity/runners/base_runner.rb', line 116

def authorization_layer_with_enforcement
  Services::AuthorizationLayer.new(self)
end

#enforce_authentication!Object (private)


94
95
96
97
98
99
100
101
# File 'app/runners/sipity/runners/base_runner.rb', line 94

def enforce_authentication!
  return true if authentication_layer.call(context)
  # I am choosing to raise this exception because the default authentication
  # service has likely thrown an exception if things have failed. This is
  # my last line of defense. If you encounter this exception, make sure
  # to review the authentication_service method for its output.
  fail Exceptions::AuthenticationFailureError, self.class
end

#runObject

The returned value should be the response from the call of a NamedCallback.

Returns:

  • results of the #callback method

See Also:

  • NamedCallback

66
67
68
# File 'app/runners/sipity/runners/base_runner.rb', line 66

def run(*)
  fail NotImplementedError, "Expected #{self.class} to implement ##{__method__}"
end