Skip to content

Hometask #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Ignore log files
**/*.log
6 changes: 6 additions & 0 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ class TestsController < Simpler::Controller

def index
@time = Time.now
status 200
headers['Content-Type'] = 'text/html'
end

def show
render plain: "Render plain for test with id: #{params[:id]}"
end

def create
Expand Down
2 changes: 2 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require_relative 'config/environment'
require_relative 'middleware/logger'

use AppLogger, logdev: File.expand_path('log/app.log', __dir__)
run Simpler.application
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
Simpler.application.routes do
get '/tests', 'tests#index'
post '/tests', 'tests#create'
get '/tests/:id/', 'tests#show'
end
16 changes: 12 additions & 4 deletions lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ def routes(&block)

def call(env)
route = @router.route_for(env)
controller = route.controller.new(env)
action = route.action

make_response(controller, action)

if route
controller = route.controller.new(env)
action = route.action
make_response(controller, action)
else
response(404, '404. Page not found.')
end
end

private
Expand All @@ -54,5 +58,9 @@ def make_response(controller, action)
controller.make_response(action)
end

def response(status, body)
Rack::Response.new([body], status, {'Content-Type' => 'text/plain'}).finish
end

end
end
19 changes: 16 additions & 3 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def make_response(action)
set_default_headers
send(action)
write_response
params

@response.finish
end
Expand All @@ -43,11 +44,23 @@ def render_body
end

def params
@request.params
@request.env['simpler.params'].merge!(@request.params)
end

def render(template)
@request.env['simpler.template'] = template
def render(**args)
if args[:plain]
headers['Content-Type'] = 'text/plain'
end

@request.env['simpler.template'] = args
end

def status(code)
@response.status = code
end

def headers
@response.headers
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def route_for(env)
method = env['REQUEST_METHOD'].downcase.to_sym
path = env['PATH_INFO']

@routes.find { |route| route.match?(method, path) }
@routes.find { |route| route.match?(method, path, env) }
end

private
Expand Down
26 changes: 23 additions & 3 deletions lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,37 @@ module Simpler
class Router
class Route

attr_reader :controller, :action
attr_reader :controller, :action, :params

def initialize(method, path, controller, action)
@method = method
@path = path
@controller = controller
@action = action
@params = {}
end

def match?(method, path)
@method == method && path.match(@path)
def match?(method, path, env)
@method == method && match_paths(path, env)
end

private

def match_paths(request_path, env)
request_parts = request_path.split('/')
route_parts = @path.split('/')
return false if request_parts.size != route_parts.size

route_parts.each_with_index do |route_part, i|
return false if request_parts[i] != route_part && route_part[0] != ':'

if route_part[0] == ':'
param = route_part[1..].to_sym
@params[param] = request_parts[i].to_i
end
end

env['simpler.params'] = params
end

end
Expand Down
12 changes: 9 additions & 3 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,14 @@ def initialize(env)
end

def render(binding)
template = File.read(template_path)

ERB.new(template).result(binding)
case template
when Hash
template[:plain] if template.has_key?(:plain)
else
template_file = File.read(template_path)

ERB.new(template_file).result(binding)
end
end

private
Expand All @@ -31,6 +36,7 @@ def template

def template_path
path = template || [controller.name, action].join('/')
@env['simpler.template'] = "#{path}.html.erb"

Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
end
Expand Down
22 changes: 22 additions & 0 deletions middleware/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'logger'

class AppLogger

def initialize(app, **options)
@logger = Logger.new(options[:logdev] || STDOUT)
@app = app
end

def call(env)
status, headers, body = @app.call(env)
@logger.info("
Request: #{env["REQUEST_METHOD"]} #{env["REQUEST_URI"]}
Handler: #{env['simpler.handler']}
Parameters: #{env['simpler.params']}
Response: #{status} [#{headers['Content-Type']}] #{env['simpler.view']}
------------------------------------------------------")

[status, headers, body]
end

end