-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsinatra_app.rb
81 lines (65 loc) · 1.2 KB
/
sinatra_app.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
require 'thin'
require 'sinatra'
require 'json'
require 'ox'
get '/simple' do
erb :simple
end
def safe_string body
if body.respond_to?(:string)
body.string
else
body.to_s
end
end
post '/json' do
@obj = JSON.parse(safe_string(request.body))
erb :json
end
post '/xml' do
@obj = Ox.parse(safe_string(request.body))
erb :xml
end
post '/text' do
@text = safe_string(request.body)
erb :text
end
post '/login' do
puts "REQUEST=#{ request.params }"
if request.params['user'] == 'alice' && request.params['pass'] == '$ecur3'
redirect '/sinatra/simple'
else
status 401
body 'that is an incorrect password'
end
end
__END__
@@ simple
<h1>Received the following params</h1>
<% params.each do |k,v| %>
<div><b><%= k %></b>: <%= v %></div>
<% end %>
@@ json
<h1>Received the following JSON</h1>
<div>
<%= @obj.inspect %>
</div>
@@ xml
<h1>Received the following XML</h1>
<div>
<%= @obj.inspect %>
</div>
@@ text
<h1>Received the following TEXT</h1>
<p>
<%= @text %>
</p>
@@ layout
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple Sinatra App</title>
</head>
<body><%= yield %></body>
</html>