-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathflatterline.rb
235 lines (196 loc) · 6.13 KB
/
flatterline.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
## Required libraries ##
require 'rubygems'
require 'sinatra'
## Global Settings ##
set :public_folder, Proc.new { File.join(root, "_site") }
set :email_username, ENV['SENDGRID_USERNAME']
set :email_password, ENV['SENDGRID_PASSWORD']
set :email_address, 'Flatterline <[email protected]>'
set :email_service, ENV['SENDGRID_ADDRESS']
set :email_domain, ENV['SENDGRID_DOMAIN']
set :protection, :except => :frame_options
## Configuration ##
configure :production do
require 'newrelic_rpm'
end
IP_BLACKLIST = %w(122.255.96.164 91.196.216.20)
## Before callback ##
# Added headers for Varnish
before do
if ENV['RACK_ENV'] == 'production' and request.path != '/blog'
response.headers['Cache-Control'] = 'public, max-age=259200'
end
end
## Error Handling
not_found do
if @not_found.nil?
@not_found = true
send_email params, 'views/error_email_template.txt.erb', "Missing Page"
end
File.read("_site/404.html")
end
error 500..510 do
send_email params, 'views/error_email_template.txt.erb', "Internal Error"
File.read("_site/500.html")
end
## GET requests ##
############################################################
# Redirects
############################################################
# Handle old site URLs with a permanent redirect
get '/index.php/blog/?' do
redirect '/blog', 301
end
get '/index.php/team/?' do
redirect '/team', 301
end
get '/index.php/services/?' do
redirect '/services', 301
end
get "/index.php/*/?" do |title|
redirect "blog/#{title}", 301
end
get '/index.php' do
redirect '/', 301
end
# Redirect trailing slash URLs
get %r{\/(.*)\/$} do |url|
redirect url, 301
end
# Redirects for Ad links
get %r{^\/(ruby|rails|ruby-on-rails|ruby-on-rails-development|ruby-development|rails-development)} do
redirect "/services/ruby-on-rails-development", 301
end
get %r{^\/code-audits?} do
redirect "/services/code-audits", 301
end
############################################################
# GET routes
############################################################
# Index page
get '/' do
page_num = (params[:p] || params[:page_id]).to_i
if page_num == 1
redirect "blog", 301
elsif page_num > 1
redirect "blog/page/#{page_num}", 301
else
File.read("_site/index.html")
end
end
# Dynamic contact form
get '/contact-form' do
@errors = {}
@title = "Ready to start developing? Contact us today."
erb :contact_form
end
# RSS Feed
get '/feed' do
if request.user_agent =~ /FeedBurner/
content_type 'application/atom+xml', :charset => 'utf-8'
File.read("_site/feed/index.xml")
else
redirect 'http://feeds.feedburner.com/flatterline', 301
end
end
get %r{^\/blog\/(feed|feed\/atom|blog\/feed)$} do
redirect 'feed', 301
end
# Blitz.io routes
get '/mu-3dd0acec-2383268f-097a0ad7-4e11727c' do
'42'
end
get '/mu-1b15f325-41eb9c2d-c6972725-c56f1bfb' do
'42'
end
# Catch All
get "/*" do |title|
if request.user_agent == '<?php system("id"); ?>' or IP_BLACKLIST.include?(request.ip)
''
else
File.read("_site/#{title}/index.html") rescue raise Sinatra::NotFound
end
end
############################################################
# POST routes
############################################################
# Dynamic contact form
post '/contact-form/?' do
@title = "Ready to start developing? Contact us today."
if (@errors = validate(params)).empty?
begin
send_email params, 'views/contact_email_template.txt.erb', "Contact form received from #{params[:name]}"
@sent = true
@title = "Thanks!"
rescue => e
puts e
@failure = "Ooops, it looks like something went wrong while attempting to contact us. Mind trying again now or later? :)"
end
end
erb :contact_form
end
## Helper Methods ##
def send_email(params, template, subject)
from = "Flatterline <[email protected]>"
if params[:name] && params[:email]
from = params[:name] + " <" + params[:email] + ">"
end
if settings.email_service.nil?
puts "Sending email from #{from}>:"
puts ERB.new(File.read(template)).result(binding)
else
require 'pony'
Pony.mail(
:from => from,
:to => settings.email_address,
:subject => "[Flatterline] #{subject}",
:body => ERB.new(File.read(template)).result(binding),
:port => '587',
:via => :smtp,
:via_options => {
:address => settings.email_service,
:port => '587',
:enable_starttls_auto => true,
:user_name => settings.email_username,
:password => settings.email_password,
:authentication => :plain,
:domain => settings.email_domain
}
)
puts "Sent email from #{from}>"
end
end
def valid_email?(email)
email =~ /^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/
end
def validate(params)
{}.tap do |errors|
[:name, :email, :message].each do |key|
params[key] = (params[key] || '').strip
errors[key] = "This field is required" if params[key].empty?
end
unless params[:email].empty?
errors[:email] = "Please enter a valid email address" unless valid_email?(params[:email])
end
end
end
__END__
@@ layout
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="/stylesheets/screen.css" media="screen, projection" rel="stylesheet" type="text/css" />
<!--[if IE]>
<link href="/stylesheets/ie.css" media="screen, projection" rel="stylesheet" type="text/css" />
<![endif]-->
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
</head>
<body>
<%= yield %>
</body>
</html>