Module: IPay::Util

Defined in:
lib/ipay/util.rb

Constant Summary collapse

ESCAPE_CHARS =
{ '&' => '&amp;', '"' => '&quot;', '<' => '&lt;', '>' => '&gt;', "'" => '&apos;' }

Class Method Summary collapse

Class Method Details

.escape(string) ⇒ Object



6
7
8
# File 'lib/ipay/util.rb', line 6

def self.escape(string)
  string.to_s.gsub(Regexp.new(ESCAPE_CHARS.keys.join('|')), ESCAPE_CHARS)
end

.hash_to_xml(h, l = 0) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ipay/util.rb', line 14

def self.hash_to_xml(h, l=0)
  return h.to_s unless h.is_a? Hash
  xml = ''
  h.each do |k,v|
    k = k.to_s.upcase
    xml << if v.kind_of? Enumerable
      (v.is_a?(Array) ? v : Array[v]).collect { |group| "#{"\t"*l}<#{k}>\n#{hash_to_xml(group, l+1)}#{"\t"*l}</#{k}>\n"}.join
    else
      "#{"\t"*l}<#{k}>#{self.escape(v)}</#{k}>\n"
    end
  end
  xml
end

.unescape(string) ⇒ Object



10
11
12
# File 'lib/ipay/util.rb', line 10

def self.unescape(string)
  string.gsub(Regexp.new(ESCAPE_CHARS.values.join('|')), ESCAPE_CHARS.invert)
end