7
8
9
10
11
12
13
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
39
40
41
42
43
|
# File 'lib/legion/api/inbound_webhooks.rb', line 7
def self.registered(app)
app.post '/api/webhooks/:source' do
require 'legion/trigger'
source_name = params[:source]
body_raw = request.body.read
body = begin
Legion::JSON.load(body_raw)
rescue StandardError
halt 400, json_error('invalid_body', 'request body must be valid JSON', status_code: 400)
end
= request.env.select { |k, _| k.start_with?('HTTP_') }
result = Legion::Trigger.process(
source_name: source_name,
headers: ,
body_raw: body_raw,
body: body
)
if result[:success]
json_response(result, status_code: 202)
elsif result[:reason] == :duplicate
json_response(result, status_code: 200)
elsif result[:reason] == :unknown_source
halt 404, json_error('unknown_source', result[:error], status_code: 404)
else
halt 500, json_error('trigger_error', result[:error] || 'processing failed', status_code: 500)
end
end
app.get '/api/webhooks/sources' do
require 'legion/trigger'
json_response({ sources: Legion::Trigger.registered_sources })
end
end
|