-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
99 lines (81 loc) · 2.07 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
require 'sinatra'
require 'rack-flash'
require 'json'
require 'pry'
require 'tilt/erb'
require 'dotenv'
require 'tilt/sass'
require 'bootstrap-sass'
require 'compass'
require 'omniauth'
require 'omniauth-foursquare'
require 'keen'
Dotenv.load! unless ENV['RACK_ENV'] == 'production'
require File.expand_path('../models/drink.rb', __FILE__)
require File.expand_path('../models/consumption.rb', __FILE__)
module Coffee
class App < Sinatra::Base
use Rack::Session::Cookie
use Rack::Flash
use OmniAuth::Builder do
provider :foursquare, ENV['FOURSQUARE_CLIENT_ID'], ENV['FOURSQUARE_CLIENT_SECRET']
end
configure do
Compass.configuration do |config|
config.project_path = File.dirname(__FILE__)
end
set :scss, Compass.sass_engine_options
end
get '/styles.css' do
scss :"sass/styles"
end
get '/' do
if current_user
@drinks = Drink.all
erb :index
else
erb :logged_out
end
end
get '/analytics' do
authenticate
erb :analytics
end
get '/auth/foursquare/callback' do
redirect '/' unless auth_hash['uid'] == ENV['ALLOWED_UID']
session['current_user'] = {
info: auth_hash['info'],
uid: auth_hash['uid'],
token: auth_hash['credentials']['token']
}.to_json
flash[:notice] = "Welcome back, #{auth_hash['info']['name']}."
redirect '/'
end
get '/sign_out' do
session['current_user'] = nil
flash[:notice] = 'You are logged out.'
redirect '/'
end
post '/log' do
authenticate
drink = Drink.new(params['name'], params['size'])
consumption = Consumption.new(drink)
consumption.save
flash[:notice] = 'Saved!'
redirect '/'
end
def authenticate
if current_user.nil?
flash[:notice] = 'You must log in first.'
redirect '/'
end
end
def current_user
return nil unless session['current_user']
JSON.parse(session['current_user'])
end
def auth_hash
request.env['omniauth.auth']
end
end
end