Class: Rapuncel::XmlRpc::Deserializer

Inherits:
Object
  • Object
show all
Defined in:
lib/rapuncel/xml_rpc/deserializer.rb

Constant Summary collapse

XML_ENCODING =
'UTF-8'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(xml) ⇒ Deserializer

Returns a new instance of Deserializer.



6
7
8
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 6

def initialize xml
  @xml_doc = Nokogiri::XML.parse xml
end

Class Attribute Details

.double_as_bigdecimalObject

Returns the value of attribute double_as_bigdecimal.



104
105
106
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 104

def double_as_bigdecimal
  @double_as_bigdecimal
end

.hash_keys_as_stringObject

Returns the value of attribute hash_keys_as_string.



104
105
106
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 104

def hash_keys_as_string
  @hash_keys_as_string
end

Class Method Details

.deserialize(xml) ⇒ Object Also known as: []



106
107
108
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 106

def deserialize xml
  new(xml).to_ruby
end

Instance Method Details

#deserialize(xml_node = root_node) ⇒ Object



10
11
12
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 10

def deserialize xml_node = root_node
  send "deserialize_#{xml_node.name}", xml_node
end

#deserialize_array(xml_node) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 22

def deserialize_array xml_node
  values = xml_node.first_element_child.element_children

  values.map do |value|
    deserialize value.first_element_child
  end
end

#deserialize_base64(xml_node) ⇒ Object



14
15
16
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 14

def deserialize_base64 xml_node
  Base64String.decode_base64 xml_node.text.strip
end

#deserialize_boolean(xml_node) ⇒ Object



30
31
32
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 30

def deserialize_boolean xml_node
  xml_node.text == "1"
end

#deserialize_double(xml_node) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 34

def deserialize_double xml_node
  text = xml_node.text.strip

  if double_as_bigdecimal?
    BigDecimal.new text
  else
    text.to_f
  end
end

#deserialize_int(xml_node) ⇒ Object Also known as: deserialize_i4



59
60
61
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 59

def deserialize_int xml_node
  xml_node.text.strip.to_i
end

#deserialize_methodResponse(xml_node) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 68

def deserialize_methodResponse xml_node
  response = xml_node.first_element_child

  if response.name == 'fault'
    deserialize_methodResponse_fault response
  else
    deserialize_methodResponse_params response
  end
end

#deserialize_methodResponse_fault(xml_node) ⇒ Object



82
83
84
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 82

def deserialize_methodResponse_fault xml_node
  deserialize xml_node.first_element_child.first_element_child
end

#deserialize_methodResponse_params(xml_node) ⇒ Object



78
79
80
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 78

def deserialize_methodResponse_params xml_node
  deserialize xml_node.first_element_child.first_element_child.first_element_child
end

#deserialize_string(xml_node) ⇒ Object



18
19
20
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 18

def deserialize_string xml_node
  xml_node.text.gsub(/(\r\n|\r)/, "\n")
end

#deserialize_struct(xml_node) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 44

def deserialize_struct xml_node
  keys_and_values = xml_node.element_children

  {}.tap do |hash|
    keys_and_values.each do |key_value|
      key = key_value.first_element_child.text.strip
      key = key.to_sym unless hash_keys_as_string?
    
      value = deserialize key_value.last_element_child.first_element_child

      hash[key] = value
    end
  end      
end

#to_rubyObject



86
87
88
# File 'lib/rapuncel/xml_rpc/deserializer.rb', line 86

def to_ruby
  deserialize
end