Skip to content
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
8 changes: 8 additions & 0 deletions app/controllers/tests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@ class TestsController < Simpler::Controller

def index
@time = Time.now

render 'tests/list', text: :plain
status 201
headers['Content-Type'] = 'text/plain'
end

def create

end

def show
@id = params[:id]
end

end
5 changes: 5 additions & 0 deletions app/views/tests/index.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Index | Simpler application

Simpler framework at work!</h1>

Time <%= @time %>
5 changes: 5 additions & 0 deletions app/views/tests/list.text.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
List | Simpler application

Simpler framework at work!

Time <%= @time %>
12 changes: 12 additions & 0 deletions app/views/tests/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Show | Simpler application</title>
</head>
<body>
<h1>Simpler framework at work!</h1>

<p>id:<%= @id %></p>
</body>
</html>
3 changes: 3 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
require_relative 'config/environment'
require_relative 'lib/middleware/logger'

use SimplerLogger, 'log/app.log'

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
24 changes: 24 additions & 0 deletions lib/middleware/logger.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'logger'

class SimplerLogger
def initialize(app, log_path)
@app = app
@logger = Logger.new(Simpler.root.join(log_path))
end

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

[status, headers, response]
end

private

def message(status, headers, env)
"\nRequest: #{env['REQUEST_METHOD']} #{env['REQUEST_URI']}\n" \
"Handler: #{env['simpler.controller'].class}##{env['simpler.action']}\n" \
"Parameters: #{env['simpler.controller'].request.params}\n" \
"Response: #{status} [#{headers['Content-Type']}] #{env['simpler.template_name']}\n" \
end
end
8 changes: 7 additions & 1 deletion lib/simpler/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def routes(&block)

def call(env)
route = @router.route_for(env)

return url_not_found unless route

controller = route.controller.new(env)
action = route.action

Expand All @@ -37,7 +40,7 @@ def call(env)
private

def require_app
Dir["#{Simpler.root}/app/**/*.rb"].each { |file| require file }
Dir["#{Simpler.root}/app/**/*.rb"].sort.each { |file| require file }
end

def require_routes
Expand All @@ -54,5 +57,8 @@ def make_response(controller, action)
controller.make_response(action)
end

def url_not_found
[404, { 'Content-Type' => 'text/plain' }, ["Error: URL not found\n"]]
end
end
end
15 changes: 15 additions & 0 deletions lib/simpler/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,26 @@ def render_body
end

def params
add_id_params
@request.params
end

def add_id_params
path_parts = @request.env['REQUEST_PATH'].split('/')
@request.params[:id] = path_parts.last if path_parts.last[/^\d+$/]
end

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

def headers
@response.headers
end

def render(template)
@request.env['simpler.template'] = template
@request.env['simpler.template_format'] = template_format
end

end
Expand Down
2 changes: 1 addition & 1 deletion lib/simpler/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def initialize(method, path, controller, action)
end

def match?(method, path)
@method == method && path.match(@path)
@method == method && path.gsub(/\d+/, ':id') == @path
end

end
Expand Down
11 changes: 10 additions & 1 deletion lib/simpler/view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module Simpler
class View

VIEW_BASE_PATH = 'app/views'.freeze
FORMAT = { plain: 'text', html: 'html', json: 'json' }.freeze

def initialize(env)
@env = env
Expand All @@ -29,10 +30,18 @@ def template
@env['simpler.template']
end

def template_format
template_format = @env['simpler.template_format']
format = template_format.nil? ? 'html'.to_sym : template_format.values.first

FORMAT[format]
end

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

Simpler.root.join(VIEW_BASE_PATH, "#{path}.html.erb")
Simpler.root.join(VIEW_BASE_PATH, @env['simpler.template_name'])
end

end
Expand Down