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

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

Returns a new instance of Request



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

#method_missing(m, *args) ⇒ Object



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

#bodyObject (readonly)

Returns the value of attribute body



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

def body
  @body
end

#disp_pathObject

Returns the value of attribute disp_path



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

def disp_path
  @disp_path
end

#headersObject (readonly)

Returns the value of attribute headers



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

def headers
  @headers
end

#methodObject (readonly)

Returns the value of attribute method



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

def method
  @method
end

#path_infoObject

Returns the value of attribute path_info



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

def path_info
  @path_info
end

#path_tokensObject

Returns the value of attribute path_tokens



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

def path_tokens
  @path_tokens
end

#uriObject (readonly)

Returns the value of attribute uri



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

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)


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

#has_body?Boolean

Whether the request body is present.

Returns:

  • (Boolean)


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

def has_body?
  !(body.nil? || body.empty?)
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



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