Class: RackEnvironment

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

Instance Method Summary collapse

Constructor Details

#initialize(app) {|_self| ... } ⇒ RackEnvironment

Returns a new instance of RackEnvironment.

Yields:

  • (_self)

Yield Parameters:



3
4
5
6
7
8
# File 'lib/rack_environment.rb', line 3

def initialize(app)
  @app = app
  @environments = {}
  @changes = lambda {}
  yield self
end

Instance Method Details

#_call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack_environment.rb', line 15

def _call(env)
  @status, @headers, @response = @app.call(env)
    @@environment ||= detect_env
    if @@environment && modificable?(@response)
      @@changes ||= @changes.call(@@environment.first)
      @response.body = append_string_to_body(@response.body,@@changes)
      @headers["Content-Length"] = @response.body.size.to_s
    end
rescue => e
  $stderr << e.inspect+"\n"
  $stderr << e.backtrace.join("\n")+"\n"
ensure
  return [@status, @headers, @response]
end

#append_to_body(proc) ⇒ Object



35
36
37
# File 'lib/rack_environment.rb', line 35

def append_to_body(proc)
  @changes = proc
end

#call(env) ⇒ Object



10
11
12
13
# File 'lib/rack_environment.rb', line 10

def call(env)
  @env = env
  dup._call(env)
end

#define_environment(name, proc) ⇒ Object



31
32
33
# File 'lib/rack_environment.rb', line 31

def define_environment(name, proc)
  @environments[name] = proc
end

#server_nameObject



39
40
41
# File 'lib/rack_environment.rb', line 39

def server_name
  @env['SERVER_NAME']
end