Class: Http::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/http/request.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(method, uri, headers = {}, body = nil, version = "1.1") ⇒ Request

:nodoc:



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/http/request.rb', line 12

def initialize(method, uri, headers = {}, body = nil, version = "1.1")
  @method = method.to_s.downcase.to_sym
  raise UnsupportedMethodError, "unknown method: #{method}" unless METHODS.include? @method

  @uri = uri.is_a?(URI) ? uri : URI(uri.to_s)

  @headers = {}
  headers.each do |name, value|
    name = name.to_s
    key = name[CANONICAL_HEADER]
    key ||= Http.canonicalize_header(name)
    @headers[key] = value
  end

  @body, @version = body, version
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



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

def body
  @body
end

#headersObject (readonly)

Returns the value of attribute headers.



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

def headers
  @headers
end

#methodObject (readonly)

Method is given as a lowercase symbol e.g. :get, :post



4
5
6
# File 'lib/http/request.rb', line 4

def method
  @method
end

#uriObject (readonly)

“Request URI” as per RFC 2616 www.w3.org/Protocols/rfc2616/rfc2616-sec5.html



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

def uri
  @uri
end

#versionObject (readonly)

Returns the value of attribute version.



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

def version
  @version
end

Instance Method Details

#[](header) ⇒ Object

Obtain the given header



30
31
32
# File 'lib/http/request.rb', line 30

def [](header)
  @headers[Http.canonicalize_header(header)]
end

#to_net_http_requestObject

Create a Net::HTTP request from this request



35
36
37
38
39
40
# File 'lib/http/request.rb', line 35

def to_net_http_request
  request_class = Net::HTTP.const_get(@method.to_s.capitalize)
  request = request_class.new(@uri.request_uri, @headers)
  request.body = @body
  request
end