Class: DPay::HTTP::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/dpay/http/response.rb,
sig/dpay/http/response.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status, headers, body) ⇒ Response

Returns a new instance of Response.

Parameters:

  • status (Integer)
  • headers (Hash[String, String])
  • body (String)


10
11
12
13
14
15
# File 'lib/dpay/http/response.rb', line 10

def initialize(status, headers, body)
  @status = status
  @headers = headers.each_with_object({}) { |(name, value), result| result[name.to_s.downcase] = value }.freeze
  @body = body
  freeze
end

Instance Attribute Details

#bodyString (readonly)

Returns the value of attribute body.

Returns:

  • (String)


8
9
10
# File 'lib/dpay/http/response.rb', line 8

def body
  @body
end

#headersHash[String, String] (readonly)

Returns the value of attribute headers.

Returns:

  • (Hash[String, String])


8
9
10
# File 'lib/dpay/http/response.rb', line 8

def headers
  @headers
end

#statusInteger (readonly)

Returns the value of attribute status.

Returns:

  • (Integer)


8
9
10
# File 'lib/dpay/http/response.rb', line 8

def status
  @status
end

Instance Method Details

#decode_jsonObject

Returns:

  • (Object)


21
22
23
24
25
26
# File 'lib/dpay/http/response.rb', line 21

def decode_json
  decoded = JSON.parse(@body.to_s)
  decoded.is_a?(Hash) || decoded.is_a?(Array) ? decoded : nil
rescue JSON::ParserError
  nil
end

#header(name) ⇒ String?

Parameters:

  • name (String)

Returns:

  • (String, nil)


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

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