Class: Adyen::REST::Request

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

Overview

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.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(action, attributes, options = {}) ⇒ Request



35
36
37
38
39
40
41
42
43
# File 'lib/adyen/rest/request.rb', line 35

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

Instance Attribute Details

#form_dataHash<String, String> (readonly)

The attributes to include in the API request as form data.



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

#prefixString (readonly)

The prefix to use for every request attribute (except action)



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

#required_attributesObject (readonly)

Returns the value of attribute required_attributes.



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

def required_attributes
  @required_attributes
end

#response_classClass

The response class to use to wrap the HTTP response to this request.



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

#response_optionsHash

The options to send to the response class initializer.



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

Instance Method Details

#[](attribute) ⇒ Object

Retrieves an attribute from the request



52
53
54
# File 'lib/adyen/rest/request.rb', line 52

def [](attribute)
  form_data[canonical_name(attribute)]
end

#[]=(attribute, value) ⇒ Object

Sets an attribute on the request



57
58
59
60
# File 'lib/adyen/rest/request.rb', line 57

def []=(attribute, value)
  form_data.merge!(flatten_attributes(attribute => value))
  value
end

#actionString

Returns the request’s action



47
48
49
# File 'lib/adyen/rest/request.rb', line 47

def action
  form_data['action']
end

#build_response(http_response) ⇒ Adyen::REST::Response

Builds a Adyen::REST::Response instnace for a given Net::HTTP response.



80
81
82
# File 'lib/adyen/rest/request.rb', line 80

def build_response(http_response)
  response_class.new(http_response, response_options)
end

#merchant_account=(value) ⇒ Object



62
63
64
# File 'lib/adyen/rest/request.rb', line 62

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

#validate!void

This method returns an undefined value.

Runs validations on the request before it is sent.



69
70
71
72
73
74
75
# File 'lib/adyen/rest/request.rb', line 69

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