Class: RestClient::RawResponse

Inherits:
Object
  • Object
show all
Includes:
AbstractResponse
Defined in:
lib/restclient/raw_response.rb

Overview

The response from RestClient on a raw request looks like a string, but is actually one of these. 99% of the time you’re making a rest call all you care about is the body, but on the occasion you want to fetch the headers you can:

RestClient.get('http://example.com').headers[:content_type]

In addition, if you do not use the response as a string, you can access a Tempfile object at res.file, which contains the path to the raw downloaded request body.

Instance Attribute Summary collapse

Attributes included from AbstractResponse

#duration, #net_http_res

Instance Method Summary collapse

Methods included from AbstractResponse

beautify_headers, #code, #cookie_jar, #cookies, #description, #follow_get_redirection, #follow_redirection, #headers, #history, #log, #log_response, #raw_headers, #response_set_vars, #return!, #to_i

Constructor Details

#initialize(tempfile, net_http_res, request, start_time = nil) ⇒ RawResponse

Returns a new instance of RawResponse.

Parameters:

  • tempfile (Tempfile)

    The temporary file containing the body

  • net_http_res (Net::HTTPResponse)
  • request (RestClient::Request)
  • start_time (Time) (defaults to: nil)


26
27
28
29
30
31
32
33
# File 'lib/restclient/raw_response.rb', line 26

def initialize(tempfile, net_http_res, request, start_time=nil)
  @file = tempfile

  # reopen the tempfile so we can read it
  @file.open

  response_set_vars(net_http_res, request, start_time)
end

Instance Attribute Details

#end_timeObject (readonly)

Returns the value of attribute end_time.



16
17
18
# File 'lib/restclient/raw_response.rb', line 16

def end_time
  @end_time
end

#fileObject (readonly)

Returns the value of attribute file.



16
17
18
# File 'lib/restclient/raw_response.rb', line 16

def file
  @file
end

#requestObject (readonly)

Returns the value of attribute request.



16
17
18
# File 'lib/restclient/raw_response.rb', line 16

def request
  @request
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



16
17
18
# File 'lib/restclient/raw_response.rb', line 16

def start_time
  @start_time
end

Instance Method Details

#bodyObject



39
40
41
42
# File 'lib/restclient/raw_response.rb', line 39

def body
  @file.rewind
  @file.read
end

#inspectObject



18
19
20
# File 'lib/restclient/raw_response.rb', line 18

def inspect
  "<RestClient::RawResponse @code=#{code.inspect}, @file=#{file.inspect}, @request=#{request.inspect}>"
end

#sizeObject



44
45
46
# File 'lib/restclient/raw_response.rb', line 44

def size
  file.size
end

#to_sObject



35
36
37
# File 'lib/restclient/raw_response.rb', line 35

def to_s
  body
end