File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 3030HTML_TYPE = 'text/html; charset=utf-8'
3131PLAINTEXT_TYPE = 'text/plain'
3232
33- TEMPLATE_PREFIX = <<~HTML
34- <!DOCTYPE html>
35- < html >
36- < head >
37- < title > Fortunes</ title >
38- </ head >
39- < body >
40- < table >
41- < tr >
42- < th > id</ th >
43- < th > message</ th >
44- </ tr >
45- HTML
46-
47- TEMPLATE_POSTFIX = <<~HTML
48- </ table>
49- </ body>
50- </ html>
51- HTML
5233
5334class BaseHandler
5435 def self . extract_queries_param ( request = nil )
@@ -130,23 +111,33 @@ def self.call(_req)
130111 end
131112end
132113
133-
134114class FortunesHandler < BaseHandler
135115 def self . call ( _req )
136116 fortunes = $pool. with do |conn |
137117 conn . exec_prepared ( 'select_fortune' , [ ] )
138118 end . map ( &:to_h )
139-
140119 fortunes << { 'id' => 0 , 'message' => 'Additional fortune added at request time.' }
141- fortunes . sort_by! { |item | item [ 'message' ] }
142-
143- buffer = String . new
144- buffer << TEMPLATE_PREFIX
145- fortunes . each do |item |
146- buffer << "<tr><td>#{ item [ 'id' ] } </td><td>#{ ERB ::Escape . html_escape ( item [ 'message' ] ) } </td></tr>"
147- end
148- buffer << TEMPLATE_POSTFIX
149- html_response ( buffer )
120+ fortunes . sort_by! { |fortune | fortune [ 'message' ] }
121+
122+ rows = fortunes . map { |fortune | "<tr><td>#{ fortune [ 'id' ] } </td><td>#{ ERB ::Escape . html_escape ( fortune [ 'message' ] ) } </td></tr>" } . join
123+
124+ html_response ( <<-HTML )
125+ <!DOCTYPE html>
126+ < html >
127+ < head >
128+ < title > Fortunes</ title >
129+ </ head >
130+ < body >
131+ < table >
132+ < tr >
133+ < th > id</ th >
134+ < th > message</ th >
135+ </ tr >
136+ #{ rows }
137+ </ table>
138+ </ body>
139+ </ html>
140+ HTML
150141 end
151142end
152143
Original file line number Diff line number Diff line change @@ -19,27 +19,7 @@ class HelloWorld
1919 PLAINTEXT_TYPE = 'text/plain'
2020 DATE = 'Date'
2121 SERVER = 'Server'
22- SERVER_STRING = 'Rack'
23-
24- TEMPLATE_PREFIX = <<~HTML
25- <!DOCTYPE html>
26- < html >
27- < head >
28- < title > Fortunes</ title >
29- </ head >
30- < body >
31- < table >
32- < tr >
33- < th > id</ th >
34- < th > message</ th >
35- </ tr >
36- HTML
37-
38- TEMPLATE_POSTFIX = <<~HTML
39- </ table>
40- </ body>
41- </ html>
42- HTML
22+ SERVER_STRING = "Rack"
4323
4424 def bounded_queries ( env )
4525 params = Rack ::Utils . parse_query ( env [ 'QUERY_STRING' ] )
@@ -76,12 +56,34 @@ def fortunes
7656
7757 fortunes . sort_by! ( &:message )
7858
79- buffer = String . new
80- buffer << TEMPLATE_PREFIX
81- fortunes . each do |item |
82- buffer << "<tr><td>#{ item . id } </td><td>#{ ERB ::Escape . html_escape ( item . message ) } </td></tr>"
59+ html = String . new ( <<~'HTML' )
60+ <!DOCTYPE html>
61+ < html >
62+ < head >
63+ < title > Fortunes</ title >
64+ </ head >
65+ < body >
66+ < table >
67+ < tr >
68+ < th > id</ th >
69+ < th > message</ th >
70+ </ tr >
71+ HTML
72+
73+ fortunes . each do |fortune |
74+ html << <<~"HTML"
75+ < tr >
76+ < td > #{ fortune . id } </ td>
77+ < td > #{ ERB ::Escape . html_escape ( fortune . message ) } </ td>
78+ </ tr>
79+ HTML
8380 end
84- buffer << TEMPLATE_POSTFIX
81+
82+ html << <<~'HTML'
83+ </ table>
84+ </ body>
85+ </ html>
86+ HTML
8587 end
8688
8789 def updates ( env )
Original file line number Diff line number Diff line change @@ -29,24 +29,6 @@ class HelloWorld
2929 DATE = 'Date'
3030 SERVER = 'Server'
3131 SERVER_STRING = 'Rack'
32- TEMPLATE_PREFIX = <<~HTML
33- <!DOCTYPE html>
34- < html >
35- < head >
36- < title > Fortunes</ title >
37- </ head >
38- < body >
39- < table >
40- < tr >
41- < th > id</ th >
42- < th > message</ th >
43- </ tr >
44- HTML
45- TEMPLATE_POSTFIX = <<~HTML
46- </ table>
47- </ body>
48- </ html>
49- HTML
5032
5133 def call ( env )
5234 case env [ 'PATH_INFO' ]
@@ -107,12 +89,31 @@ def fortunes
10789 fortunes << { 'id' => 0 , 'message' => 'Additional fortune added at request time.' }
10890 fortunes . sort_by! { |item | item [ 'message' ] }
10991
110- buffer = String . new
111- buffer << TEMPLATE_PREFIX
92+ html = String . new ( <<~'HTML' )
93+ <!DOCTYPE html>
94+ < html >
95+ < head >
96+ < title > Fortunes</ title >
97+ </ head >
98+
99+ < body >
100+
101+ < table >
102+ < tr >
103+ < th > id</ th >
104+ < th > message</ th >
105+ </ tr >
106+ HTML
107+
112108 fortunes . each do |item |
113- buffer << "<tr><td>#{ item [ 'id' ] } </td><td>#{ ERB ::Escape . html_escape ( item [ 'message' ] ) } </td></tr>"
109+ html << "<tr><td>#{ item [ 'id' ] } </td><td>#{ ERB ::Escape . html_escape ( item [ 'message' ] ) } </td></tr>"
114110 end
115- buffer << TEMPLATE_POSTFIX
111+
112+ html << <<~'HTML'
113+ </ table>
114+ </ body>
115+ </ html>
116+ HTML
116117 end
117118
118119 def update_worlds ( count )
You can’t perform that action at this time.
0 commit comments