Class: Savon::Transport::Response

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

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 = cookies
end

Instance Attribute Details

#bodyObject (readonly)

Returns the response body string.



61
62
63
# File 'lib/savon/transport/response.rb', line 61

def body
  @body
end

#codeObject (readonly)

Returns the HTTP status code.



55
56
57
# File 'lib/savon/transport/response.rb', line 55

def code
  @code
end

#cookiesObject (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
  @cookies
end

#headersObject (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.parse_cookies(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