Class: Recurly::HTTP::AdapterResponse

Inherits:
Object
  • Object
show all
Defined in:
lib/recurly/http/response.rb

Overview

The normalized response object every HTTP adapter must return from #call. It decouples the client from any particular transport library (+Net::HTTP+, Faraday, Typhoeus, ...).

Header lookup is case-insensitive: keys are downcased on construction and #[] downcases the lookup key.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code:, headers:, body:, reason_phrase: nil) ⇒ AdapterResponse

Returns a new instance of AdapterResponse.



23
24
25
26
27
28
29
30
# File 'lib/recurly/http/response.rb', line 23

def initialize(status_code:, headers:, body:, reason_phrase: nil)
  @status_code = status_code
  @headers = (headers || {}).each_with_object({}) do |(k, v), acc|
    acc[k.to_s.downcase] = v
  end
  @body = body
  @reason_phrase = reason_phrase
end

Instance Attribute Details

#bodyString? (readonly)

Returns the raw response body.

Returns:

  • (String, nil)

    the raw response body



17
18
19
# File 'lib/recurly/http/response.rb', line 17

def body
  @body
end

#headersHash (readonly)

Returns response headers, keyed by downcased name.

Returns:

  • (Hash)

    response headers, keyed by downcased name



14
15
16
# File 'lib/recurly/http/response.rb', line 14

def headers
  @headers
end

#reason_phraseString? (readonly)

Returns the HTTP reason phrase (e.g. "Bad Gateway"), if the adapter captured one. May be nil for adapters that do not surface it.

Returns:

  • (String, nil)

    the HTTP reason phrase (e.g. "Bad Gateway"), if the adapter captured one. May be nil for adapters that do not surface it.



21
22
23
# File 'lib/recurly/http/response.rb', line 21

def reason_phrase
  @reason_phrase
end

#status_codeInteger (readonly)

Returns the numeric HTTP status code (e.g. 200).

Returns:

  • (Integer)

    the numeric HTTP status code (e.g. 200)



11
12
13
# File 'lib/recurly/http/response.rb', line 11

def status_code
  @status_code
end

Instance Method Details

#[](name) ⇒ String?

Case-insensitive header lookup.

Parameters:

  • name (String)

Returns:

  • (String, nil)


35
36
37
# File 'lib/recurly/http/response.rb', line 35

def [](name)
  @headers[name.to_s.downcase]
end

#codeString

The status code as a String. Errors::APIError.from_response keys ERROR_MAP on the string status, so the object handed to it must expose a string #code.

Returns:

  • (String)


43
44
45
# File 'lib/recurly/http/response.rb', line 43

def code
  @status_code.to_s
end