Class: RightScale::CloudApi::HTTPRequest
- Inherits:
-
HTTPParent
- Object
- HTTPParent
- RightScale::CloudApi::HTTPRequest
- Defined in:
- lib/base/helpers/http_request.rb
Overview
A Wrapper around standard Net::HTTPRequest class.
The class supports some handy methods for managing the verb, the body, the path and the headers. And everythig else can be accessed through raw attribute that points to the original Net::HTTPRequest instance.
Constant Summary collapse
- BODY_BYTES_TO_LOG =
Max byte to log
6000
Instance Attribute Summary collapse
-
#params ⇒ Hash
Request HTTP params.
-
#path ⇒ String
Request path.
-
#verb ⇒ String
Request HTTP verb.
Attributes inherited from HTTPParent
Instance Method Summary collapse
-
#[]=(header, value) ⇒ void
Sets a new headers value(s).
-
#body=(new_body) ⇒ void
Sets the body and the ‘content-length’ header.
-
#body_info ⇒ String
Displays the body information.
-
#initialize(verb, path, body, headers, raw = nil) ⇒ Rightscale::CloudApi::HTTPRequest
constructor
Constructor.
-
#to_s ⇒ String
Displays the request as a String with the verb and the path.
Methods inherited from HTTPParent
#[], #headers, #headers_info, #is_io?
Constructor Details
#initialize(verb, path, body, headers, raw = nil) ⇒ Rightscale::CloudApi::HTTPRequest
Constructor
82 83 84 85 86 87 88 89 |
# File 'lib/base/helpers/http_request.rb', line 82 def initialize(verb, path, body, headers, raw=nil) # Create a request @verb = verb.to_s.downcase @path = path @raws = raw @headers = HTTPHeaders::new(headers) self.body = body end |
Instance Attribute Details
#params ⇒ Hash
Request HTTP params
62 63 64 |
# File 'lib/base/helpers/http_request.rb', line 62 def params @params end |
#path ⇒ String
Request path
53 54 55 |
# File 'lib/base/helpers/http_request.rb', line 53 def path @path end |
#verb ⇒ String
Request HTTP verb
44 45 46 |
# File 'lib/base/helpers/http_request.rb', line 44 def verb @verb end |
Instance Method Details
#[]=(header, value) ⇒ void
This method returns an undefined value.
Sets a new headers value(s)
100 101 102 |
# File 'lib/base/helpers/http_request.rb', line 100 def []=(header, value) @headers[header] = value end |
#body=(new_body) ⇒ void
This method returns an undefined value.
Sets the body and the ‘content-length’ header
If the body is blank it sets the header to 0. If the body is a String it sets the header to the string size. If the body is an IO object it tries to open it in binmode mode and sets the header to the filesize (if the header is not set or points outside of the range of (0..filesize-1)).
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/base/helpers/http_request.rb', line 117 def body=(new_body) # Set a request body if new_body._blank? @body = nil self['content-length'] = 0 else if new_body.is_a?(IO) @body = file = new_body # Make sure the file is openned in binmode file.binmode if file.respond_to?(:binmode) # Fix 'content-length': it must not be bigger than a piece of a File left to be read or a String body size. # Otherwise the connection may behave like crazy causing 4xx or 5xx responses # KD: Make sure this code is used with the patched RightHttpConnection gem (see net_fix.rb) file_size = file.respond_to?(:lstat) ? file.lstat.size : file.size bytes_to_read = [ file_size - file.pos, self['content-length'].first ].compact.map{|v| v.to_i }.sort.first # remove nils then make values Integers if self['content-length'].first._blank? || self['content-length'].first.to_i > bytes_to_read self['content-length'] = bytes_to_read end else @body = new_body self['content-length'] = body.size if self['content-length'].first.to_i > body.size end end end |
#body_info ⇒ String
Displays the body information
163 164 165 166 167 168 169 |
# File 'lib/base/helpers/http_request.rb', line 163 def body_info if is_io? "#{body.class.name}, size: #{body.respond_to?(:lstat) ? body.lstat.size : body.size}, pos: #{body.pos}" else "size: #{body.to_s.size}, first #{BODY_BYTES_TO_LOG} bytes:\n#{body.to_s[0...BODY_BYTES_TO_LOG]}" end end |
#to_s ⇒ String
Displays the request as a String with the verb and the path
152 153 154 |
# File 'lib/base/helpers/http_request.rb', line 152 def to_s "#{verb.upcase} #{path}" end |