Class: Webmachine::Request

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

Overview

This represents a single HTTP request sent from a client.

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Request) initialize(meth, uri, headers, body)



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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(m, *args)



17
18
19
20
21
22
23
24
# File 'lib/webmachine/request.rb', line 17

def method_missing(m, *args)
  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

- (Object) body (readonly)

Returns the value of attribute body



7
8
9
# File 'lib/webmachine/request.rb', line 7

def body
  @body
end

- (Object) disp_path

Returns the value of attribute disp_path



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

def disp_path
  @disp_path
end

- (Object) headers (readonly)

Returns the value of attribute headers



7
8
9
# File 'lib/webmachine/request.rb', line 7

def headers
  @headers
end

- (Object) method (readonly)

Returns the value of attribute method



7
8
9
# File 'lib/webmachine/request.rb', line 7

def method
  @method
end

- (Object) path_info

Returns the value of attribute path_info



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

def path_info
  @path_info
end

- (Object) path_tokens

Returns the value of attribute path_tokens



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

def path_tokens
  @path_tokens
end

- (Object) uri (readonly)

Returns the value of attribute uri



7
8
9
# File 'lib/webmachine/request.rb', line 7

def uri
  @uri
end

Instance Method Details

- (URI) base_uri

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



34
35
36
37
38
39
# File 'lib/webmachine/request.rb', line 34

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

- (Boolean) has_body?

Whether the request body is present.



27
28
29
# File 'lib/webmachine/request.rb', line 27

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

- (Hash) query

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.



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

def query
  unless @query
    @query = {}
    uri.query.split(/&/).each do |kv|
      k, v = URI.unescape(kv).split(/=/)
      @query[k] = v if k && v
    end
  end
  @query
end