This repository was archived by the owner on Mar 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.rb
More file actions
62 lines (54 loc) · 1.7 KB
/
app.rb
File metadata and controls
62 lines (54 loc) · 1.7 KB
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
require 'sinatra'
require 'sinatra/param'
require 'sinatra/cross_origin'
require 'json'
require 'google_drive'
# noinspection RubyStringKeysInHashInspection
PRODUCTS = {
'Hoodie' => %w(black blue grey),
'T-Shirt' => %w(black blue grey),
'Travel Mug' => %w(white)
}
configure do
enable :cross_origin
end
get '/' do
'It\'s working!'
end
post '/reservations.json' do
content_type :json
param :name, String, required: true
param :email, String, required: true
param :product, String, required: true, in: PRODUCTS.keys
param :colour, String
param :size, String, in: %w(XS S M L XL)
param :quantity, Integer, required: true
key = OpenSSL::PKey::RSA.new ENV['CLIENT_KEY'].gsub(/\\n/, "\n") || raise('No CLIENT_KEY provided'), 'notasecret'
client = Google::APIClient.new
client.authorization = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: %w(https://www.googleapis.com/auth/drive https://spreadsheets.google.com/feeds),
issuer: ENV['SERVICE_EMAIL'],
signing_key: key
)
client.authorization.fetch_access_token!
session = GoogleDrive.login_with_oauth client.authorization.access_token
spreadsheet = session.spreadsheet_by_key ENV['SHEET_KEY']
ws = spreadsheet.worksheets.first
# Add a new row to the sheet with the reservation info
ws.list.push({
:'Name' => params['name'],
:'Email' => params['email'],
:'Product' => params['product'],
:'Colour' => params['colour'],
:'Size' => params['size'],
:'Quantity' => params['quantity']
})
# Attempt save
if ws.save
200
else
[500, 'Failed to save spreadsheet']
end
end