forked from MLH/mlh-no-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_light_sinatra.rb
113 lines (92 loc) · 2.81 KB
/
no_light_sinatra.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
require_relative 'models/submission'
class NoLightSinatra < Sinatra::Base
set public_folder: 'public', static: true
use Airbrake::Sinatra
configure do
DEFAULT_BRANDING = 'dell'
ENVIRONMENTS = {
'development' => { 'uri' => 'mongodb://localhost/no_light_development' },
'test' => { 'uri' => 'mongodb://localhost/no_light_test' },
'production' => { 'uri' => ENV['MONGODB_URI'] }
}
MongoMapper.setup(ENVIRONMENTS, ENV['RACK_ENV'])
Airbrake.configure do |c|
c.project_id = 141503
c.project_key = '94823b6fc825a7bd16f6fc359d0ac501'
c.logger = NoLightSinatra.logger
c.ignore_environments = %w(test)
c.blacklist_keys = [/password/i]
end
end
configure :production do
require "skylight/sinatra"
Skylight.start!
end
get '/' do
erb :default_page
end
post '/submit' do
@submission = Submission.new(get_submit_params)
if @submission.already_exists?
erb :error
else
@submission.save
erb :submitted
end
end
get '/:hackathon.zip' do
if @submissions = Submission.by_hackathon(params[:hackathon])
create_zip_folder(@submissions)
set_response_headers
download_zip_folder
end
end
get '/:hackathon' do
show_editor(DEFAULT_BRANDING)
end
get '/:hackathon/:branding' do
show_editor(params[:branding])
end
private
def show_editor(custom_branding)
@body_class = ['editor', custom_branding].join(' ')
erb :editor
end
def create_tempfile
@create_tempfile ||= Tempfile.new(get_hackathon)
end
def get_submit_params
get_submit_params = params[:submission] || {}
get_submit_params.merge('seconds' => seconds_from(params[:submission][:seconds]))
end
def get_hackathon
params[:hackathon].to_s.downcase
end
def seconds_from(time_to_compare)
(Time.now - Time.parse(time_to_compare)).to_i
end
def create_zip_folder(array = [])
Zippy.create(create_tempfile.path) do |zip|
if zip && block_given?
yield(zip)
else
array.to_a.each do |entry|
zip[entry.filename] = entry.html
end
end
end
end
def download_zip_folder
File.read(create_tempfile.path)
end
def set_response_headers
response.headers['Pragma'] = 'public'
response.headers['Expires'] = '0'
response.headers['Cache-Control'] = 'must-revalidate, post-check=0, pre-check=0'
response.headers['Cache-Control'] = 'public'
response.headers['Content-Type'] = 'application/octet-stream'
response.headers['Content-Disposition'] = 'attachment; filename="' + get_hackathon + '.zip"'
response.headers['Content-Transfer-Encoding'] = 'binary'
response.headers['Content-Length'] = create_tempfile.size
end
end