Class: WirecardCheckoutPage::Request

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

Direct Known Subclasses

InitRequest, Toolkit::Request

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url: nil, params: {}) ⇒ Request

Returns a new instance of Request.


18
19
20
21
# File 'lib/wirecard_checkout_page/request.rb', line 18

def initialize(url: nil, params: {})
  @url = url
  params.each { |param, value| send "#{param}=", value }
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors


27
28
29
# File 'lib/wirecard_checkout_page/request.rb', line 27

def errors
  @errors
end

#paramsObject (readonly)

Returns the value of attribute params


25
26
27
# File 'lib/wirecard_checkout_page/request.rb', line 25

def params
  @params
end

#urlObject (readonly)

Returns the value of attribute url


23
24
25
# File 'lib/wirecard_checkout_page/request.rb', line 23

def url
  @url
end

Class Method Details

.param(name, options = {}) ⇒ Object


3
4
5
6
7
8
# File 'lib/wirecard_checkout_page/request.rb', line 3

def self.param(name, options = {})
  name = name.to_sym
  params_order << name
  params[name] = options
  attr_accessor name
end

.paramsObject


10
11
12
# File 'lib/wirecard_checkout_page/request.rb', line 10

def self.params
  @params ||= {}
end

.params_orderObject


14
15
16
# File 'lib/wirecard_checkout_page/request.rb', line 14

def self.params_order
  @params_order ||= []
end

Instance Method Details

#bodyObject


39
40
41
# File 'lib/wirecard_checkout_page/request.rb', line 39

def body
  fingerprinted_request_params
end

#callObject


43
44
45
# File 'lib/wirecard_checkout_page/request.rb', line 43

def call
  raise NotImplementedError, '#call not implemented'
end

#fingerprintObject


51
52
53
# File 'lib/wirecard_checkout_page/request.rb', line 51

def fingerprint
  Digest::MD5.hexdigest fingerprint_string
end

#fingerprint_orderObject


55
56
57
58
59
# File 'lib/wirecard_checkout_page/request.rb', line 55

def fingerprint_order
  self.class.params_order.select do |param|
    attributes[param][:required] || send(param).to_s != ''
  end
end

#fingerprint_stringObject


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

def fingerprint_string
  fingerprint_order.each_with_object('') { |param, str| str << send(param).to_s }
end

#fingerprinted_request_paramsObject


72
73
74
75
76
77
# File 'lib/wirecard_checkout_page/request.rb', line 72

def fingerprinted_request_params
  request_params.merge(
    'requestFingerprint'      => fingerprint,
    'requestFingerprintOrder' => fingerprint_order.join(',')
  )
end

#request_paramsObject


61
62
63
64
65
66
67
68
69
70
# File 'lib/wirecard_checkout_page/request.rb', line 61

def request_params
  rp = {}
  attributes.keys.each do |param|
    next if param == :secret
    val = send(param).to_s
    next if val == ''
    rp[param.to_s] = val
  end
  rp
end

#valid?Boolean

Returns:

  • (Boolean)

29
30
31
32
33
34
35
36
37
# File 'lib/wirecard_checkout_page/request.rb', line 29

def valid?
  @errors = []
  attributes.each do |param, options|
    next unless options[:required] == true
    val = send param
    @errors << "#{param} is required" if val.nil? || val == ''
  end
  @errors.empty?
end