-
Notifications
You must be signed in to change notification settings - Fork 55
/
example.rb
53 lines (44 loc) · 859 Bytes
/
example.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
until defined? Sinatra
puts "What Sinatra implementation should I use?",
"[R]eal Sinatra", "[A]lmost Sinatra"
print ">> "
case gets.strip.downcase
when 'a' then require_relative "almost_sinatra"
when 'r' then require 'sinatra'
else puts "invalid input!!"
end
end
configure do
enable :sessions
end
helpers do
def inc_counter
session[:counter] ||= 0
session[:counter] += 1
end
end
before do
puts " yay! got a request ".center(80, "=")
end
get '/' do
@title = "Almost Sinatra"
haml :index
end
# /hello?name=world
get '/hello' do
erb :hello, locals: { name: params[:name] }
end
get '/counter' do
inc_counter
session[:counter].to_s
end
__END__
@@ index
%html
%head
%title= @title
%body
%a{:href => '/hello?name=World'} Say hello!
%a{:href => '/counter'} Show Counter
@@ hello
Hello <%= name %>!