Rack::Response and Rack::Request work pretty good
This commit is contained in:
parent
3ec99843eb
commit
92e199d5db
|
@ -1,29 +1,26 @@
|
||||||
module Zero
|
module Zero
|
||||||
|
|
||||||
# abstract class to make creation of controllers easier
|
# abstract class to make creation of controllers easier
|
||||||
#
|
#
|
||||||
# This abstract class creates an interface to make it easier to write
|
# This abstract class creates an interface to make it easier to write
|
||||||
# rack compatible controllers. It catches #call and creates a new instance
|
# rack compatible controllers. It catches #call and creates a new instance
|
||||||
# with the environment and calls #render on it.
|
# with the environment and calls #render on it.
|
||||||
class Controller
|
class Controller
|
||||||
|
# initialize a new instance of the controller and call response on it
|
||||||
def self.call(env)
|
def self.call(env)
|
||||||
new(Request.new(env)).response
|
new(Rack::Request.new(env)).response
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# initialize the controller
|
||||||
|
# @param Request a request object
|
||||||
def initialize(request)
|
def initialize(request)
|
||||||
@request = request
|
@request = request
|
||||||
@code = 200
|
@response = Rack::Response.new
|
||||||
@header = {}
|
|
||||||
@body = ''
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# build the response and return it
|
||||||
|
# @return Response a rack conform response
|
||||||
def response
|
def response
|
||||||
render
|
raise NotImplementedError.new("Not Implemented in ${__FILE__}")
|
||||||
[@code, @header, [@body]]
|
|
||||||
end
|
|
||||||
|
|
||||||
def render
|
|
||||||
raise NotImplementedError.new
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
module Zero
|
|
||||||
# encapsulate the rack environment
|
|
||||||
class Request
|
|
||||||
attr_reader :env
|
|
||||||
|
|
||||||
# create a new
|
|
||||||
def initialize(env)
|
|
||||||
@env = env
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -1,22 +0,0 @@
|
||||||
module Zero
|
|
||||||
# an easier interface for the response
|
|
||||||
#
|
|
||||||
# The response for rack has to be an array of three elements. This class makes
|
|
||||||
# it easier to fill all the needed data.
|
|
||||||
class Response
|
|
||||||
attr_accessor :code, :header, :body
|
|
||||||
|
|
||||||
# init a new response
|
|
||||||
def initialize
|
|
||||||
@code = 404
|
|
||||||
@header = {}
|
|
||||||
@body = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
# builds the response for rack and checks for protocol errors
|
|
||||||
# @return Array the rack response
|
|
||||||
def to_a
|
|
||||||
[@code, @header, [@body]]
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
Loading…
Reference in New Issue