Skip to content

Commit 0e8e8ee

Browse files
committed
homework
1 parent c3d0a78 commit 0e8e8ee

File tree

10 files changed

+344
-6
lines changed

10 files changed

+344
-6
lines changed

app/controllers/tests_controller.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
class TestsController < Simpler::Controller
2-
32
def index
43
@time = Time.now
54
end
65

7-
def create
6+
def create; end
87

8+
def show
9+
status(201)
10+
@test = Test.first(id: params['id'])
911
end
10-
1112
end

app/views/tests/show.html.erb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>Show | Simpler application</title>
6+
</head>
7+
<body>
8+
<h1>Simpler framework at work!!</h1>
9+
10+
<p>ID - <%= @test.id %></p>
11+
<p>Title - <%= @test.title %></p>
12+
<p>Level - <%= @test.level %></p>
13+
</body>
14+
</html>

config.ru

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
require_relative 'config/environment'
2+
require_relative 'middleware/logger'
23

4+
use Logger
35
run Simpler.application

config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
Simpler.application.routes do
22
get '/tests', 'tests#index'
33
post '/tests', 'tests#create'
4+
get '/tests/:id', 'tests#show'
45
end

lib/simpler/application.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,9 @@ def routes(&block)
2828

2929
def call(env)
3030
route = @router.route_for(env)
31+
return @router.not_found(env) unless route
32+
33+
route.extract_params(env)
3134
controller = route.controller.new(env)
3235
action = route.action
3336

lib/simpler/controller.rb

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ def initialize(env)
99
@name = extract_name
1010
@request = Rack::Request.new(env)
1111
@response = Rack::Response.new
12+
@request.params.merge!(env['params'])
13+
@rendered = false
1214
end
1315

1416
def make_response(action)
@@ -36,19 +38,62 @@ def write_response
3638
body = render_body
3739

3840
@response.write(body)
41+
@rendered = true
42+
end
43+
44+
def set_content_type(content_type)
45+
headers['Content-Type'] = content_type
3946
end
4047

4148
def render_body
4249
View.new(@request.env).render(binding)
4350
end
4451

52+
def render_string(template)
53+
@request.env['simpler.template'] = template
54+
end
55+
56+
def render_error(error_message)
57+
status 500
58+
@response.write(error_message)
59+
@response.finish
60+
end
61+
62+
def render_hash(params)
63+
if params.key?(:plain)
64+
render_plain(hash[:plain])
65+
else
66+
render_error("Cannot render template with #{params.keys} options")
67+
end
68+
end
69+
70+
def render_plain(text)
71+
set_content_type('text/plain')
72+
@response.write(text)
73+
end
74+
75+
def status(status)
76+
response.status = status
77+
end
78+
79+
def headers
80+
@response.headers
81+
end
82+
4583
def params
4684
@request.params
4785
end
4886

4987
def render(template)
50-
@request.env['simpler.template'] = template
88+
case template
89+
when String
90+
render_string(template)
91+
when Hash
92+
render_hash(template)
93+
else
94+
render_error("Cannot render #{template.class} template")
95+
end
96+
@rendered = true
5197
end
52-
5398
end
5499
end

lib/simpler/router.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ def route_for(env)
2222
@routes.find { |route| route.match?(method, path) }
2323
end
2424

25+
def not_found(env)
26+
method = env['REQUEST_METHOD'].downcase.to_sym
27+
path = env['PATH_INFO']
28+
Rack::Response.new.then do |response|
29+
response.status = 404
30+
response.write("[#{method} #{path}] route not found")
31+
response.finish
32+
end
33+
end
34+
2535
private
2636

2737
def add_route(method, path, route_point)
@@ -36,6 +46,5 @@ def add_route(method, path, route_point)
3646
def controller_from_string(controller_name)
3747
Object.const_get("#{controller_name.capitalize}Controller")
3848
end
39-
4049
end
4150
end

lib/simpler/router/route.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,26 @@ def initialize(method, path, controller, action)
1313

1414
def match?(method, path)
1515
@method == method && path.match(@path)
16+
@method == method && path.match(to_regex(@path))
1617
end
1718

19+
def extract_params(env)
20+
request_path_items = env['PATH_INFO'].split('/')
21+
path_items = @path.split('/').map { |item| item.sub(':', '') }
22+
23+
params = Hash[path_items.zip(request_path_items)].delete_if { |k, v| k == v }
24+
env['params'] = params
25+
end
26+
27+
private
28+
29+
def to_regex(path)
30+
path.split('/')
31+
.map { |string| string.start_with?(':') ? '\d+' : string }
32+
.join('/')
33+
.concat('$')
34+
.then { |regex| Regexp.new regex }
35+
end
1836
end
1937
end
2038
end

