Class: Webmachine::Request

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/webmachine/request.rb

Overview

Request represents a single HTTP request sent from a client. It should be instantiated by Adapters when a request is received

Constant Summary collapse

STANDARD_HTTP_METHODS =
%w[GET HEAD POST PUT DELETE TRACE CONNECT OPTIONS]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, headers, body) ⇒ Request

Returns a new instance of Request.

Parameters:

  • method (String)

    the HTTP request method

  • uri (URI)

    the requested URI, including host, scheme and port

  • headers (Headers)

    the HTTP request headers

  • body (String, #to_s, #each, nil)

    the entity included in the request, if present



20
21
22
# File 'lib/webmachine/request.rb', line 20

def initialize(method, uri, headers, body)
  @method, @uri, @headers, @body = method, uri, headers, body
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ Object

Enables quicker access to request headers by using a lowercased-underscored version of the header name, e.g. ‘if_unmodified_since`.



29
30
31
32
33
34
35
36
# File 'lib/webmachine/request.rb', line 29

def method_missing(m, *args, &block)
  if m.to_s =~ /^(?:[a-z0-9])+(?:_[a-z0-9]+)*$/i
    # Access headers more easily as underscored methods.
    self[m.to_s.tr('_', '-')]
  else
    super
  end
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



9
10
11
# File 'lib/webmachine/request.rb', line 9

def body
  @body
end

#disp_pathObject

Returns the value of attribute disp_path.



10
11
12
# File 'lib/webmachine/request.rb', line 10

def disp_path
  @disp_path
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/webmachine/request.rb', line 9

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method.



9
10
11
# File 'lib/webmachine/request.rb', line 9

def method
  @method
end

#path_infoObject

Returns the value of attribute path_info.



10
11
12
# File 'lib/webmachine/request.rb', line 10

def path_info
  @path_info
end

#path_tokensObject

Returns the value of attribute path_tokens.



10
11
12
# File 'lib/webmachine/request.rb', line 10

def path_tokens
  @path_tokens
end

#uriObject (readonly)

Returns the value of attribute uri.



9
10
11
# File 'lib/webmachine/request.rb', line 9

def uri
  @uri
end

Instance Method Details

#base_uriURI

The root URI for the request, ignoring path and query. This is useful for calculating relative paths to resources.

Returns:

  • (URI)


46
47
48
49
50
51
# File 'lib/webmachine/request.rb', line 46

def base_uri
  @base_uri ||= uri.dup.tap do |u|
    u.path = "/"
    u.query = nil
  end
end

#connect?Boolean

Is this a CONNECT request?

Returns:

  • (Boolean)

    true if this request was made with the CONNECT method



142
143
144
# File 'lib/webmachine/request.rb', line 142

def connect?
  method == "CONNECT"
end

#cookiesHash

The cookies sent in the request.

Returns:

  • (Hash)

    {} if no Cookies header set



75
76
77
78
79
80
# File 'lib/webmachine/request.rb', line 75

def cookies
  unless @cookies
    @cookies = Webmachine::Cookie.parse(headers['Cookie'])
  end
  @cookies
end

#delete?Boolean

Is this a DELETE request?

Returns:

  • (Boolean)

    true if this request was made with the DELETE method



126
127
128
# File 'lib/webmachine/request.rb', line 126

def delete?
  method == "DELETE"
end

#get?Boolean

Is this a GET request?

Returns:

  • (Boolean)

    true if this request was made with the GET method



94
95
96
# File 'lib/webmachine/request.rb', line 94

def get?
  method == "GET"
end

#has_body?Boolean

@return[true, false] Whether the request body is present.

Returns:

  • (Boolean)


39
40
41
# File 'lib/webmachine/request.rb', line 39

def has_body?
  !(body.nil? || body.empty?)
end

#head?Boolean

Is this a HEAD request?

Returns:

  • (Boolean)

    true if this request was made with the HEAD method



102
103
104
# File 'lib/webmachine/request.rb', line 102

def head?
  method == "HEAD"
end

#https?Boolean

Is this an HTTPS request?

Returns:

  • (Boolean)

    true if this request was made via HTTPS



86
87
88
# File 'lib/webmachine/request.rb', line 86

def https?
  uri.scheme == "https"
end

#options?Boolean

Is this an OPTIONS request?

Returns:

  • (Boolean)

    true if this request was made with the OPTIONS method



150
151
152
# File 'lib/webmachine/request.rb', line 150

def options?
  method == "OPTIONS"
end

#post?Boolean

Is this a POST request?

Returns:

  • (Boolean)

    true if this request was made with the GET method



110
111
112
# File 'lib/webmachine/request.rb', line 110

def post?
  method == "POST"
end

#put?Boolean

Is this a PUT request?

Returns:

  • (Boolean)

    true if this request was made with the PUT method



118
119
120
# File 'lib/webmachine/request.rb', line 118

def put?
  method == "PUT"
end

#queryHash

Returns a hash of query parameters (they come after the ? in the URI). Note that this does NOT work in the same way as Rails, i.e. it does not support nested arrays and hashes.

Returns:

  • (Hash)

    query parameters



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/webmachine/request.rb', line 57

def query
  unless @query
    @query = {}
    (uri.query || '').split(/&/).each do |kv|
      key, value = kv.split(/=/)
      if key && value
        key, value = CGI.unescape(key), CGI.unescape(value)
        @query[key] = value
      end
    end
  end
  @query
end

#trace?Boolean

Is this a TRACE request?

Returns:

  • (Boolean)

    true if this request was made with the TRACE method



134
135
136
# File 'lib/webmachine/request.rb', line 134

def trace?
  method == "TRACE"
end