Class: Adyen::REST::Response

Inherits:
Object
  • Object
show all
Defined in:
lib/adyen/rest/response.rb

Overview

The Response class models the HTTP response that is the result of a API call to Adyen’s REST webservice.

Some API calls may respond with an instance of a subclass, to make dealing with the response easier.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response, options = {}) ⇒ Response



24
25
26
27
28
# File 'lib/adyen/rest/response.rb', line 24

def initialize(http_response, options = {})
  @http_response = http_response
  @prefix = options.key?(:prefix) ? options[:prefix].to_s : nil
  @attributes = parse_response_attributes
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



22
23
24
# File 'lib/adyen/rest/response.rb', line 22

def attributes
  @attributes
end

#http_responseNet::HTTPResponse (readonly)

The underlying net/http response.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/adyen/rest/response.rb', line 21

class Response
  attr_reader :http_response, :prefix, :attributes

  def initialize(http_response, options = {})
    @http_response = http_response
    @prefix = options.key?(:prefix) ? options[:prefix].to_s : nil
    @attributes = parse_response_attributes
  end

  # Looks up an attribute in the response.
  # @return [String, nil] The value of the attribute if it was included in the response.
  def [](name)
    attributes[canonical_name(name)]
  end

  def has_attribute?(name)
    attributes.has_key?(canonical_name(name))
  end

  def psp_reference
    Integer(self[:psp_reference])
  end

  protected

  def map_response_list(response_prefix, mapped_attributes)
    list  = []
    index = 0

    loop do
      response = {}
      mapped_attributes.each do |key, value|
        new_value = attributes["#{response_prefix}.#{index.to_s}.#{value}"]
        response[key] = new_value unless new_value.empty?
      end

      index += 1
      break unless response.any?
      list << response
    end

    list
  end

  def canonical_name(name)
    Adyen::Util.camelize(apply_prefix(name))
  end

  def apply_prefix(name)
    prefix ? name.to_s.sub(/\A(?!#{Regexp.quote(prefix)}\.)/, "#{prefix}.") : name.to_s
  end

  def parse_response_attributes
    attributes = CGI.parse(http_response.body)
    attributes.each { |key, values| attributes[key] = values.first }
    attributes
  end
end

#prefixString (readonly)

The prefix to use when reading attributes from the response



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/adyen/rest/response.rb', line 21

class Response
  attr_reader :http_response, :prefix, :attributes

  def initialize(http_response, options = {})
    @http_response = http_response
    @prefix = options.key?(:prefix) ? options[:prefix].to_s : nil
    @attributes = parse_response_attributes
  end

  # Looks up an attribute in the response.
  # @return [String, nil] The value of the attribute if it was included in the response.
  def [](name)
    attributes[canonical_name(name)]
  end

  def has_attribute?(name)
    attributes.has_key?(canonical_name(name))
  end

  def psp_reference
    Integer(self[:psp_reference])
  end

  protected

  def map_response_list(response_prefix, mapped_attributes)
    list  = []
    index = 0

    loop do
      response = {}
      mapped_attributes.each do |key, value|
        new_value = attributes["#{response_prefix}.#{index.to_s}.#{value}"]
        response[key] = new_value unless new_value.empty?
      end

      index += 1
      break unless response.any?
      list << response
    end

    list
  end

  def canonical_name(name)
    Adyen::Util.camelize(apply_prefix(name))
  end

  def apply_prefix(name)
    prefix ? name.to_s.sub(/\A(?!#{Regexp.quote(prefix)}\.)/, "#{prefix}.") : name.to_s
  end

  def parse_response_attributes
    attributes = CGI.parse(http_response.body)
    attributes.each { |key, values| attributes[key] = values.first }
    attributes
  end
end

Instance Method Details

#[](name) ⇒ String?

Looks up an attribute in the response.



32
33
34
# File 'lib/adyen/rest/response.rb', line 32

def [](name)
  attributes[canonical_name(name)]
end

#has_attribute?(name) ⇒ Boolean



36
37
38
# File 'lib/adyen/rest/response.rb', line 36

def has_attribute?(name)
  attributes.has_key?(canonical_name(name))
end

#psp_referenceObject



40
41
42
# File 'lib/adyen/rest/response.rb', line 40

def psp_reference
  Integer(self[:psp_reference])
end