-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
56 lines (51 loc) · 1.76 KB
/
app.rb
1
2
3
4
5
6
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
44
45
46
47
48
49
50
51
52
53
54
55
56
require 'dotenv/load'
require 'sinatra'
require './lib/slack_notifier'
require './lib/webhook_bib_response'
require './lib/message_validator'
# echo back challenge to confirm endpoint viability
get '/' do
challenge = request.params['challenge']
response.write({ challenge: challenge }.to_json)
response.close
end
# upon validating EXL sig header, push Webhook notification body to Slack
post '/' do
request_body = request.body.read
unless MessageValidator.valid?(
request_body,
request.env['X-Exl-Signature'] || request.env['HTTP_X_EXL_SIGNATURE']
)
response.status = 401
response.write({ error_message: 'Invalid Signature' }.to_json)
response.close
return
end
bib_action = WebhookBibResponse.new request_body
# No MMS ID? Nothing we can do with this hook
unless bib_action.mms_id
response.status = 400
response.write({ error_message: 'Failed to extract MMS ID from response' }.to_json)
response.close
return
end
# Based on the BIB action, do something (for now, post a Slack notification)
# Use a response status with light semantic value, mostly for spec purposes
# as Alma/ExL likely doesn't care about response codes.
response.status = case bib_action.event
when 'BIB_UPDATED'
# do noting for now
# SlackNotifier.send bib_action.template
204
when 'BIB_DELETED'
# do nothing for now
# SlackNotifier.send bib_action.template
204
when 'BIB_CREATED'
SlackNotifier.ping bib_action.template
200
else
500
end
response.close
end