Class: NeoRack::Builder

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

Overview

A single application NeoRack application DSL script handler.

Use the Builder.load methods for loading scripts.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, script, filename) ⇒ Builder

Initializes a new builder object and run the script in filename



9
10
11
12
13
14
# File 'lib/neorack/builder.rb', line 9

def initialize(server, script, filename)
  @server___, @app___, @warmup___, @app___= server, nil, nil, nil
  @stack_pre___, @stack___, @stack_post___  = [].dup, [].dup, [].dup
  # run script in context of the object, enabling the DLS
  instance_eval(script, filename)
end

Class Method Details

.load(server_klass, filename = 'config.ru') ⇒ Object

Returns a three member Array containing the setup callback stack (Array), the application object and the cleanup callback stack (Array).

On script loading failure (i.e., file name doesn’t exist), returns ‘nil`.

Note: may raise an exception if the script itself raises an exception.

Use:

pre_request, app, post_request = *NeoRack::Builder.load(MyServerClass, 'config.ru')
raise "MyServer couldn't find 'config.ru'" unless app && pre_request && post_request


90
91
92
93
94
95
96
97
98
99
100
# File 'lib/neorack/builder.rb', line 90

def self.load(server_klass, filename = 'config.ru')
	# try to load the file
  script = ::File.read(filename) rescue nil
  return `nil` unless script
  # remove UTF-8 BOM, see: https://stackoverflow.com/questions/2223882/whats-the-difference-between-utf-8-and-utf-8-without-bom
  script.slice!(0..2) if script.encoding == Encoding::UTF_8 && script.start_with?('\xef\xbb\xbf')
  # start a new builder
  instance = NeoRack::Builder.new(server_klass, script, filename)
  # build stack and return
  instance.build_stack___
end

Instance Method Details

#build_stack___Object

Internal use: returns the setup callback stack, the application object and the cleanup callback stack.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/neorack/builder.rb', line 61

def build_stack___
  raise "Application object missing!" unless @app___
  @stack___ << @app___
  app = @stack___.pop
  tmp = nil
  while((tmp = @stack___.pop))
    if tmp[3]
      app = tmp[0].new(app, *tmp[1], &tmp[2])
    else
      app = tmp[0].new(app, *tmp[1])
    end
  end
  @app___ = app
  @stack_post___.reverse!
  @warmup___.call(@app___) if @warmup___
  [@stack_pre___, @app___, @stack_post___]
end

#run(application) ⇒ Object

DSL method - set the application to be used by the Script



22
23
24
25
26
# File 'lib/neorack/builder.rb', line 22

def run(application)
  # add middleware to a middleware stack
  @app___ = application
  self
end

#run_after(prc = nil, &block) ⇒ Object

DSL method - runs ‘.call(request)` after the response ended (when steaming, this is delayed until streaming ends).

Used for cleanup logic, such as removing database connections from the ‘request` Hash, logging, etc’.

Raises:

  • (ArgumentError)


41
42
43
44
45
46
# File 'lib/neorack/builder.rb', line 41

def run_after(prc = nil, &block)
  prc ||= block
  raise(ArgumentError, "this method requires an object that responds to `call(request, response)`") unless(prc.respond_to?(:call))
  @stack_pre___ << prc
  self
end

#run_before(prc = nil, &block) ⇒ Object

DSL method - runs ‘.call(request, response)` before the application handles the response.

Used pre-request logic, such as authentication, database connection checkout, etc’.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
# File 'lib/neorack/builder.rb', line 31

def run_before(prc = nil, &block)
  prc ||= block
  raise(ArgumentError, "this method requires an object that responds to `call(request, response)`") unless(prc.respond_to?(:call))
  @stack_pre___ << prc
  self
end

#serverObject

DSL method - access the Server object and its methods



17
18
19
# File 'lib/neorack/builder.rb', line 17

def server
  @server___
end

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

DSL method for backwards compatibility



49
50
51
52
53
# File 'lib/neorack/builder.rb', line 49

def use(middleware, *args, &block)
  # add middleware to a middleware stack
  @stack___ << [middleware, args, block]
  self
end

#warmup(prc = nil, &block) ⇒ Object

DSL method for backwards compatibility



56
57
58
# File 'lib/neorack/builder.rb', line 56

def warmup(prc = nil, &block)
  @warmup___ ||= prc || block
end