Method: Adyen::REST::Request#response_options

Defined in:
lib/adyen/rest/request.rb,
lib/adyen/rest/request.rb

#response_optionsHash

The options to send to the response class initializer.

Returns:

  • (Hash)


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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/adyen/rest/request.rb', line 31

class Request
  attr_reader :prefix, :form_data, :required_attributes
  attr_accessor :response_class, :response_options

  def initialize(action, attributes, options = {})
    @prefix = options[:prefix]
    @form_data = generate_form_data(action, attributes)

    @response_class   = options[:response_class]   || Adyen::REST::Response
    @response_options = options[:response_options] || {}

    @required_attributes = ['action']
  end

  # Returns the request's action
  # @return [String]
  def action
    form_data['action']
  end

  # Retrieves an attribute from the request
  def [](attribute)
    form_data[canonical_name(attribute)]
  end

  # Sets an attribute on the request
  def []=(attribute, value)
    form_data.merge!(flatten_attributes(attribute => value))
    value
  end

  def merchant_account=(value)
    self[:merchant_account] = value
  end

  # Runs validations on the request before it is sent.
  # @return [void]
  # @raises [Adyen::REST::RequestValidationFailed]
  def validate!
    required_attributes.each do |attribute|
      if form_data[attribute].nil? || form_data[attribute].empty?
        raise Adyen::REST::RequestValidationFailed, "#{attribute} is empty, but required!"
      end
    end
  end

  # Builds a Adyen::REST::Response instnace for a given Net::HTTP response.
  # @param http_response [Net::HTTPResponse] The HTTP response return for this request.
  # @return [Adyen::REST::Response] An instance of {Adyen::REST::Response}, or a subclass.
  def build_response(http_response)
    response_class.new(http_response, response_options)
  end

  protected

  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

  # Flattens the {#attributes} hash and converts all the keys to camelcase.
  # @return [Hash] A potentially nested hash of attributes.
  # @return [Hash<String, String>] A dictionary of API request attributes that
  #   can be included in an HTTP request as form data.
  def flatten_attributes(attributes)
    if prefix
      Adyen::Util.flatten(prefix => attributes)
    else
      Adyen::Util.flatten(attributes)
    end
  end

  def generate_form_data(action, attributes)
    flatten_attributes(attributes).merge('action' => action.to_s)
  end
end