Class: RestClient::Response
- Inherits:
-
String
- Object
- String
- RestClient::Response
- 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
-
.create(body, net_http_res, request, start_time = nil) ⇒ Object
Initialize a Response object.
-
.fix_encoding(response) ⇒ Object
Set the String encoding according to the ‘Content-Type: charset’ header, if possible.
Instance Method Summary collapse
-
#body ⇒ String
Return the HTTP response body.
- #inspect ⇒ Object
-
#to_s ⇒ String
Convert the HTTP response body to a pure String object.
-
#to_str ⇒ String
Convert the HTTP response body to a pure String object.
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.
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
#body ⇒ String
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`.
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 |
#inspect ⇒ Object
37 38 39 |
# File 'lib/restclient/response.rb', line 37 def inspect "<RestClient::Response #{code.inspect} #{body_truncated(10).inspect}>" end |
#to_s ⇒ String
Convert the HTTP response body to a pure String object.
26 27 28 |
# File 'lib/restclient/response.rb', line 26 def to_s body end |
#to_str ⇒ String
Convert the HTTP response body to a pure String object.
33 34 35 |
# File 'lib/restclient/response.rb', line 33 def to_str body end |