Class: RestClient::Response

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

Overview

A Response from RestClient, you can access the response body, the code or the headers.

Instance Attribute Summary

Attributes included from AbstractResponse

#duration, #end_time, #net_http_res, #request, #start_time

Class Method Summary collapse

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

Class Method Details

.create(body, net_http_res, request, start_time = nil) ⇒ Object

Initialize a Response object. Because RestClient::Response is (unfortunately) a subclass of String for historical reasons, Response.create is the preferred initializer.

Parameters:

  • body (String, nil)

    The response body from the Net::HTTPResponse

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


49
50
51
52
53
54
55
56
# File 'lib/restclient/response.rb', line 49

def self.create(body, net_http_res, request, start_time=nil)
  result = self.new(body || '')

  result.response_set_vars(net_http_res, request, start_time)
  fix_encoding(result)

  result
end

.fix_encoding(response) ⇒ Object

Set the String encoding according to the ‘Content-Type: charset’ header, if possible.



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/restclient/response.rb', line 60

def self.fix_encoding(response)
  charset = RestClient::Utils.get_encoding_from_headers(response.headers)
  encoding = nil

  begin
    encoding = Encoding.find(charset) if charset
  rescue ArgumentError
    if response.log
      response.log << "No such encoding: #{charset.inspect}"
    end
  end

  return unless encoding

  response.force_encoding(encoding)

  response
end

Instance Method Details

#bodyString

Return the HTTP response body.

Future versions of RestClient will deprecate treating response objects directly as strings, so it will be necessary to call ‘.body`.

Returns:

  • (String)


16
17
18
19
20
21
# File 'lib/restclient/response.rb', line 16

def body
  # Benchmarking suggests that "#{self}" is fastest, and that caching the
  # body string in an instance variable doesn't make it enough faster to be
  # worth the extra memory storage.
  String.new(self)
end

#inspectObject



37
38
39
# File 'lib/restclient/response.rb', line 37

def inspect
  "<RestClient::Response #{code.inspect} #{body_truncated(10).inspect}>"
end

#to_sString

Convert the HTTP response body to a pure String object.

Returns:

  • (String)


26
27
28
# File 'lib/restclient/response.rb', line 26

def to_s
  body
end

#to_strString

Convert the HTTP response body to a pure String object.

Returns:

  • (String)


33
34
35
# File 'lib/restclient/response.rb', line 33

def to_str
  body
end