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 @raw = 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 |
# File 'lib/base/helpers/http_request.rb', line 117 def body=(new_body) # Set a request body 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 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.to_s self['content-length'] = @body.bytesize if self['content-length'].first.to_i > @body.bytesize end end |
#body_info ⇒ String
Displays the body information
157 158 159 160 161 162 163 |
# File 'lib/base/helpers/http_request.rb', line 157 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.bytesize}, 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
146 147 148 |
# File 'lib/base/helpers/http_request.rb', line 146 def to_s "#{verb.upcase} #{path}" end |