Class: Lita::RackApp

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

Overview

A Rack application to serve HTTP routes registered by handlers.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(robot) ⇒ RackApp

Returns a new instance of RackApp.

Parameters:



31
32
33
34
35
# File 'lib/lita/rack_app.rb', line 31

def initialize(robot)
  @robot = robot
  @router = HttpRouter.new
  compile
end

Instance Attribute Details

#robotLita::Robot (readonly)

The currently running robot.

Returns:



6
7
8
# File 'lib/lita/rack_app.rb', line 6

def robot
  @robot
end

#routerHttpRouter (readonly)

An HttpRouter used for dispatch.

Returns:

  • (HttpRouter)

    The router.



10
11
12
# File 'lib/lita/rack_app.rb', line 10

def router
  @router
end

Class Method Details

.build(robot) ⇒ Lita::RackApp, Class

Constructs a Lita::RackApp inside a Rack::Builder, including any configured middleware.

Parameters:

Returns:



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

def self.build(robot)
  builder = Rack::Builder.new
  builder.run(new(robot))

  robot.config.http.middleware.each do |wrapper|
    if wrapper.block
      builder.use(wrapper.middleware, *wrapper.args, &wrapper.block)
    else
      builder.use(wrapper.middleware, *wrapper.args)
    end
  end

  builder.to_app
end

Instance Method Details

#call(env) ⇒ void

This method returns an undefined value.

Entry point for Lita’s HTTP routes. Invokes the Rack application.

Parameters:

  • env (Hash)

    A Rack environment.



40
41
42
43
# File 'lib/lita/rack_app.rb', line 40

def call(env)
  env["lita.robot"] = robot
  router.call(env)
end

#inspectObject

Overrides the default inspect implementation to make output less verbose and more readable.



46
47
48
49
# File 'lib/lita/rack_app.rb', line 46

def inspect
  hex_address = (object_id << 1).to_s(16).rjust(14, "0")
  "#<Lita::RackApp:0x#{hex_address}>"
end

#recognize(env) ⇒ Array

Finds the first route that matches the request environment, if any. Does not trigger the route.

Parameters:

  • env (Hash)

    A Rack environment.

Returns:

  • (Array)

    An array of the name of the first matching route.

Since:

  • 4.0.0



56
57
58
59
# File 'lib/lita/rack_app.rb', line 56

def recognize(env)
  env["lita.robot"] = robot
  recognized_routes_for(env).map { |match| match.route.name }
end