class Adyen::REST::Request

The request object models an API request to be sent to Adyen's webservice.

Some API calls may use a subclass to model their request.

@!attribute prefix [r]

The prefix to use for every request attribute (except action)
@return [String]

@!attribute form_data [r]

The attributes to include in the API request as form data.
@return [Hash<String, String>] A dictionary of key value pairs

@!required_attributes [r]

The list of required attributes that should show up in the request.
{#validate!} will fail if any of these attributes is missing or empty.
@return [Array<String>]

@!attribute response_class [rw]

The response class to use to wrap the HTTP response to this request.
@return [Class]

@!attribute response_options [rw]

The options to send to the response class initializer.
@return [Hash]

@see Adyen::REST::Client @see Adyen::REST::Response

Attributes

form_data[R]
prefix[R]
required_attributes[R]
response_class[RW]
response_options[RW]

Public Class Methods

new(action, attributes, options = {}) click to toggle source
   # File lib/adyen/rest/request.rb
35 def initialize(action, attributes, options = {})
36   @prefix = options[:prefix]
37   @form_data = generate_form_data(action, attributes)
38 
39   @response_class   = options[:response_class]   || Adyen::REST::Response
40   @response_options = options[:response_options] || {}
41 
42   @required_attributes = ['action']
43 end

Public Instance Methods

[](attribute) click to toggle source

Retrieves an attribute from the request

   # File lib/adyen/rest/request.rb
52 def [](attribute)
53   form_data[canonical_name(attribute)]
54 end
[]=(attribute, value) click to toggle source

Sets an attribute on the request

   # File lib/adyen/rest/request.rb
57 def []=(attribute, value)
58   form_data.merge!(flatten_attributes(attribute => value))
59   value
60 end
action() click to toggle source

Returns the request's action @return [String]

   # File lib/adyen/rest/request.rb
47 def action
48   form_data['action']
49 end
build_response(http_response) click to toggle source

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.

   # File lib/adyen/rest/request.rb
80 def build_response(http_response)
81   response_class.new(http_response, response_options)
82 end
merchant_account=(value) click to toggle source
   # File lib/adyen/rest/request.rb
62 def merchant_account=(value)
63   self[:merchant_account] = value
64 end
validate!() click to toggle source

Runs validations on the request before it is sent. @return [void] @raises [Adyen::REST::RequestValidationFailed]

   # File lib/adyen/rest/request.rb
69 def validate!
70   required_attributes.each do |attribute|
71     if form_data[attribute].nil? || form_data[attribute].empty?
72       raise Adyen::REST::RequestValidationFailed, "#{attribute} is empty, but required!"
73     end
74   end
75 end

Protected Instance Methods

apply_prefix(name) click to toggle source
   # File lib/adyen/rest/request.rb
90 def apply_prefix(name)
91   prefix ? name.to_s.sub(/\A(?!#{Regexp.quote(prefix)}\.)/, "#{prefix}.") : name.to_s
92 end
canonical_name(name) click to toggle source
   # File lib/adyen/rest/request.rb
86 def canonical_name(name)
87   Adyen::Util.camelize(apply_prefix(name))
88 end
flatten_attributes(attributes) click to toggle source

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.
    # File lib/adyen/rest/request.rb
 98 def flatten_attributes(attributes)
 99   if prefix
100     Adyen::Util.flatten(prefix => attributes)
101   else
102     Adyen::Util.flatten(attributes)
103   end
104 end
generate_form_data(action, attributes) click to toggle source
    # File lib/adyen/rest/request.rb
106 def generate_form_data(action, attributes)
107   flatten_attributes(attributes).merge('action' => action.to_s)
108 end