forked from g3ortega/kakaomailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
81 lines (64 loc) · 1.9 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require "rubygems"
require "sinatra/base"
require "json"
require "sendgrid-ruby"
include SendGrid
class App < Sinatra::Base
before do
content_type :json
headers 'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': ['OPTIONS', 'GET', 'POST'],
'Access-Control-Allow-Headers': 'Content-Type'
end
set :protection, except: :http_origin
helpers do
def send_email(params, user)
# create variables
from = Email.new(email: params["email"], name: params["name"])
to = Email.new(email: user.email, name: user.name)
subject = params["subject"]
content = Content.new(type: 'text/html', value: params["message"])
# create email
mail = Mail.new(from, subject, to, content)
# instance sendgrid
sendgrid = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
if ENV['SENDGRID_API_KEY']
# send email
response = sendgrid.client.mail._('send').post(request_body: mail.to_json)
response.body
else
puts "Please add an environment variable for Mandrill Key"
end
end
end
options "/user" do
200
end
post "/user" do
content_type :json
params = JSON.parse(request.body.read)
user = User.first(uuid: params["uuid"])
unless user
halt 400, "Bad Request"
end
if user and params.keys.length == 5
status = send_email(params, user)
if status[0]['status'] != 'sent'
return { message: "The message could not be sent" }.to_json
else
halt 200, {message: "Message Sent!"}.to_json
end
else
halt 400, "Bad Request"
end
end
post "/register" do
user = User.first(email: params[:email])
if user
"The user is already registered"
else
@user = User.create(name: params[:name], email: params[:email], url: params[:url], uuid: UUID.generate)
"Token: #{@user.uuid} "
end
end
end