Class: Restfulness::Request
- Inherits:
-
Object
- Object
- Restfulness::Request
- Includes:
- Restfulness::Requests::Authorization
- 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.
-
#env ⇒ Object
Expose rack env to interact with rack middleware.
-
#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
- #accept ⇒ Object
- #content_type ⇒ Object
-
#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
Methods included from Restfulness::Requests::Authorization
Constructor Details
#initialize(app) ⇒ Request
Returns a new instance of Request.
31 32 33 34 35 36 37 38 |
# File 'lib/restfulness/request.rb', line 31 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
17 18 19 |
# File 'lib/restfulness/request.rb', line 17 def action @action end |
#app ⇒ Object (readonly)
Who does this request belong to?
14 15 16 |
# File 'lib/restfulness/request.rb', line 14 def app @app end |
#body ⇒ Object
Raw HTTP body, for POST and PUT requests.
26 27 28 |
# File 'lib/restfulness/request.rb', line 26 def body @body end |
#env ⇒ Object
Expose rack env to interact with rack middleware
11 12 13 |
# File 'lib/restfulness/request.rb', line 11 def env @env end |
#headers ⇒ Object
Hash of HTTP headers. Keys always normalized to lower-case symbols with underscore.
20 21 22 |
# File 'lib/restfulness/request.rb', line 20 def headers @headers end |
#remote_ip ⇒ Object
Additional useful fields
29 30 31 |
# File 'lib/restfulness/request.rb', line 29 def remote_ip @remote_ip end |
#uri ⇒ Object
Ruby URI object
23 24 25 |
# File 'lib/restfulness/request.rb', line 23 def uri @uri end |
#user_agent ⇒ Object
Additional useful fields
29 30 31 |
# File 'lib/restfulness/request.rb', line 29 def user_agent @user_agent end |
Instance Method Details
#accept ⇒ Object
61 62 63 64 65 |
# File 'lib/restfulness/request.rb', line 61 def accept if headers[:accept] @accept ||= Headers::Accept.new(headers[:accept]) end end |
#content_type ⇒ Object
67 68 69 70 71 |
# File 'lib/restfulness/request.rb', line 67 def content_type if headers[:content_type] @content_type ||= Headers::MediaType.new(headers[:content_type]) end end |
#http_accept_language ⇒ Object
Provide a wrapper for the http_accept_language parser
97 98 99 |
# File 'lib/restfulness/request.rb', line 97 def http_accept_language @http_accept_language = HttpAcceptLanguage::Parser.new(headers[:accept_language]) end |
#params ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/restfulness/request.rb', line 73 def params @params ||= begin data = body_to_string || "" if data.length > 0 if content_type && content_type.json? params_from_json(data) elsif content_type && content_type.form? params_from_form(data) else # Body provided with no or invalid content type raise HTTPException.new(406) end else {} end end end |
#path ⇒ Object
44 45 46 |
# File 'lib/restfulness/request.rb', line 44 def path @path ||= (route ? route.build_path(uri.path) : nil) end |
#query ⇒ Object
53 54 55 |
# File 'lib/restfulness/request.rb', line 53 def query @query ||= ::Rack::Utils.parse_nested_query(uri.query).with_indifferent_access end |
#route ⇒ Object
48 49 50 51 |
# File 'lib/restfulness/request.rb', line 48 def route # Determine the route from the uri @route ||= app.router.route_for(uri.path) end |
#sanitized_params ⇒ Object
91 92 93 94 |
# File 'lib/restfulness/request.rb', line 91 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
57 58 59 |
# File 'lib/restfulness/request.rb', line 57 def sanitized_query_string @sanitized_query ||= uri.query ? Sanitizer.sanitize_query_string(uri.query) : '' end |