class Lita::RackApp

A Rack application to serve HTTP routes registered by handlers.

Attributes

robot[R]

The currently running robot. @return [Lita::Robot] The robot.

router[R]

An HttpRouter used for dispatch. @return [HttpRouter] The router.

Public Class Methods

build(robot) click to toggle source

Constructs a {RackApp} inside a Rack::Builder, including any configured middleware. @param robot [Lita::Robot] The currently running robot. @return [Lita::RackApp, Class] The Rack application.

# 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
new(robot) click to toggle source

@param robot [Lita::Robot] The currently running robot.

# File lib/lita/rack_app.rb, line 31
def initialize(robot)
  @robot = robot
  @router = HttpRouter.new
  compile
end

Public Instance Methods

call(env) click to toggle source

Entry point for Lita's HTTP routes. Invokes the Rack application. @param env [Hash] A Rack environment. @return [void]

# File lib/lita/rack_app.rb, line 40
def call(env)
  env["lita.robot"] = robot
  router.call(env)
end
inspect() click to toggle source

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

# 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) click to toggle source

Finds the first route that matches the request environment, if any. Does not trigger the route. @param env [Hash] A Rack environment. @return [Array] An array of the name of the first matching route. @since 4.0.0

# 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

Private Instance Methods

compile() click to toggle source

Registers routes in the router for each handler's defined routes.

# File lib/lita/rack_app.rb, line 64
def compile
  robot.handlers.each do |handler|
    next unless handler.respond_to?(:http_routes)

    handler.http_routes.each { |route| router.add_route(route) }
  end
end
recognized_routes_for(env) click to toggle source

Returns an array containing the first recongnized route, if any.

# File lib/lita/rack_app.rb, line 73
def recognized_routes_for(env)
  Array(router.recognize(env).first)
end