-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rb
54 lines (42 loc) · 1.1 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
require "sinatra"
require 'dotenv/load'
require "sendgrid-ruby"
require "json"
require "erb"
include SendGrid
file = File.read('data/catalog.json')
data = JSON.parse(file)
get "/" do
erb :index
end
get "/cakes" do
@catalog = data["data"]["cakes"]
erb :cakes
end
get "/cupcakes" do
@catalog = data["data"]["cupcakes"]
erb :cupcakes
end
get "/macaroons" do
@catalog = data["data"]["macaroons"]
erb :macaroons
end
post "/contacts" do
@name, @email = params[:name], params[:email]
@data = render_email
from = Email.new(email: '[email protected]')
to = Email.new(email: @email)
subject = "Catalog"
content = Content.new(type: 'text/html', value: @data)
mail = Mail.new(from, subject, to, content)
sg = SendGrid::API.new(api_key: ENV['SENDGRID_API_KEY'])
response = sg.client.mail._('send').post(request_body: mail.to_json)
redirect "/"
end
def render_email
file = File.read('data/catalog.json')
data = JSON.parse(file)
@catalog = data["data"]
@template = File.read('./views/email.erb')
ERB.new(@template).result( binding )
end