Class: Sidewalk::Request

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(env) ⇒ Request

Create a new instance from a Rack environment array

Parameters:

  • env (Array)

    is the environment data provided by Rack.



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_environmentObject (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_requestObject (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.

Returns:

  • (Boolean)


55
56
57
# File 'lib/sidewalk/request.rb', line 55

def asynchronous?
  @rack_request.xhr?
end

#cookiesObject

Cookies provided by the client.



50
51
52
# File 'lib/sidewalk/request.rb', line 50

def cookies
  rack_request.cookies
end

#get_paramsHash

Parameters provided via the query string.

Consider using #params instead.

Returns:

  • (Hash)


108
109
110
# File 'lib/sidewalk/request.rb', line 108

def get_params
  @get_params ||= @rack_request.GET
end

#headersObject

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.

Returns:

  • ('1.1')

    1.1’

  • ('1.0')

    1.0’

  • nil



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

#paramsObject

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_paramsHash

Paramters provided via POST.

Consider using #params instead.

Returns:

  • (Hash)


122
123
124
# File 'lib/sidewalk/request.rb', line 122

def post_params
  @post_params ||= @rack_request.POST
end

#root_uriURI::Common

The root URI of the application.

If you’re looking at this to construct an URI to another page, take a look at AppUri.

If you want to find out where the current request was to, see #uri.

Returns:

  • (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.

Returns:

  • (true, false)


99
100
101
# File 'lib/sidewalk/request.rb', line 99

def secure?
  @secure
end

#uriURI::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.

Returns:

  • (URI::Common)


92
93
94
# File 'lib/sidewalk/request.rb', line 92

def uri
  @request_uri.dup
end

#uri_matchObject

The UriMatch created by UriMapper.



113
114
115
# File 'lib/sidewalk/request.rb', line 113

def uri_match
  @uri_match ||= rack_environment['sidewalk.urimatch']
end

#uri_paramsHash

Paramters provided via named captures in the URL.

Consider using #params instead.

Returns:

  • (Hash)


131
132
133
# File 'lib/sidewalk/request.rb', line 131

def uri_params
  uri_match.parameters
end