This repository has been archived by the owner on Dec 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.rb
166 lines (134 loc) · 3.41 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require 'rubygems'
require 'bundler'
require './sinatra/javascript'
require './src/db_user'
require './src/db_conferences'
require './src/db_growth'
Bundler.require(:default)
class MyApp < Sinatra::Base
helpers Sinatra::JavaScripts
configure do
enable :sessions
Bundler.require(:development) if development?
register Sinatra::Reloader if development?
set :db_user, UserData.new
set :db_conf, ConferenceData.new
set :db_growth, GrowthData.new
end
helpers do
# Whether user is authenticated
def authenticated?
not session[:identity].nil?
end
# Get all errors and reset array
def pop_errors
tmp = session[:errors] || []
session[:errors] = []
return tmp
end
# Add error to array
def push_error(error)
(session[:errors] ||= []).push(error)
end
end
before do
logger.level = Logger::DEBUG
end
get '/' do
js :home, :introjs, 'introjs/home'
erb :home
end
get '/browse' do
@title = "Browse Conferences"
js :knockout, 'knockout/browse', :introjs, 'introjs/browse'
erb :browse
end
get '/venues' do
js :knockout, 'foursquare', 'knockout/venues'
erb :venues
end
get '/growth' do
js :knockout, :nvd3, 'growth/pie', 'growth/years', 'growth/timelapse', 'knockout/growth', :introjs, 'introjs/growth'
erb :growth
end
post '/growth' do
return settings.db_growth.get(params["industry"],params["year"],params["location"]).sort{|x,y| x["date"] <=> y["date"]}.to_json
end
#### AUTHENTICATION ####
get '/login' do
@title = "Log In"
erb :login
end
post '/login' do
session[:identity] = settings.db_user.authenticate(params['email'], params['password'])
if session[:identity]
redirect to '/'
else
# push_error('Invalid login')
redirect to '/login'
end
end
get '/logout' do
session.delete(:identity)
redirect to '/'
end
get '/signup' do
@title = "Sign Up"
erb :signup
end
post '/signup' do
push_error("Email taken") if settings.db_user.getUser(params['email'])
push_error("Passwords must match") if not (params['password'] == params['re-password'])
if not session[:errors] or session[:errors].empty?
session[:identity] = 1
settings.db_user.storeUser(params['email'], params['password'])
redirect to '/'
else
redirect to '/signup'
end
end
get '/conference' do
@title = "Conference Details"
js :knockout, 'knockout/conference'
erb :conference
end
get '/conference/new' do
js :knockout, :leaf, 'foursquare', 'knockout/new_conference', 'jquery.bootstrap.wizard', :introjs, 'introjs/conference_new'
erb :new_conference
end
#### JSON API ####
get '/api/conference' do
content_type :json
settings.db_conf.getConferences(0, 0).map do |item|
item['_id'] = item['_id'].to_s
item
end.to_json
end
post '/api/conference/new' do
"/conference#id/#{settings.db_conf.putConference(params[:conf])}"
end
get '/api/conference/:id' do
content_type :json
conf = settings.db_conf.getConference(params[:id])
conf['_id'] = conf['_id'].to_s
conf.to_json
end
post '/api/conference/:id' do
content_type :json
settings.db_conf.updateConference(params[:conf])
params[:conf].to_json
end
get '/api/industry' do
content_type :json
settings.db_growth.getIndustryNames.delete_if do |e|
e == 'Total employed, all industries'
end.to_json
end
post '/api/industry/max' do
content_type :json
settings.db_growth.getIndustryMax(params[:industry]).map do |e|
e['_id'] = e['_id'].to_s
e
end.to_json
end
end