Class: Ingenico::Connect::SDK::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/ingenico/connect/sdk/response.rb

Overview

Base class for HTTP responses. It provides access to the response headers, status code and response message body

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status_code, body, headers) ⇒ Response

Create a new response object that stores the following:

status_code

HTTP status code

body

response message body, given as a string

headers

response headers as a Ingenico::Connect::SDK::ResponseHeader list



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ingenico/connect/sdk/response.rb', line 10

def initialize(status_code, body, headers)
  @status_code = status_code
  @body = body
  @headers = if headers.nil? or headers.empty?
               {}
              else
                headers.inject({}) do |hash, header|
                  hash[header.name.downcase.to_sym] = header.dup.freeze
                  hash
                end
              end.freeze
end

Instance Attribute Details

#bodyObject (readonly)

Response message body



27
28
29
# File 'lib/ingenico/connect/sdk/response.rb', line 27

def body
  @body
end

#status_codeObject (readonly)

HTTP status code



24
25
26
# File 'lib/ingenico/connect/sdk/response.rb', line 24

def status_code
  @status_code
end

Instance Method Details

#get_header(header_name) ⇒ Object

Returns the Ingenico::Connect::SDK::ResponseHeader that goes by the given header_name, or nil if this Response does not contain a header with the given name.



36
37
38
# File 'lib/ingenico/connect/sdk/response.rb', line 36

def get_header(header_name)
  @headers[header_name.downcase.to_sym]
end

#get_header_value(header_name) ⇒ Object

Returns the header value of the header that goes by the given header_name, or nil if this Response does not contain a header with the given name.



42
43
44
45
46
47
48
49
# File 'lib/ingenico/connect/sdk/response.rb', line 42

def get_header_value(header_name)
  header = get_header(header_name)
  unless header.nil?
    header.value
  else
    nil
  end
end

#headersObject

Response headers as a Ingenico::Connect::SDK::ResponseHeader list



30
31
32
# File 'lib/ingenico/connect/sdk/response.rb', line 30

def headers
  @headers.values
end

#to_sObject



51
52
53
54
55
56
57
# File 'lib/ingenico/connect/sdk/response.rb', line 51

def to_s
  str = self.class.name
  str << "[status_code=#{@status_code}"
  str << ",body='#{@body}'" unless @body.nil? or @body.empty?
  str << ",headers=#{headers}" unless headers.nil? or headers.empty?
  str << ']'
end