Class: Rapuncel::XmlRpc::Serializer

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

Constant Summary collapse

XML_ENCODING =
'UTF-8'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Serializer

Returns a new instance of Serializer.



11
12
13
14
15
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 11

def initialize object
  @builder = Nokogiri::XML::Builder.new :encoding => XML_ENCODING

  serialize object
end

Instance Attribute Details

#builderObject (readonly)

Returns the value of attribute builder.



9
10
11
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 9

def builder
  @builder
end

Class Method Details

.serialize(object) ⇒ Object Also known as: []



146
147
148
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 146

def serialize object
  new(object).to_xml
end

Instance Method Details

#serialize(object) ⇒ Object



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
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 17

def serialize object
  case object
  when Base64String
    serialize_base64 object
  when Array
    serialize_array object
  when String, Symbol
    serialize_string object.to_s
  when TrueClass
    serialize_true
  when FalseClass
    serialize_false
  when Float
    serialize_float object
  when BigDecimal
    serialize_big_decimal object
  when Hash
    serialize_hash object
  when Integer
    serialize_integer object
  when NilClass
    serialize_nil
  when Time
    serialize_time object
  when Request
    serialize_request object
  else
    if object.respond_to?(:acts_like?) && object.acts_like?(:time)
      serialize_time object
    elsif object.respond_to?(:to_xmlrpc)
      object.to_xmlrpc self
    else
      serialize_hash instance_variable_hash(object)
    end
  end

  self
end

#serialize_array(array) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 60

def serialize_array array
  builder.array do |builder|
    builder.data do |builder|
      array.each do |element|
        builder.value do |_|
          serialize element
        end
      end
    end
  end
end

#serialize_base64(string) ⇒ Object



56
57
58
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 56

def serialize_base64 string
  builder.base64 string.base64_encoded
end

#serialize_big_decimal(big_decimal) ⇒ Object



89
90
91
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 89

def serialize_big_decimal big_decimal
  builder.double big_decimal.to_s("F")
end

#serialize_falseObject Also known as: serialize_nil



80
81
82
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 80

def serialize_false
  builder.boolean "0"
end

#serialize_float(float) ⇒ Object



85
86
87
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 85

def serialize_float float
  builder.double float.to_s
end

#serialize_hash(hash) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 93

def serialize_hash hash
  builder.struct do |builder|
    hash.each_pair do |key, value|
      builder.member do |builder|
        # Get a better string representation of BigDecimals
        key = key.to_s("F") if BigDecimal === key
        builder.name key.to_s

        builder.value do |builder|
          serialize value
        end
      end
    end
  end
end

#serialize_integer(int) ⇒ Object



109
110
111
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 109

def serialize_integer int
  builder.int int.to_s
end

#serialize_request(request) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 117

def serialize_request request
  builder.methodCall do |builder|
    builder.methodName request.method_name
    builder.params do |builder|
      request.arguments.each do |argument|
        builder.param do |builder|
          builder.value do |builder|
            serialize argument
          end
        end
      end
    end
  end
end

#serialize_string(string) ⇒ Object



72
73
74
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 72

def serialize_string string
  builder.string string
end

#serialize_time(time) ⇒ Object



113
114
115
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 113

def serialize_time time
  builder.send "dateTime.iso8601", time.iso8601
end

#serialize_trueObject



76
77
78
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 76

def serialize_true
  builder.boolean "1"
end

#to_xmlObject



132
133
134
# File 'lib/rapuncel/xml_rpc/serializer.rb', line 132

def to_xml
  @builder.to_xml
end