Class: RightScale::CloudApi::HTTPRequest

Inherits:
HTTPParent show all
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.

API:

  • public

Constant Summary collapse

BODY_BYTES_TO_LOG =

Max byte to log

API:

  • public

6000

Instance Attribute Summary collapse

Attributes inherited from HTTPParent

#body, #raw

Instance Method Summary collapse

Methods inherited from HTTPParent

#[], #headers, #headers_info, #is_io?

Constructor Details

#initialize(verb, path, body, headers, raw = nil) ⇒ Rightscale::CloudApi::HTTPRequest

Constructor

Examples:

new('get', 'xxx/yyy/zzz', nil, {})

Parameters:

  • The current verb (‘get’, ‘post’, ‘put’, etc).

  • The request body.

  • The URL path.

  • The request headers.

  • (defaults to: nil)

    The original request (optional).

API:

  • public



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

#paramsHash

Request HTTP params

Examples:

response.params #=> { 'a' => 'b', 'c' => 'd' }

Returns:

API:

  • public



62
63
64
# File 'lib/base/helpers/http_request.rb', line 62

def params
  @params
end

#pathString

Request path

Examples:

response.path #=> 'xxx/yyy/zzz'

Returns:

API:

  • public



53
54
55
# File 'lib/base/helpers/http_request.rb', line 53

def path
  @path
end

#verbString

Request HTTP verb

Examples:

response.verb #=> 'get'

Returns:

API:

  • public



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)

Examples:

# no example

Parameters:

  • The header name.

  • The value for the header.

API:

  • public



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)).

Examples:

# no example

Parameters:

API:

  • public



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_infoString

Displays the body information

Examples:

request.body_info #=> "something"

Returns:

  • The body info.

API:

  • public



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_sString

Displays the request as a String with the verb and the path

Examples:

ec2.request.to_s #=>
 "GET /?AWSAccessKeyId=000..000A&Action=DescribeSecurityGroups&SignatureMethod=HmacSHA256&
   SignatureVersion=2&Timestamp=2013-02-22T23%3A54%3A30.000Z&Version=2012-10-15&
   Signature=Gd...N4yQStO5aKXfYnrM4%3D"

Returns:

  • The request verb and path info.

API:

  • public



152
153
154
# File 'lib/base/helpers/http_request.rb', line 152

def to_s
  "#{verb.upcase} #{path}"
end