Skip to content

Lesson20 #148

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 5 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
256 changes: 256 additions & 0 deletions .byebug_history
Original file line number Diff line number Diff line change
@@ -0,0 +1,256 @@
c
@out_params
c
@out_params
c
v
env['simpler.controller']
"#{env['simpler.controller'].class}##{env['simpler.action']}"
c
@out_params
c
handler
response
headers
status
c
params
c
@params
c
controller.params
c
controller.params
c
params
c
controller.params
c
controller.params
c
controller.params
c
controller.params
c
controller.params
controller
c
controller.headers
controller.params
c
controller.params
controller.params=
controller.params
c
@params
c
@request.env['simpler.controller.params']
c
controller.request.env['simpler.controller.params']
c
controller.request.env['simpler.action']
controller.request
route.params
controller.route
controller
c
route
c
route.determ_params(env)
env
route
exit
@request.env['simpler.controller']
@request.env['simpler.contoller.params']
@request.env['simpler.contoller']
self
@request.env['simpler.contoller']
@request.env['simpler.action']
env['simpler.action']
{env['simpler.controller']
#{env['simpler.controller'].class}##{env['simpler.action']}"
exit
@request
exit
env['simpler.action']
env['simpler.router']
env['simpler']
@app.call(env)
env['router']
env['simpler.router']
env
exit
env['simpler.controller']
env
exit
handler
exit
env['simpler.action']
env['simpler.controller']
env['simpler.controller'].class
env
env['simpler.controller'].class
#{env{'simpler.action'}}
"#{env['simpler.controller'].class}"
env['simpler.controller'].class
env
@app.call(env)
exit
handler.class
handler
exit
respone
response
header
headers
status
status, header, respone = @app.call(env)
all.status
all
exit
all
env
all
c
route_path
user_path
c
@params
route_path
exit
route_path
user_path
user_path.match?(route_path)
@path
exit
"/tests/1".match(/\/tests\/(\w+)\Z/)
"/tests/1".match?(/\/tests\/(\w+)\Z/)
"/tests/1".match?(/\/tests\/(\w+)/)
"/tests/1".match?(/\/tests\/(\w+)/Z)
/tests/1.match?(/\/tests\/(\w+)/)
"/tests/1".match?(/\/tests\/(\w+)/)
"/tests/1".match?("/\/tests\/(\w+)/")
"/tests/1".match? "/\/tests\/(\w+)/"
"/tests/1".match?('/\/tests\/(\w+)/')
route_path
"/tests/1".match?(route_path)
"/tests/1".match?("/\/tests\/(\w+)/")
user_path.match?(route_path)
route_path
user_path
c
route_path
user_path
@path
exit
Hash[keys.captures.zip(values.captures)]
Hash[*keys.captures.zip(values.captures)]
keys.captures.zip(values.captures)
values.captures
keys.captures
keys.captures.zip(values.captures)
keys.captures
keys
keys.names
@path
c
@path
exit
keys
values
route_path
user_path
user_path.match?(route_path)
@path
exit
values
keys
@path
c
@path
path
c
keys
values
c
keys
exit
route_path
@path
route_path
keys
route_path
c
route_path
values
route_path
c
@router
@router.route_for(env)
c
@routes.find { |route| route.match?(method, path) }
c
route
c
@routes.find { |route| route.match?(method, path) }
path
@routes
c
@router.route_for(env)
env
@router
route
c
@method == method && correct_path?(path)
c
@method == method && correct_path?(path)
correct_path?(path)
c
@path
path
correct_path?(path)
c
correct_path?(path)
exit
c
correct_path?(path)
@method == method
@method
c
@routes.find { |route| route.match?(method, path) }
c
route
c
@params
c
@path
c
@path
path
exit
@params
c
route
c
@path
path
c
@path
user_path
route_path
@params
c
exit
user_path
path
@params
params
c
@path
path
c
@path
path
exit
@path
path
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/.byebug_history
/app/app.log
12 changes: 8 additions & 4 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
class TestsController < Simpler::Controller
# frozen_string_literal: true

class TestsController < Simpler::Controller
def index
@time = Time.now
@tests = Test.all
# render plain: "ololo"
status 201
end

def create

def show
@test = Test.find(id: @params[:id])
end

def create; end
end
3 changes: 2 additions & 1 deletion app/models/test.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# frozen_string_literal: true

# Simpler.application.db.create_table(:tests) do
# primary_key :id
# String :title, null: false
# Integer :level, default: 0
# end
class Test < Sequel::Model

end
9 changes: 7 additions & 2 deletions app/views/tests/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
<body>
<h1>Simpler framework at work!</h1>

<p><%= @time %></p>
<ul>
<% @tests.each do |test| %>
<li><%= test.title %></li>
<% end %>

</ul>
</body>
</html>
</html>
10 changes: 10 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Index | Simpler application</title>
</head>
<body>
<h1> <%= @test.title %> </h1>
</body>
</html>
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 'lib/middleware/historylogger'

use HistoryLogger
run Simpler.application
2 changes: 2 additions & 0 deletions config/environment.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require_relative '../lib/simpler'

Simpler.application.bootstrap!
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# frozen_string_literal: true

Simpler.application.routes do
get '/tests', 'tests#index'
get '/tests/:id', 'tests#show'
post '/tests', 'tests#create'
end
31 changes: 31 additions & 0 deletions lib/middleware/historylogger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

class HistoryLogger
attr_reader :out_params

def initialize(app)
@app = app
@out_params = {}
end

def call(env)
status, headers, response = @app.call(env)

@out_params[:Request] = "#{env['REQUEST_METHOD']} #{env['REQUEST_PATH']}"
@out_params[:Handler] = "#{env['simpler.controller'].class}##{env['simpler.action']}"
@out_params[:Parameters] = (env['simpler.controller.params']).to_s
@out_params[:Response] = "#{status} [#{headers['Content-Type']}] #{env['simpler.template_path']}"

add_log(@out_params)

[status, headers, response]
end

def add_log(content)
logfile = 'log/app.log'
File.open(logfile, 'a') do |file|
content.each { |k, v| file << ("#{k}: #{v}\n") }
file << "\n"
end
end
end
Loading