-
Notifications
You must be signed in to change notification settings - Fork 0
/
communication_app.rb
45 lines (34 loc) · 1.03 KB
/
communication_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
class CommunicationApp < Sinatra::Base
set :show_exceptions, false
post "/email" do
format_date
email = Email.new(params)
if email.save
email = email.reload
status = email.status
message = 'Success'
elsif email.errors.any?
status = 422
message = email.errors.details
else
status = 500
message = 'Internal server error'
end
{ status: status, message: message }.to_json
end
post '/sg_webhook' do
params = JSON.parse(request.body.read)
WebhookLog.create(service: :sendgrid, event: params[0]['event'], email: params[0]['email'], response: params)
end
post '/pm_webhook' do
params = JSON.parse(request.body.read)
WebhookLog.create(service: :postmark, event: params['RecordType'], email: params['Email'], response: params)
end
error do
{ status: 500, message: env['sinatra.error'].message }.to_json
end
private
def format_date
params[:send_at] = DateTime.parse(params[:send_at]) if params[:send_at].present?
end
end