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

#net_http_res, #request

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, #raw_headers, #response_set_vars, #return!, #to_i

Class Method Details

.create(body, net_http_res, request) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/restclient/response.rb', line 41

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

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

  result
end

.fix_encoding(response) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/restclient/response.rb', line 50

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 RestClient.log
      RestClient.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