Class: IyziPay::PkiBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/iyzipay/pki_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request_string = '') ⇒ PkiBuilder

Returns a new instance of PkiBuilder.



5
6
7
# File 'lib/iyzipay/pki_builder.rb', line 5

def initialize(request_string = '')
  @request_string = request_string
end

Instance Attribute Details

#request_stringObject

Returns the value of attribute request_string.



3
4
5
# File 'lib/iyzipay/pki_builder.rb', line 3

def request_string
  @request_string
end

Instance Method Details

#append(key, value = nil) ⇒ Object



22
23
24
25
26
27
# File 'lib/iyzipay/pki_builder.rb', line 22

def append(key, value = nil)
  unless value.nil?
    append_key_value(key, value)
  end
  self
end

#append_array(key, array = nil) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/iyzipay/pki_builder.rb', line 36

def append_array(key, array = nil)
  unless array.nil?
    appended_value = ''
    array.each do |value|
      appended_value << value.to_s
      appended_value << ', '
    end
  end
  append_key_value_array(key, appended_value)

  self
end

#append_key_value(key, value) ⇒ Object



49
50
51
# File 'lib/iyzipay/pki_builder.rb', line 49

def append_key_value(key, value)
  @request_string = "#{@request_string}#{key}=#{value}," unless value.nil?
end

#append_key_value_array(key, value) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/iyzipay/pki_builder.rb', line 53

def append_key_value_array(key, value)
  unless value.nil?
    sub = ', '
    value = value.gsub(/[#{sub}]+$/, '')
    @request_string = "#{@request_string}#{key}=[#{value}],"
  end

  self
end

#append_prefixObject



63
64
65
# File 'lib/iyzipay/pki_builder.rb', line 63

def append_prefix
  @request_string = "[#{@request_string}]"
end

#append_price(key, value = nil) ⇒ Object



29
30
31
32
33
34
# File 'lib/iyzipay/pki_builder.rb', line 29

def append_price(key, value = nil)
  unless value.nil?
    append_key_value(key, format_price(value))
  end
  self
end

#append_super(super_request_string) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/iyzipay/pki_builder.rb', line 9

def append_super(super_request_string)
  unless super_request_string.nil?

    s = super_request_string[1..-2]
    if s.length > 0
      result = @request_string + s
      result << ','
    end
    @request_string = result
  end
  self
end

#format_price(price) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/iyzipay/pki_builder.rb', line 79

def format_price(price)
  unless price.include? '.'
    price = price+'.0'
  end
  sub_str_index = 0
  price_reversed = price.reverse
  i=0
  while i < price.size do
    if price_reversed[i] == '0'
      sub_str_index = i + 1
    elsif price_reversed[i] == '.'
      price_reversed = '0' + price_reversed
      break
    else
      break
    end
    i+=1
  end
  (price_reversed[sub_str_index..-1]).reverse
end

#get_request_stringObject



72
73
74
75
76
77
# File 'lib/iyzipay/pki_builder.rb', line 72

def get_request_string
  remove_trailing_comma
  append_prefix

  @request_string
end

#remove_trailing_commaObject



67
68
69
70
# File 'lib/iyzipay/pki_builder.rb', line 67

def remove_trailing_comma
  sub = ','
  @request_string = @request_string.gsub(/[#{sub}]+$/, '')
end