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 )
@@ -133,20 +114,33 @@ def self.call(_req)
133114
134115class FortunesHandler < BaseHandler
135116 def self . call ( _req )
136- fortunes = $pool. with do |conn |
117+ f_1 = $pool. with do |conn |
137118 conn . exec_prepared ( 'select_fortune' , [ ] )
138- end . map ( &:to_h )
139-
140- 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>"
147119 end
148- buffer << TEMPLATE_POSTFIX
149- html_response ( buffer )
120+
121+ f_2 = f_1 . map ( &:to_h ) .
122+ append ( { 'id' => '0' , 'message' => 'Additional fortune added at request time.' } ) .
123+ sort_by { |item | item [ 'message' ] } .
124+ map { |f | "<tr><td>#{ f [ 'id' ] } </td><td>#{ ERB ::Escape . html_escape ( f [ 'message' ] ) } </td></tr>" } .
125+ join
126+
127+ html_response ( <<-HTML )
128+ <!DOCTYPE html>
129+ < html >
130+ < head >
131+ < title > Fortunes</ title >
132+ </ head >
133+ < body >
134+ < table >
135+ < tr >
136+ < th > id</ th >
137+ < th > message</ th >
138+ </ tr >
139+ #{ f_2 }
140+ </ table>
141+ </ body>
142+ </ html>
143+ HTML
150144 end
151145end
152146
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,37 @@ 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+
66+ < body >
67+
68+ < table >
69+ < tr >
70+ < th > id</ th >
71+ < th > message</ th >
72+ </ tr >
73+ HTML
74+
75+ fortunes . each do |fortune |
76+ html << <<~"HTML"
77+ < tr >
78+ < td > #{ fortune . id } </ td>
79+ < td > #{ ERB ::Escape . html_escape ( fortune . message ) } </ td>
80+ </ tr>
81+ HTML
8382 end
84- buffer << TEMPLATE_POSTFIX
83+
84+ html << <<~'HTML'
85+ </ table>
86+
87+ </ body>
88+ </ html>
89+ HTML
8590 end
8691
8792 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