This repository has been archived by the owner on Mar 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
coming_soon.rb
223 lines (189 loc) · 5.94 KB
/
coming_soon.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
require 'rubygems'
require 'sinatra/base'
require 'haml'
require 'sass'
require 'compass'
require 'split'
require 'hominid'
require 'feedzirra'
require 'hoptoad_notifier'
require 'uri'
require 'csv'
CSVKLASS = CSV
def load_configuration(file, name)
if !File.exist?(file)
puts "There's no configuration file at #{file}!"
exit!
end
const_set(name, YAML.load_file(file))
end
class SplitGroup
attr_accessor :splits
def initialize
@splits = []
end
def add(test_name)
@splits << test_name
test_name
end
end
class ComingSoon < Sinatra::Base
enable :sessions
helpers Split::Helper
configure do
load_configuration('config/config.yml', 'APP_CONFIG')
if APP_CONFIG['hoptoad_api_key']
HoptoadNotifier.configure do |config|
config.api_key = APP_CONFIG['hoptoad_api_key']
end
use HoptoadNotifier::Rack
enable :raise_errors
end
DataMapper.setup(:default, {
:adapter => 'redis',
:user => APP_CONFIG['redis']['user'],
:password => APP_CONFIG['redis']['password'],
:host => APP_CONFIG['redis']['host'],
:port => APP_CONFIG['redis']['port']
})
user = APP_CONFIG['redis']['user']
password = APP_CONFIG['redis']['password']
host = APP_CONFIG['redis']['host']
port = APP_CONFIG['redis']['port']
Split.redis = "redis://#{user}:#{password}@#{host}:#{port}/"
redis_app_name = APP_CONFIG['app_name'].gsub(' ', '_').downcase
Split.redis.namespace = "split:soonatra:#{redis_app_name}"
Compass.configuration do |config|
config.project_path = File.dirname(__FILE__)
config.css_dir = 'tmp/stylesheets'
config.sass_dir = 'views'
config.http_path = '/'
end
set :haml, { :format => :html5 }
set :sass, Compass.sass_engine_options
set :root, File.dirname(__FILE__)
end
before do
@app_name = APP_CONFIG['app_name']
@splits = SplitGroup.new
File.read('views/themes/_current_theme.sass').scan(/@import themes\/(.*)/)
@app_theme = $1
expires 60, :public, :'no-cache'
end
get '/stylesheets/:name.css' do
content_type 'text/css', :charset => 'utf-8'
sass(:"#{params[:name]}")
end
get '/' do
protected! if APP_CONFIG['http_basic']
flash_message(params[:m])
@concepts = APP_CONFIG['concepts']
@blog_posts = FeedzirraRedis::Entry.last(2)
@features = APP_CONFIG['features']
@intro = APP_CONFIG['intro']
@signup = APP_CONFIG['signup']
haml :index
end
post '/create' do
protected! if APP_CONFIG['http_basic']
@splits.splits.each { |test| finished(test) }
if params[:email].blank?
redirect '/?m=email_blank'
elsif params[:email].match /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
chimp = Hominid::API.new( APP_CONFIG['mailchimp']['api_key'] )
list = chimp.find_list_by_name(APP_CONFIG['mailchimp']['list_name'])
chimp.list_subscribe(list['id'], params[:email])
redirect '/?m=success'
else
redirect '/?m=email_invalid'
end
end
helpers do
def flash_message(message)
case message
when 'email_blank'
@notice = APP_CONFIG['flash_messages']['email_blank']
when 'email_invalid'
@notice = APP_CONFIG['flash_messages']['email_invalid']
when 'success'
@success = APP_CONFIG['flash_messages']['email_success']
else ''
end
end
def pluralize(count, singular, plural = nil)
"#{count || 0} " + ((count == 1 || count =~ /^1(\.0+)?$/) ? singular : (plural || singular.pluralize))
end
def testing?
ENV['RACK_ENV'] == 'test'
end
def protected!
unless authorized?
response['WWW-Authenticate'] = %(Basic realm="Authentication Required")
throw(:halt, [401, "Not authorized\n"])
end
end
def authorized?
return true if testing?
@auth ||= Rack::Auth::Basic::Request.new(request.env)
@auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == [APP_CONFIG['admin_username'], APP_CONFIG['admin_password']]
end
def email_signup(content_for)
case content_for
when :headline
ab_test @splits.add('Email Signup Headline'), *APP_CONFIG['email_signup']['headlines']
when :intro
ab_test @splits.add('Email Signup Intro'), *APP_CONFIG['email_signup']['intros']
when :call_to_action
ab_test @splits.add('Email Signup Call To Action'), *APP_CONFIG['email_signup']['call_to_actions']
when :image_button
APP_CONFIG['email_signup']['image_button']
end
end
def features(content_for)
case content_for
when :headline
ab_test @splits.add('Features Headline'), *APP_CONFIG['features']['headlines']
when :items
APP_CONFIG['features']['items']
when :empty
APP_CONFIG['features']['empty']
end
end
def feedback(content_for)
case content_for
when :headline
ab_test @splits.add('Feedback Headline'), *APP_CONFIG['feedback']['headlines']
when :intro
ab_test @splits.add('Feedback Intro'), *APP_CONFIG['feedback']['intros']
when :call_to_action
ab_test @splits.add('Feedback Call To Action'), *APP_CONFIG['feedback']['call_to_actions']
when :concepts
APP_CONFIG['feedback']['concepts']
when :empty
APP_CONFIG['feedback']['empty']
end
end
def blog(content_for)
case content_for
when :headline
ab_test @splits.add('Blog Headline'), *APP_CONFIG['blog']['headlines']
when :empty
APP_CONFIG['blog']['empty']
end
end
def footer(content_for)
case content_for
when :copyright
APP_CONFIG['footer']['copyright']
when :network
APP_CONFIG['footer']['network']
end
end
# ex: class => cycle(:even, :odd)
def cycle(*args)
@cycle_index ||= -1 # to start in 0
@cycle_index = (@cycle_index >= args.size-1) ? 0 : @cycle_index + 1
args[@cycle_index].to_s
end
end
end