module Transbank::Webpay::Helper

Constants

XS_DATE_TIME
XS_INTEGER

Public Instance Methods

camelcase(underscore) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

assin_values(hash, result, child) click to toggle source
# File lib/transbank/webpay/helper.rb, line 71
def assin_values(hash, result, child)
  return if child.name == 'text'

  key = underscore child.name
  value = typecasting result
  hash.store key, value
end
return_result?(child) click to toggle source
# File lib/transbank/webpay/helper.rb, line 61
def return_result?(child)
  child.next_sibling.nil? && child.previous_sibling.nil?
end
try_to_convert(value) { |value| ... } click to toggle source
# File lib/transbank/webpay/helper.rb, line 65
def try_to_convert(value)
  yield value
rescue ArgumentError
  value
end
xml_node_to_hash(node) click to toggle source
# File lib/transbank/webpay/helper.rb, line 46
def xml_node_to_hash(node)
  return Transbank::Webpay::Struct.new if node.nil?
  return node.content unless node.element?
  return if node.children.empty?

  hash = node.children.each_with_object({}) do |child, h|
    result = xml_node_to_hash(child)
    return result if return_result?(child)

    assin_values h, result, child
  end

  Transbank::Webpay::Struct.new hash
end