Sha256: 8495faacee0db4bbcb291a9fd1a8283300a8caf46d2f6130a4394510294d73c4

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

# frozen_string_literal: true

require "rack"

module Lita
  # A wrapper around a handler's HTTP route callbacks that sets up the request and response.
  # @api private
  # @since 4.0.0
  class HTTPCallback
    # @param handler_class [Handler] The handler defining the callback.
    # @param callback [Proc] The callback.
    def initialize(handler_class, callback)
      @handler_class = handler_class
      @callback = callback
    end

    # Call the Rack endpoint with a standard environment hash.
    def call(env)
      request = Rack::Request.new(env)
      response = Rack::Response.new

      if request.head?
        response.status = 204
      else
        begin
          handler = @handler_class.new(env["lita.robot"])

          @callback.call(handler, request, response)
        rescue StandardError => e
          robot = env["lita.robot"]
          error_handler = robot.config.robot.error_handler

          if error_handler.arity == 2
            error_handler.call(e, rack_env: env, robot: robot)
          else
            error_handler.call(e)
          end

          raise
        end
      end

      response.finish
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
rita-5.0.0.alpha.4 lib/lita/http_callback.rb
rita-5.0.0.alpha.3 lib/lita/http_callback.rb
rita-5.0.0.alpha.2 lib/lita/http_callback.rb
rita-5.0.0.alpha.1 lib/lita/http_callback.rb