-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathapp.rb
199 lines (163 loc) · 4.22 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
require 'sinatra'
require 'haml'
require 'v8'
require 'json'
require 'coffee-script'
require 'carQuote'
require 'lifeQuote'
require 'carPremiumCalculator'
require 'lifePremiumCalculator'
require 'emailValidator'
require 'rack-google-analytics'
require 'logger'
require "warden"
require 'data_mapper'
require 'bcrypt'
DataMapper::Logger.new($stdout, :debug)
DataMapper.setup(:default, ENV['DATABASE_URL'] || "sqlite3://#{Dir.pwd}/db/db.sqlite")
require './models/policies'
require './models/users'
DataMapper.finalize
DataMapper.auto_migrate!
use Rack::GoogleAnalytics, :tracker => 'UA-53462613-1'
configure :production do
require 'newrelic_rpm'
end
class App < Sinatra::Base
logger = Logger.new(STDERR)
emailValidator = EmailValidator.new()
#configure do
# Dir.mkdir('log') unless File.exists?('log')
# enable :logging
# log_file = File.new('log/access.log', 'a+')
# log_file.sync = true
# use Rack::CommonLogger, log_file
#end
use Rack::Session::Cookie, :key => 'rack.session',
:path => '/',
:secret => 'secret_stuff'
use Warden::Manager do |config|
config.serialize_into_session{|user| user.id }
config.serialize_from_session{|id| User.get(id) }
config.scope_defaults :default,
strategies: [:password],
action: 'auth/unauthenticated'
config.failure_app = self
end
Warden::Manager.before_failure do |env,opts|
env['REQUEST_METHOD'] = 'POST'
end
Warden::Strategies.add(:password) do
def valid?
params['user']['username'] && params['user']['password']
end
def authenticate!
user = User.first(username: params['user']['username'])
if user.nil?
fail!("The username you entered does not exist.")
elsif user.authenticate(params['user']['password'])
success!(user)
else
fail!("Could not log in")
end
end
end
get '/' do
session["quote"] ||= nil
haml :index
end
get '/seed' do
createUser("[email protected]", "hans", "Hans", "ADMIN")
createUser("[email protected]", "hans", "Hans RD", "ADMIN")
redirect '/users'
end
get "/bad" do
content_type :json
{ :login_successful => "false" }.to_json
end
get "/good" do
content_type :json
{ :login_successful => "true", :name => "Hans"}.to_json
end
get '/landing' do
session["quote"] ||= nil
haml :landing
end
get '/life' do
sleep 0.5
haml :life
end
get '/car' do
haml :car
end
get '/payment' do
@quote = session["quote"]
haml :payment
end
post '/landing' do
haml :landing
end
get '/users' do
@users = User.all
haml :users
end
get '/policies' do
@policies = Policy.all
haml :policies
end
post '/pay' do
#logger.info params
@quote = session["quote"]
createUser(params["username"], params["password"], params["cardholdername"], "USER")
# Create insurance policy in DB
haml :done
end
post '/quote' do
#logger.info params
type = params["typeOfInsurance"]
if type == "life"
@quote = getLifeQuote(params)
else
@quote = getCarQuote(params)
end
session["quote"] = @quote
haml :quote
end
post '/checkemail' do
#logger.info params
email = params["email"]
content_type :json
{:valid => emailValidator.isEmailValid?(email)}.to_json
end
get '/javascripts/application.js' do
coffee :application
end
not_found do
haml :not_found
end
def getLifeQuote(params)
age = params["age"]
email = params["email"]
occupationCategory = params["occupation"]
gender = params["gender"]
state = params["state"]
LifeQuote.new(age, email, state, occupationCategory, gender)
end
def getCarQuote(params)
age = params["age"]
email = params["email"]
make = params["make"]
year = params["year"]
gender = params["gender"]
state = params["state"]
CarQuote.new(age, email, state, make, gender, year)
end
def createUser(username, password, name, role)
if !User.get(:username => username)
User.create(:role=>role, :username=>username, :password=>password, :name => name)
end
end
def createPolicy(quote, user)
# Create Policy in DB
end
end