Class: Savon::Transport::Response
- Inherits:
-
Object
- Object
- Savon::Transport::Response
- Defined in:
- lib/savon/transport/response.rb
Overview
Transport-agnostic HTTP response value object.
Every transport produces a Transport::Response so that higher-level code never depends on transport-specific code. Immutable once constructed.
The shape of #cookies is transport-specific: HTTPI responses expose an Array of HTTPI::Cookie, while Faraday responses expose a plain Hash so Faraday users do not depend on HTTPI types.
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the response body string.
-
#code ⇒ Object
readonly
Returns the HTTP status code.
-
#cookies ⇒ Object
readonly
Returns the parsed cookies in a transport-specific shape.
-
#headers ⇒ Object
readonly
Returns the response headers hash.
Class Method Summary collapse
-
.from_faraday(faraday_response) ⇒ Transport::Response
Builds a Response from a Faraday::Response.
-
.from_httpi(httpi_response) ⇒ Transport::Response
Builds a Response from an HTTPI::Response.
Instance Method Summary collapse
-
#error? ⇒ Boolean
Returns true when the HTTP status code indicates an error (>= 300).
-
#initialize(code, headers, body, cookies: nil) ⇒ Response
constructor
A new instance of Response.
Constructor Details
#initialize(code, headers, body, cookies: nil) ⇒ Response
47 48 49 50 51 52 |
# File 'lib/savon/transport/response.rb', line 47 def initialize(code, headers, body, cookies: nil) @code = code @headers = headers @body = body @cookies = end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the response body string.
61 62 63 |
# File 'lib/savon/transport/response.rb', line 61 def body @body end |
#code ⇒ Object (readonly)
Returns the HTTP status code.
55 56 57 |
# File 'lib/savon/transport/response.rb', line 55 def code @code end |
#cookies ⇒ Object (readonly)
Returns the parsed cookies in a transport-specific shape. See class-level docs.
65 66 67 |
# File 'lib/savon/transport/response.rb', line 65 def @cookies end |
#headers ⇒ Object (readonly)
Returns the response headers hash.
58 59 60 |
# File 'lib/savon/transport/response.rb', line 58 def headers @headers end |
Class Method Details
.from_faraday(faraday_response) ⇒ Transport::Response
Builds a Response from a Faraday::Response.
34 35 36 37 38 39 40 41 |
# File 'lib/savon/transport/response.rb', line 34 def self.from_faraday(faraday_response) new( faraday_response.status, faraday_response.headers.to_h, faraday_response.body, cookies: Savon::Transport::Faraday.(faraday_response.headers) ) end |
.from_httpi(httpi_response) ⇒ Transport::Response
Builds a Response from an HTTPI::Response.
21 22 23 24 25 26 27 28 |
# File 'lib/savon/transport/response.rb', line 21 def self.from_httpi(httpi_response) new( httpi_response.code, httpi_response.headers, httpi_response.body, cookies: ::HTTPI::Cookie.list_from_headers(httpi_response.headers) ) end |
Instance Method Details
#error? ⇒ Boolean
Returns true when the HTTP status code indicates an error (>= 300).
68 69 70 |
# File 'lib/savon/transport/response.rb', line 68 def error? @code >= 300 end |