Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
79 lines (60 sloc) 1.36 KB
require 'rack'
module Frankie
module BookKeeping
VERSION = 0.1
end
class Application
class << self
def call(env)
new.call(env)
end
def routes
@routes ||= []
end
def get(path, &block)
route('GET', path, block)
end
def post(path, &block)
route('POST', path, block)
end
def route(verb, path, block)
routes << {
verb: verb,
path: path,
block: block
}
end
end
def call(env)
@request = Rack::Request.new(env)
@verb = @request.request_method
@path = @request.path_info
@response = {
status: 200,
headers: headers,
body: []
}
route!
@response.values
end
def status(code)
@response[:status] = code
end
def headers
@headers ||= { 'Content-Type' => 'text/html' }
end
def body(string)
@response[:body] = [string]
end
def route!
match = Application.routes
.select { |route| route[:verb] == @verb }
.find { |route| route[:path] == @path }
return status(404) unless match
body match[:block].call
end
end
end
# example:
Frankie::Application.get('/') { 'Frankie says hello.' }
Rack::Handler::WEBrick.run Frankie::Application
You can’t perform that action at this time.