This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
/
scout.rb
executable file
·186 lines (145 loc) · 3.86 KB
/
scout.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
#!/usr/bin/env ruby
require './config/environment'
require 'sinatra/content_for'
require 'sinatra/flash'
require 'rack/file'
set :logging, false
set :views, 'app/views'
set :public_folder, 'public'
# disable sessions in test environment so it can be manually set
unless test?
use Rack::Session::Cookie,
key: 'rack.session',
path: '/',
expire_after: (60 * 60 * 24 * 30), # 30 days
secret: Environment.config['session_secret']
end
# TODO: seriously?
disable :protection
configure(:development) do |config|
require 'sinatra/reloader'
config.also_reload "./config/environment.rb"
config.also_reload "./config/admin.rb"
config.also_reload "./config/email.rb"
config.also_reload "./config/sms.rb"
config.also_reload "./app/helpers/*.rb"
config.also_reload "./app/models/*.rb"
config.also_reload "./app/controllers/*.rb"
config.also_reload File.join(settings.adapter_path, "*.rb")
config.also_reload "./subscriptions/*.rb"
config.also_reload "./deliveries/*.rb"
end
Dir.glob("./app/controllers/*.rb").each {|filename| load filename}
# log google hits in a database, to understand behavior and performance better
# before do
# @start_time = Time.now
# end
# after do
# Event.google!(env, @start_time) if google? and !Environment.downtime?
# end
# downtime mode. All GETs return downtime.erb,
# all other methods return 500 (since it's our bad).
before do
if Environment.downtime?
if request.get?
halt 200, erb(:downtime)
else
halt 500
end
end
end
# some main routes
get '/' do
erb :index
end
get '/about' do
erb :about
end
get '/collections' do
erb :collections
end
get '/error' do
raise Exception.new("KABOOM.")
end
post '/error' do
raise Exception.new("KABOOM.")
end
# redirector and tracker
# * records the click event and data, redirects user to final URL
#
# recognized params:
# * from - 'email'
# * to - url to redirect to (generated at email render time)
#
# And a 'd' hash that can contain:
# * remote - a remote service name, e.g. "open_states"
# * url_type - 'item'
#
# and if this is to a landing page:
# * item_type - type of landing page
# * item_id - ID of landing item
# * because - 'search' if a search result, 'item' if an item subscription
# * query - for 'search' - search term
get '/url' do
halt 500 unless params[:to].present?
if params[:from] == "email"
Event.email_click! (params[:d] || {}).merge(
to: params[:to],
mobile: mobile?
)
end
redirect params[:to]
end
# custom 404 and 500 handlers
not_found do
erb :"404"
end
error do
exception = env['sinatra.error']
extra = {current_user: (current_user ? current_user.id.to_s : nil)}
# if no Sentry, piece together request details myself
if Environment.config['sentry'].blank?
extra = extra.merge(
method: env['REQUEST_METHOD'],
url: [Environment.config['hostname'], env['REQUEST_URI']].join,
params: params.inspect,
user_agent: env['HTTP_USER_AGENT']
)
end
Admin.exception "Web Handler", exception, extra
erb :"500"
end
helpers do
def pjax?
(request.env['HTTP_X_PJAX'] or params[:_pjax]) ? true : false
end
def logged_in?
!current_user.nil?
end
def google?
request.env['HTTP_USER_AGENT']["Googlebot"] if request.env['HTTP_USER_AGENT']
end
def mobile?
!!request.env['HTTP_USER_AGENT']["Mobi"] if request.env['HTTP_USER_AGENT']
end
def current_user
@current_user ||= (session['user_id'] ? User.find(session['user_id']) : nil)
end
def requires_login(path = '/')
redirect path unless logged_in?
end
# done as the end of an endpoint
def json(code, object)
headers["Content-Type"] = "application/json"
status code
object.to_json
end
def load_user
user_id = params[:user_id].strip
if user = User.where(username: user_id).first
user
else # can be nil
User.find user_id
end
end
end