Class: SecureTrading::Util

Inherits:
Object
  • Object
show all
Defined in:
lib/secure_trading/util.rb

Class Method Summary collapse

Class Method Details

.stringify_values(hash) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/secure_trading/util.rb', line 5

def stringify_values(hash)
  hash.inject({}) do |options, (key,value)|
    if value.is_a?(Array)
      options[key] = value.first
    else
      options[key] = value
    end
    options
  end
end

.to_xml(hash, options = {}) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/secure_trading/util.rb', line 16

def to_xml(hash, options = {})
  options[:indent] ||= 2
  options = {:builder => Builder::XmlMarkup.new(:indent => options[:indent])}.merge(options)
    
  attributes      = options[:attributes] ||= {}
  root_attributes = attributes[options[:root].to_s] || {}
  options[:builder].__send__(:method_missing, options[:root].to_s, root_attributes) do
    hash.each do |key, value|
      case value
      when ::Hash
        to_xml(value, options.merge({ :root => key, :skip_instruct => true }))
      else
        if value.respond_to?(:to_hash)
          value = value.to_hash
        end
        if value.is_a?(Hash)
          Util.to_xml(value, options.merge({ :root => key, :skip_instruct => true }))
        else
          options[:builder].tag!(key.to_s, value)
        end
      end
    end

  end

end