Class: Restfulness::Request
- Inherits:
-
Object
- Object
- Restfulness::Request
- Defined in:
- lib/restfulness/request.rb
Overview
Simple, indpendent, request interface for dealing with the incoming information in a request.
Currently wraps around the information provided in a Rack Request object.
Instance Attribute Summary collapse
-
#action ⇒ Object
The HTTP action being handled.
-
#app ⇒ Object
readonly
Who does this request belong to?.
-
#body ⇒ Object
Raw HTTP body, for POST and PUT requests.
-
#headers ⇒ Object
Hash of HTTP headers.
-
#remote_ip ⇒ Object
Additional useful fields.
-
#uri ⇒ Object
Ruby URI object.
-
#user_agent ⇒ Object
Additional useful fields.
Instance Method Summary collapse
-
#http_accept_language ⇒ Object
Provide a wrapper for the http_accept_language parser.
-
#initialize(app) ⇒ Request
constructor
A new instance of Request.
- #params ⇒ Object
- #path ⇒ Object
- #query ⇒ Object
- #route ⇒ Object
- #sanitized_params ⇒ Object
- #sanitized_query_string ⇒ Object
Constructor Details
#initialize(app) ⇒ Request
Returns a new instance of Request.
27 28 29 30 31 32 33 34 |
# File 'lib/restfulness/request.rb', line 27 def initialize(app) @app = app # Prepare basics self.action = nil self.headers = {} self.body = nil end |
Instance Attribute Details
#action ⇒ Object
The HTTP action being handled
13 14 15 |
# File 'lib/restfulness/request.rb', line 13 def action @action end |
#app ⇒ Object (readonly)
Who does this request belong to?
10 11 12 |
# File 'lib/restfulness/request.rb', line 10 def app @app end |
#body ⇒ Object
Raw HTTP body, for POST and PUT requests.
22 23 24 |
# File 'lib/restfulness/request.rb', line 22 def body @body end |
#headers ⇒ Object
Hash of HTTP headers. Keys always normalized to lower-case symbols with underscore.
16 17 18 |
# File 'lib/restfulness/request.rb', line 16 def headers @headers end |
#remote_ip ⇒ Object
Additional useful fields
25 26 27 |
# File 'lib/restfulness/request.rb', line 25 def remote_ip @remote_ip end |
#uri ⇒ Object
Ruby URI object
19 20 21 |
# File 'lib/restfulness/request.rb', line 19 def uri @uri end |
#user_agent ⇒ Object
Additional useful fields
25 26 27 |
# File 'lib/restfulness/request.rb', line 25 def user_agent @user_agent end |
Instance Method Details
#http_accept_language ⇒ Object
Provide a wrapper for the http_accept_language parser
77 78 79 |
# File 'lib/restfulness/request.rb', line 77 def http_accept_language @http_accept_language = HttpAcceptLanguage::Parser.new(headers[:accept_language]) end |
#params ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/restfulness/request.rb', line 57 def params return @params if @params || body.nil? case headers[:content_type] when /application\/json/ begin @params = MultiJson.decode(body) rescue MultiJson::LoadError raise HTTPException.new(400) end else raise HTTPException.new(406) end end |
#path ⇒ Object
40 41 42 |
# File 'lib/restfulness/request.rb', line 40 def path @path ||= (route ? route.build_path(uri.path) : nil) end |
#query ⇒ Object
49 50 51 |
# File 'lib/restfulness/request.rb', line 49 def query @query ||= ::Rack::Utils.parse_nested_query(uri.query).with_indifferent_access end |
#route ⇒ Object
44 45 46 47 |
# File 'lib/restfulness/request.rb', line 44 def route # Determine the route from the uri @route ||= app.router.route_for(uri.path) end |
#sanitized_params ⇒ Object
71 72 73 74 |
# File 'lib/restfulness/request.rb', line 71 def sanitized_params # Note: this returns nil if #params has not been called @sanitized_params ||= @params ? Sanitizer.sanitize_hash(@params) : nil end |
#sanitized_query_string ⇒ Object
53 54 55 |
# File 'lib/restfulness/request.rb', line 53 def sanitized_query_string @sanitized_query ||= uri.query ? Sanitizer.sanitize_query_string(uri.query) : '' end |