Class: Response
- Inherits:
-
Object
- Object
- Response
- Defined in:
- lib/rct/response.rb
Overview
The Response object represents the response from one HTTP call. This Response class wraps the response object from the underlying HTTP client library and provides assorted convenience methods.
Client class methods will receive a Response object back from yield. Client class methods must return this Response object.
Currently using response from HTTPClient: rubydoc.info/gems/httpclient/HTTP/Message
Instance Method Summary collapse
-
#add_error(msg) ⇒ Object
—————————————————————————- Flag this reponse as an error and add an error message string.
-
#body ⇒ Object
—————————————————————————- Return HTTP response body (or nil if an error).
-
#header(name) ⇒ Object
—————————————————————————- Return requested HTTP header (or nil if an error).
-
#headers ⇒ Object
—————————————————————————- Return all HTTP headers (or nil if an error).
-
#initialize(res) ⇒ Response
constructor
—————————————————————————- Constructor…
-
#ok ⇒ Object
—————————————————————————- Returns true unless this response has been flagged as an error via add_error() method.
-
#status ⇒ Object
—————————————————————————- Return HTTP status code (or -1 if an error).
-
#to_s ⇒ Object
—————————————————————————- Return short string representation.
Constructor Details
#initialize(res) ⇒ Response
Constructor…
The ‘res’ argument must never be nil for successful responses. It may be nil if this represents an error response.
42 43 44 45 |
# File 'lib/rct/response.rb', line 42 def initialize(res) @res = res @fail_msg = nil end |
Instance Method Details
#add_error(msg) ⇒ Object
Flag this reponse as an error and add an error message string. This method may be called multiple times, the error messages are appended to each other.
53 54 55 56 57 58 59 |
# File 'lib/rct/response.rb', line 53 def add_error(msg) if (@fail_msg == nil) @fail_msg = msg else @fail_msg = "#{@fail_msg}; #{msg}" end end |
#body ⇒ Object
Return HTTP response body (or nil if an error)
102 103 104 105 |
# File 'lib/rct/response.rb', line 102 def body return nil if @res == nil return @res.content end |
#header(name) ⇒ Object
Return requested HTTP header (or nil if an error)
84 85 86 87 |
# File 'lib/rct/response.rb', line 84 def header(name) return nil if @res == nil return @res.header[name] end |
#headers ⇒ Object
Return all HTTP headers (or nil if an error)
93 94 95 96 |
# File 'lib/rct/response.rb', line 93 def headers return nil if @res == nil return @res.headers end |
#ok ⇒ Object
Returns true unless this response has been flagged as an error via add_error() method.
66 67 68 69 |
# File 'lib/rct/response.rb', line 66 def ok return true if @fail_msg == nil return false end |
#status ⇒ Object
Return HTTP status code (or -1 if an error)
75 76 77 78 |
# File 'lib/rct/response.rb', line 75 def status return -1 if @res == nil return @res.status end |
#to_s ⇒ Object
Return short string representation. On error, contains the error message(s). Otherwise, contains the HTTP response code and text description.
113 114 115 116 117 118 119 120 121 |
# File 'lib/rct/response.rb', line 113 def to_s return "error: #{@fail_msg}" if @res == nil rv = "#{@res.status} #{@res.reason}" if (@fail_msg != nil) rv += " (#{@fail_msg})" end return rv end |