Skip to content

test task completion #144

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 1 commit 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
Binary file added .DS_Store
Binary file not shown.
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 201
headers['Content-Type'] = 'text/plain'
end

def show
Test.find_by(params[:id])
end

def create
Expand Down
17 changes: 16 additions & 1 deletion lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
module Simpler
class Controller

attr_reader :name, :request, :response
attr_reader :name, :request, :response, :headers
attr_writer :headers

def initialize(env)
@name = extract_name
@request = Rack::Request.new(env)
@response = Rack::Response.new
@headers = @response
end

def make_response(action)
Expand All @@ -32,6 +34,14 @@ def set_default_headers
@response['Content-Type'] = 'text/html'
end

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

def set_headers(template)
@response['Content-Type'] = "text/#{template.first[0]}"
end

def write_response
body = render_body

Expand All @@ -47,6 +57,11 @@ def params
end

def render(template)
if template.is_a(Hash)
set_headers(template)
else
set_default_headers
end
@request.env['simpler.template'] = template
end

Expand Down
11 changes: 10 additions & 1 deletion lib/simpler/router.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Router

def initialize
@routes = []
@params = {}
end

def get(path, route_point)
Expand All @@ -19,7 +20,12 @@ def route_for(env)
method = env['REQUEST_METHOD'].downcase.to_sym
path = env['PATH_INFO']

@routes.find { |route| route.match?(method, path) }
route = @routes.find { |route| route.match?(method, path) }
if route == nil
raise "404"
else
route
end
end

private
Expand All @@ -30,6 +36,9 @@ def add_route(method, path, route_point)
action = route_point[1]
route = Route.new(method, path, controller, action)

path = path.split('/')
Controller.params[:id] = path.last

@routes.push(route)
end

Expand Down
11 changes: 8 additions & 3 deletions lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ class View

def initialize(env)
@env = env
@templates = { plain: Hash.new { |hash, value| hash[value] = ERB.new(value) } }
end

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

ERB.new(template).result(binding)
template = @env['simpler.template'].to_a.flatten
if @templates[template[0]]
@templates[template[0]][template[1]].result(binding)
else
template = File.read(template_path)
ERB.new(template).result(binding)
end
end

private
Expand Down
Empty file added log/app.log
Empty file.
15 changes: 15 additions & 0 deletions middleware/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require_relative 'logger'

class App
def initialize # - сюда нужно передать app, не понял как это сделать
@logger = Logger.new
end

def call(env)
req = Rack::Request.new(env)
status, headers, response = req
@logger.log(status, env)

[status, headers, response]
end
end
2 changes: 2 additions & 0 deletions middleware/config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require_relative 'app'
run App.new
16 changes: 16 additions & 0 deletions middleware/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Logger
def log(status, env)
req_method = env['REQUEST_METHOD']
action = env['simpler.action']
path = env['PATH_INFO']
controller = env['simpler.controller']
type = env['Content-Type']
template ||= env['simpler.template']

file = File.open('log/app.log', 'w') # - no such file or directory @ rb_sysopen - '...'

file.write(
"Request: #{req_method} #{path} Handler: #{controller}##{action} Response: #{status} #{type} #{template}"
)
end
end