Class: Mailkube::HttpResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/mailkube/transport.rb,
sig/mailkube/transport.rbs

Overview

One HTTP response, in the shape every adapter returns.

headers are stored exactly as the adapter supplied them, and read through #header, which matches case-insensitively as HTTP requires.

Unlike the rest of this file, this class is public: it is the return type of the http: adapter contract, so any adapter you write has to construct one.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status:, headers: {}, body: "") ⇒ HttpResponse

Returns a new instance of HttpResponse.

Parameters:

  • status (Integer)

    the HTTP status code.

  • headers (Hash{String => String}) (defaults to: {})

    response headers, in any casing.

  • body (String) (defaults to: "")

    the raw response body.

  • status: (Integer)
  • headers: (Hash[String, String]) (defaults to: {})
  • body: (String) (defaults to: "")


34
# File 'lib/mailkube/transport.rb', line 34

def initialize(status:, headers: {}, body: "") = super

Instance Attribute Details

#bodyString (readonly)

Returns the value of attribute body.

Returns:

  • (String)


16
17
18
# File 'sig/mailkube/transport.rbs', line 16

def body
  @body
end

#headersHash[String, String] (readonly)

Returns the value of attribute headers.

Returns:

  • (Hash[String, String])


15
16
17
# File 'sig/mailkube/transport.rbs', line 15

def headers
  @headers
end

#statusInteger (readonly)

Returns the value of attribute status.

Returns:

  • (Integer)


14
15
16
# File 'sig/mailkube/transport.rbs', line 14

def status
  @status
end

Instance Method Details

#header(name) ⇒ String?

Look a header up, case-insensitively.

Both sides are downcased, rather than downcasing the argument and indexing a map assumed to be lowercase already. That assumption used to be this class's documented contract, and since the class is public — the http: seam means third-party adapters construct it — it was an invariant the SDK asked for and could not enforce. An adapter that passed X-Request-Id through in the server's own casing produced a silent nil for every header the SDK reads, which is exactly how the request id could have stayed empty even after the gateway started sending it. A scan over a handful of pairs costs nothing and cannot be got wrong from outside.

Parameters:

  • name (String)

    a header name in any casing.

  • (String)

Returns:

  • (String, nil)

    the header value, or nil when absent.



49
50
51
52
53
# File 'lib/mailkube/transport.rb', line 49

def header(name)
  wanted = name.downcase
  headers.each { |key, value| return value if key.downcase == wanted }
  nil
end