diff options
author | Gibheer <gibheer@gmail.com> | 2012-11-08 21:31:24 +0100 |
---|---|---|
committer | Gibheer <gibheer@gmail.com> | 2012-11-08 21:36:56 +0100 |
commit | 24eba118e6a5cdef01a6f73a32dcfc7f57184ac1 (patch) | |
tree | 8cd2d8023f339e3a0029ff352c4d21fddf56bf19 /lib/zero/request.rb | |
parent | 396151eb70f5c8eb4ed08698a4dcd04e241df099 (diff) |
replacement for rack request
This should replace Rack::Request in the hole lib. It seperates
everything worth into its own classes, like parameters and the accept
header till now.
More will follow
Diffstat (limited to 'lib/zero/request.rb')
-rw-r--r-- | lib/zero/request.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/lib/zero/request.rb b/lib/zero/request.rb new file mode 100644 index 0000000..48b59d3 --- /dev/null +++ b/lib/zero/request.rb @@ -0,0 +1,53 @@ +require_relative 'request/accept' +require_relative 'request/parameter' + +module Zero + # This class wraps around a rack environment for easier access to all data. + class Request + CONST_CONTENT_TYPE = 'CONTENT_TYPE' + CONST_HTTP_ACCEPT = 'HTTP_ACCEPT' + CONST_PATH_INFO = 'PATH_INFO' + CONST_REQUEST_METHOD = 'REQUEST_METHOD' + + # create a new request object + def initialize(env) + @env = env + @method = @env[CONST_REQUEST_METHOD].downcase.to_sym + end + + # get the requested path + def path + @path ||= @env[CONST_PATH_INFO] + end + + # returns a set of get and post variables + def params + @params ||= Request::Parameter.new(@env) + end + + # return the content type of the request + # TODO change into its own object? + def content_type + @env[CONST_CONTENT_TYPE] if @env.has_key?(CONST_CONTENT_TYPE) + end + + # get the media types + # @return Accept on Accept object managing all types and their order + def media_types + @accept ||= Request::Accept.new(@env[CONST_HTTP_ACCEPT]) + end + + # is the method 'GET'? + def get?; @method == :get; end + # is the method 'POST'? + def post?; @method == :post; end + # is the method 'PUT'? + def put?; @method == :put; end + # is the method 'DELETE'? + def delete?; @method == :delete; end + # is the method 'HEAD'? + def head?; @method == :head; end + # is the method 'PATCH'? + def head?; @method == :patch; end + end +end |