Class: Lackie::Rack::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/lackie/rack/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, logger = NilLogger.new) ⇒ Middleware

Returns a new instance of Middleware.



7
8
9
10
11
12
13
# File 'lib/lackie/rack/middleware.rb', line 7

def initialize(app, logger=NilLogger.new)
  @app, @logger = app, logger
  @command = nil
  @result = nil
  @surrender = Lackie::JavaScript::Surrender.new
  @command_id = 0
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/lackie/rack/middleware.rb', line 15

def call(env)
  request = ::Rack::Request.new(env)
  if request.path.match(/\/lackie\/(.+)$/) and self.respond_to?($1)
    self.send($1, request)
  else
    @app.call(env)
  end
end

#eval(request) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/lackie/rack/middleware.rb', line 29

def eval(request)
  @result = nil
  @command = request.body.read.to_s
  @command_id += 1
  @logger.log("eval " + command_json(@command))
  plain("OK")
end

#result(request) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/lackie/rack/middleware.rb', line 47

def result(request)
  if request.get?
    get_result(request)
  else
    set_result(request)
  end
end

#surrender(request) ⇒ Object



24
25
26
27
# File 'lib/lackie/rack/middleware.rb', line 24

def surrender(request)
  @logger.log("surrendered to #{request.user_agent}")
  js(@surrender.script)
end

#yield(request) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/lackie/rack/middleware.rb', line 37

def yield(request)
  if @command.nil?
    not_found
  else
    cmd = @command
    @command = nil
    js(command_json(cmd))
  end
end