Class: RubyPsigate::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_psigate/serializer.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, options = { :header => false }) ⇒ Serializer

Returns a new instance of Serializer.

Raises:

  • (ArgumentError)


4
5
6
7
8
# File 'lib/ruby_psigate/serializer.rb', line 4

def initialize(hash, options = { :header => false })
  raise ArgumentError unless hash.is_a?(Hash)
  @hash = hash
  @header = options[:header]
end

Instance Method Details

#to_xmlObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ruby_psigate/serializer.rb', line 10

def to_xml
  @builder = Builder::XmlMarkup.new
  @builder.instruct! if @header

  for key, value in @hash
    case value
    when String
      @builder.tag!(key.to_sym, value)
    when Hash
      @builder.tag!(key.to_sym) do
        @builder << Serializer.new(value).to_xml
      end
    when Array
      values = value
      values.each do |val|
        @builder.tag!(key.to_sym) do
          @builder << Serializer.new(val).to_xml
        end
      end
    when NilClass
      # do nothing
    else
      raise ArgumentError, "Unknown class: #{value.class}"
    end
  end
  
  @builder.target!
end