Class: Teams::RackApp

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

Constant Summary collapse

FUNCTION_PATH =
%r{\A/api/functions/([^/]+)\z}

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackApp

Returns a new instance of RackApp.



8
9
10
# File 'lib/teams/rack_app.rb', line 8

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/teams/rack_app.rb', line 14

def call(env)
  request = Rack::Request.new(env)

  if request.post? && (function = FUNCTION_PATH.match(request.path_info))
    body = request.body.read
    payload = body.empty? ? {} : JSON.parse(body)
    return rack_response(@app.process_function(function[1], payload, env:))
  end

  return not_found unless request.post? && request.path_info == @app.messaging_endpoint

  body = request.body.read
  payload = body.empty? ? {} : JSON.parse(body)
  response = @app.process_inbound(payload, env:)
  rack_response(response)
rescue JSON::ParserError
  @app.logger.warn("Rejected Teams request: invalid JSON body")
  rack_response(Response.new(status: 400, body: { error: "invalid JSON" }))
rescue BadRequestError, AuthenticationError => error
  @app.logger.warn("Rejected Teams request (#{error_status(error)}): #{error.message}")
  rack_response(Response.new(status: error_status(error), body: { error: error.message }))
rescue StandardError => error
  @app.logger.error("Error processing Teams activity: #{error.class}: #{error.message}")
  rack_response(Response.new(status: 500, body: { error: "Internal server error" }))
end