Class: Serf::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/serf/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Builder

Returns a new instance of Builder.



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/serf/builder.rb', line 14

def initialize(*args, &block)
  opts = Optser.extract_options! args

  @run = opts.get :interactor
  @use = []
  @policy_chain = opts.get :policy_chain, []

  if block_given?
    instance_eval(&block)
  else
    use_defaults
  end
end

Instance Method Details

#run(interactor) ⇒ Object



71
72
73
# File 'lib/serf/builder.rb', line 71

def run(interactor)
  @run = interactor
end

#to_appObject



75
76
77
# File 'lib/serf/builder.rb', line 75

def to_app
  @use.reverse.inject(@run) { |a,e| e[a] }
end

#use(middleware, *args, &block) ⇒ Object



67
68
69
# File 'lib/serf/builder.rb', line 67

def use(middleware, *args, &block)
  @use << proc { |app| middleware.new(app, *args, &block) }
end

#use_default_middlewareObject

Add the following middleware to the chain:

use Serf::Middleware::RequestTimer
use Serf::Middleware::ParcelMasher
use Serf::Middleware::UuidTagger
use Serf::Middleware::ErrorHandler


47
48
49
50
51
52
# File 'lib/serf/builder.rb', line 47

def use_default_middleware
  use Serf::Middleware::RequestTimer
  use Serf::Middleware::ParcelMasher
  use Serf::Middleware::UuidTagger
  use Serf::Middleware::ErrorHandler
end

#use_default_serfer_stageObject

Add the following middleware to the chain:

use Serf::Middleware::ParcelFreezer
use Serf::Middleware::PolicyChecker, @policy_chain
use Serf::Serfer


61
62
63
64
65
# File 'lib/serf/builder.rb', line 61

def use_default_serfer_stage
  use Serf::Middleware::ParcelFreezer
  use Serf::Middleware::PolicyChecker, policy_chain: @policy_chain
  use Serf::Serfer
end

#use_defaultsObject

Set a default chain of the following:

use_default_middleware
use_default_serfer_stage


34
35
36
37
# File 'lib/serf/builder.rb', line 34

def use_defaults
  use_default_middleware
  use_default_serfer_stage
end