-
Notifications
You must be signed in to change notification settings - Fork 1
/
application.rb
48 lines (43 loc) · 1.42 KB
/
application.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
require 'rubygems'
require 'bundler'
require 'pg'
require 'rack'
require 'rack/contrib'
require 'json'
Bundler.require :default, (ENV['RACK_ENV'] || 'development').to_sym
# Basic Sinatra app that takes posts to /segment and inserts them in a PG DB
class Application < Sinatra::Base
configure :production, :development do
enable :logging
end
def initialize
uri = URI.parse(ENV['DATABASE_URL'])
begin
@db = PG.connect(uri.hostname, uri.port, nil, nil, uri.path[1..-1], uri.user, uri.password)
rescue
puts 'Problem connecting to Postgres. Exiting.'
exit
end
super
end
post '/' do
if params[:type] == 'track'
begin
@db.exec("INSERT INTO events ( \
event_name, \
occurred_at, \
user_id, \
details \
) VALUES ( \
'#{params[:event]}', \
'#{params[:timestamp]}', \
'#{params[:userId]}', \
'#{params[:properties].to_json}' \
)")
rescue PG::Error => err
logger.error "Problem with (#{params[:event]}) @#{params[:timestamp]}"
logger.error err.message
end
end
end
end