log/app.log

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
2+
Request: GET
3+
Handler: TestsController#index
4+
Parameters: {}
5+
Response: 200 [text/html]
6+
7+
Request: GET
8+
Handler: TestsController#index
9+
Parameters: {}
10+
Response: 200 [text/html]
11+
12+
Request: GET
13+
Handler: TestsController#index
14+
Parameters: {}
15+
Response: 200 [text/html]
16+
17+
Request: GET
18+
Handler: TestsController#index
19+
Parameters: {}
20+
Response: 200 [text/html]
21+
22+
Request: GET
23+
Handler: TestsController#index
24+
Parameters: {}
25+
Response: 200 [text/html]
26+
27+
Request: GET
28+
Handler: NilClass#
29+
Parameters:
30+
Response: 404 []
31+
32+
Request: GET
33+
Handler: TestsController#index
34+
Parameters: {}
35+
Response: 200 [text/html]
36+
37+
Request: GET
38+
Handler: NilClass#
39+
Parameters:
40+
Response: 404 []
41+
42+
Request: GET
43+
Handler: TestsController#show
44+
Parameters: {"id"=>"2"}
45+
Response: 201 [text/html]
46+
47+
Request: GET
48+
Handler: TestsController#show
49+
Parameters: {"id"=>"2"}
50+
Response: 201 [text/html]
51+
52+
Request: GET
53+
Handler: TestsController#show
54+
Parameters: {"id"=>"2"}
55+
Response: 201 [text/html]
56+
57+
Request: GET
58+
Handler: TestsController#show
59+
Parameters: {"id"=>"2"}
60+
Response: 201 [text/html]
61+
62+
Request: GET
63+
Handler: TestsController#index
64+
Parameters: {}
65+
Response: 200 [text/html]
66+
67+
Request: GET
68+
Handler: TestsController#show
69+
Parameters: {"id"=>"1"}
70+
Response: 201 [text/html]
71+
72+
Request: GET
73+
Handler: NilClass#
74+
Parameters:
75+
Response: 404 []
76+
77+
Request: GET
78+
Handler: NilClass#
79+
Parameters:
80+
Response: 404 []
81+
82+
Request: GET
83+
Handler: TestsController#show
84+
Parameters: {"id"=>"1"}
85+
Response: 201 [text/html]
86+
87+
Request: GET
88+
Handler: NilClass#
89+
Parameters:
90+
Response: 404 []
91+
92+
Request: GET
93+
Handler: NilClass#
94+
Parameters:
95+
Response: 404 []
96+
97+
Request: GET
98+
Handler: TestsController#index
99+
Parameters: {}
100+
Response: 200 [text/html]
101+
102+
Request: GET
103+
Handler: NilClass#
104+
Parameters:
105+
Response: 404 []
106+
107+
Request: GET
108+
Handler: NilClass#
109+
Parameters:
110+
Response: 404 []
111+
112+
Request: GET
113+
Handler: TestsController#index
114+
Parameters: {}
115+
Response: 200 [text/html]
116+
117+
Request: GET
118+
Handler: TestsController#index
119+
Parameters: {}
120+
Response: 200 [text/html]
121+
122+
Request: GET
123+
Handler: TestsController#show
124+
Parameters: {"id"=>"1"}
125+
Response: 201 [text/html]
126+
127+
Request: GET
128+
Handler: TestsController#show
129+
Parameters: {"id"=>"1"}
130+
Response: 201 [text/html]
131+
132+
Request: GET
133+
Handler: TestsController#show
134+
Parameters: {"id"=>"1"}
135+
Response: 201 [text/html]
136+
137+
Request: GET
138+
Handler: TestsController#index
139+
Parameters: {}
140+
Response: 200 [text/html]
141+
142+
Request: GET
143+
Handler: TestsController#index
144+
Parameters: {}
145+
Response: 200 [text/html]
146+
147+
Request: GET
148+
Handler: NilClass#
149+
Parameters:
150+
Response: 404 []
151+
152+
Request: GET
153+
Handler: TestsController#index
154+
Parameters: {}
155+
Response: 200 [text/html]
156+
157+
Request: GET
158+
Handler: TestsController#index
159+
Parameters: {}
160+
Response: 200 [text/html]
161+
162+
Request: GET
163+
Handler: NilClass#
164+
Parameters:
165+
Response: 404 []
166+
167+
Request: GET
168+
Handler: NilClass#
169+
Parameters:
170+
Response: 404 []
171+
172+
Request: GET
173+
Handler: TestsController#index
174+
Parameters: {}
175+
Response: 200 [text/html]
176+
177+
Request: GET
178+
Handler: NilClass#
179+
Parameters:
180+
Response: 404 []
181+
182+
Request: GET
183+
Handler: NilClass#
184+
Parameters:
185+
Response: 404 []
186+
187+
Request: GET
188+
Handler: TestsController#index
189+
Parameters: {}
190+
Response: 200 [text/html]
191+
192+
Request: GET
193+
Handler: TestsController#index
194+
Parameters: {}
195+
Response: 200 [text/html]
196+
197+
Request: GET
198+
Handler: TestsController#show
199+
Parameters: {"id"=>"1"}
200+
Response: 201 [text/html]
201+
202+
Request: GET
203+
Handler: TestsController#show
204+
Parameters: {"id"=>"1"}
205+
Response: 201 [text/html]
206+
207+
Request: GET
208+
Handler: TestsController#show
209+
Parameters: {"id"=>"1"}
210+
Response: 201 [text/html]
211+

0 commit comments

Comments
 (0)