Module: Transbank::Webpay::Helper

Included in:
Document, Params, Response
Defined in:
lib/transbank/webpay/helper.rb

Constant Summary collapse

XS_INTEGER =
/^[-+]?[1-9]([0-9]*)?$|^0$/
XS_DATE_TIME =
/^-?\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:?\d{2})?$/

Instance Method Summary collapse

Instance Method Details

#camelcase(underscore) ⇒ Object



7
8
9
10
11
12
# File 'lib/transbank/webpay/helper.rb', line 7

def camelcase(underscore)
  underscore
    .to_s
    .gsub(/(?<=_)(\w)/) { Regexp.last_match[1].upcase }
    .gsub(/(?:_)(\w)/, '\1')
end

#typecasting(value) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/transbank/webpay/helper.rb', line 24

def typecasting(value)
  case value
  when 'true'
    true
  when 'false'
    false
  when XS_DATE_TIME
    try_to_convert(value) { |v| DateTime.parse(v) }
  when XS_INTEGER
    try_to_convert(value) { |v| Integer v }
  else
    value
  end
end

#underscore(camelcase) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/transbank/webpay/helper.rb', line 14

def underscore(camelcase)
  camelcase
    .to_s
    .gsub(/::/, '/')
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end

#xml_to_hash(xml_io) ⇒ Object



39
40
41
42
# File 'lib/transbank/webpay/helper.rb', line 39

def xml_to_hash(xml_io)
  result = Nokogiri::XML(xml_io)
  xml_node_to_hash(result.root)
end