forked from MLH/mlh-no-light
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathno_light_sinatra.rb
129 lines (107 loc) · 3.33 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
require_relative 'models/submission'
Airbrake.configure do |c|
c.project_id = 141503
c.project_key = '94823b6fc825a7bd16f6fc359d0ac501'
c.logger.level = Logger::DEBUG
c.environment = ENV['RACK_ENV']
c.ignore_environments = %w(test)
c.blacklist_keys = [/password/i]
end
class NoLightSinatra < Sinatra::Base
use Airbrake::Rack::Middleware unless ENV['RACK_ENV'] == 'test'
enable :sessions
set public_folder: 'public', static: true
configure do
DEFAULT_BRANDING = ''
ENVIRONMENTS = {
'development' => { 'uri' => 'mongodb://192.168.0.40/no_light_development' },
'test' => { 'uri' => 'mongodb://192.168.0.40/no_light_test' },
'production' => { 'uri' => ENV['MONGODB_URI'] }
}
MongoMapper.setup(ENVIRONMENTS, ENV['RACK_ENV'])
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, locals: {
title: "Error - Already Submitted",
message: "You have already submitted this code under your name (\"#{@submission.name}\")."
}
else
@submission.save
erb :submitted
end
end
get '/:hackathon.zip' do
@submissions = Submission.by_hackathon(params[:hackathon])
if @submissions.count > 0
create_zip_folder(@submissions)
set_response_headers
download_zip_folder
else
erb :error, locals: {
title: "Error - No Submissions",
message: "We did not receive any submissions for your event (\"#{params[:hackathon]}\")."
}
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] || {}
name = (0...8).map { (65 + rand(26)).chr }.join
get_submit_params.merge({
'seconds' => seconds_from(params[:submission][:seconds]),
'name' => name
})
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