Class: RestMan::Response

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

Overview

A Response from RestMan, 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!, #success?, #to_i

Class Method Details

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

:include: _doc/lib/restman/response/create.rdoc



32
33
34
35
36
37
38
39
# File 'lib/restman/response.rb', line 32

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

:include: _doc/lib/restman/response/fix_encoding.rdoc



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/restman/response.rb', line 42

def self.fix_encoding(response)
  charset = RestMan::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

#bodyObject

:include: _doc/lib/restman/response/body.rdoc



10
11
12
13
14
15
# File 'lib/restman/response.rb', line 10

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



27
28
29
# File 'lib/restman/response.rb', line 27

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

#to_sObject

Convert the HTTP response body to a pure String object.



18
19
20
# File 'lib/restman/response.rb', line 18

def to_s
  body
end

#to_strObject

Convert the HTTP response body to a pure String object.



23
24
25
# File 'lib/restman/response.rb', line 23

def to_str
  body
end