Class: Sidewalk::Request
- Inherits:
-
Object
- Object
- Sidewalk::Request
- Defined in:
- lib/sidewalk/request.rb
Overview
An object representing an HTTP request.
This is meant to provide convenient, high-level functions. You do have access to ##rack_environment and ##rack_request for lower-level information, but it’s best avoided.
Instances of this class are created by the Application.
Instance Attribute Summary collapse
-
#rack_environment ⇒ Object
readonly
The environment array provided by Rack.
-
#rack_request ⇒ Object
readonly
An instance of
Rack::Request
.
Instance Method Summary collapse
-
#asynchronous? ⇒ Boolean
Whether or not this is an asynchronous (AJAX) request.
-
#cookies ⇒ Object
Cookies provided by the client.
-
#get_params ⇒ Hash
Parameters provided via the query string.
-
#headers ⇒ Object
The HTTP headers.
-
#http_version ⇒ '1.1', '1.0'
What version of HTTP the client is using.
-
#initialize(env) ⇒ Request
constructor
Create a new instance from a Rack environment array.
-
#params ⇒ Object
Parameters provided by any means.
-
#post_params ⇒ Hash
Paramters provided via POST.
-
#root_uri ⇒ URI::Common
The root URI of the application.
-
#secure? ⇒ true, false
Whether or not this request came via HTTPS.
-
#uri ⇒ URI::Common
The URI of the current request.
- #uri_match ⇒ Object
-
#uri_params ⇒ Hash
Paramters provided via named captures in the URL.
Constructor Details
#initialize(env) ⇒ Request
Create a new instance from a Rack environment array
30 31 32 33 34 35 36 37 38 |
# File 'lib/sidewalk/request.rb', line 30 def initialize env @rack_environment = env @rack_version = env['rack.version'] sanity_check! @rack_request = Rack::Request.new(rack_environment) initialize_uris end |
Instance Attribute Details
#rack_environment ⇒ Object (readonly)
The environment array provided by Rack.
Please don’t use this directly unless you’re sure you need to.
19 20 21 |
# File 'lib/sidewalk/request.rb', line 19 def rack_environment @rack_environment end |
#rack_request ⇒ Object (readonly)
An instance of Rack::Request
.
This is a lower-level wrapper around #rack_environment
Please don’t use this directly unless you’re sure you need to.
25 26 27 |
# File 'lib/sidewalk/request.rb', line 25 def rack_request @rack_request end |
Instance Method Details
#asynchronous? ⇒ Boolean
Whether or not this is an asynchronous (AJAX) request.
55 56 57 |
# File 'lib/sidewalk/request.rb', line 55 def asynchronous? @rack_request.xhr? end |
#cookies ⇒ Object
Cookies provided by the client.
50 51 52 |
# File 'lib/sidewalk/request.rb', line 50 def rack_request. end |
#get_params ⇒ Hash
Parameters provided via the query string.
Consider using #params instead.
108 109 110 |
# File 'lib/sidewalk/request.rb', line 108 def get_params @get_params ||= @rack_request.GET end |
#headers ⇒ Object
The HTTP headers.
This does not include Rack/CGI variables - just real HTTP headers.
43 44 45 |
# File 'lib/sidewalk/request.rb', line 43 def headers @headers ||= rack_environment.select{|k,v| k.start_with? 'HTTP_'} end |
#http_version ⇒ '1.1', '1.0'
What version of HTTP the client is using.
64 65 66 67 68 69 |
# File 'lib/sidewalk/request.rb', line 64 def http_version @http_version ||= [ 'HTTP_VERSION', 'SERVER_PROTOCOL', ].map{ |x| rack_environment[x] }.find.first.to_s.split('/').last end |
#params ⇒ Object
Parameters provided by any means.
Precedence:
-
URL captures first
-
Then query string parameters
-
Then POST parameters
141 142 143 144 |
# File 'lib/sidewalk/request.rb', line 141 def params # URI parameters take precendence @params ||= post_params.merge(get_params.merge(uri_params)) end |
#post_params ⇒ Hash
Paramters provided via POST.
Consider using #params instead.
122 123 124 |
# File 'lib/sidewalk/request.rb', line 122 def post_params @post_params ||= @rack_request.POST end |
#root_uri ⇒ URI::Common
79 80 81 |
# File 'lib/sidewalk/request.rb', line 79 def root_uri @root_uri.dup end |
#secure? ⇒ true, false
Whether or not this request came via HTTPS.
99 100 101 |
# File 'lib/sidewalk/request.rb', line 99 def secure? @secure end |
#uri ⇒ URI::Common
The URI of the current request.
If you’re looking at this to construct a relative URI, take a look at Sidewalk::RelativeUri.
If you want to find out what the URI for the application is, see #root_uri.
92 93 94 |
# File 'lib/sidewalk/request.rb', line 92 def uri @request_uri.dup end |
#uri_match ⇒ Object
113 114 115 |
# File 'lib/sidewalk/request.rb', line 113 def uri_match @uri_match ||= rack_environment['sidewalk.urimatch'] end |
#uri_params ⇒ Hash
Paramters provided via named captures in the URL.
Consider using #params instead.
131 132 133 |
# File 'lib/sidewalk/request.rb', line 131 def uri_params uri_match.parameters end |