Module: XMLRPC::Convert
- Defined in:
- lib/xmlrpc/parser.rb
Overview
Helper class used to convert types.
Class Method Summary collapse
-
.base64(str) ⇒ Object
Decodes the given
str
using XMLRPC::Base64.decode. -
.boolean(str) ⇒ Object
Converts a String to
true
orfalse
. -
.dateTime(str) ⇒ Object
Converts a the given
str
to adateTime.iso8601
formatted date. -
.double(str) ⇒ Object
Converts a String to a Float.
-
.fault(hash) ⇒ Object
Converts the given
hash
to an XMLRPC::FaultException object by passing thefaultCode
andfaultString
attributes of the Hash to XMLRPC::FaultException.new. -
.int(str) ⇒ Object
Converts a String to an Integer.
-
.struct(hash) ⇒ Object
Converts the given
hash
to a marshalled object.
Class Method Details
.base64(str) ⇒ Object
Decodes the given str
using XMLRPC::Base64.decode
136 137 138 |
# File 'lib/xmlrpc/parser.rb', line 136 def self.base64(str) XMLRPC::Base64.decode(str) end |
.boolean(str) ⇒ Object
Converts a String to true
or false
Raises an exception if str
is not 0
or 1
84 85 86 87 88 89 90 91 |
# File 'lib/xmlrpc/parser.rb', line 84 def self.boolean(str) case str when "0" then false when "1" then true else raise "RPC-value of type boolean is wrong" end end |
.dateTime(str) ⇒ Object
Converts a the given str
to a dateTime.iso8601
formatted date.
Raises an exception if the String isn’t in dateTime.iso8601
format.
See also, XMLRPC::DateTime
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/xmlrpc/parser.rb', line 105 def self.dateTime(str) case str when /^(-?\d\d\d\d)-?(\d\d)-?(\d\d)T(\d\d):(\d\d):(\d\d)(?:Z|([+-])(\d\d):?(\d\d))?$/ a = [$1, $2, $3, $4, $5, $6].collect{|i| i.to_i} if $7 ofs = $8.to_i*3600 + $9.to_i*60 ofs = -ofs if $7=='+' utc = Time.utc(*a) + ofs a = [ utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec ] end XMLRPC::DateTime.new(*a) when /^(-?\d\d)-?(\d\d)-?(\d\d)T(\d\d):(\d\d):(\d\d)(Z|([+-]\d\d):(\d\d))?$/ a = [$1, $2, $3, $4, $5, $6].collect{|i| i.to_i} if a[0] < 70 a[0] += 2000 else a[0] += 1900 end if $7 ofs = $8.to_i*3600 + $9.to_i*60 ofs = -ofs if $7=='+' utc = Time.utc(*a) + ofs a = [ utc.year, utc.month, utc.day, utc.hour, utc.min, utc.sec ] end XMLRPC::DateTime.new(*a) else raise "wrong dateTime.iso8601 format " + str end end |
.double(str) ⇒ Object
Converts a String to a Float
See also String.to_f
96 97 98 |
# File 'lib/xmlrpc/parser.rb', line 96 def self.double(str) str.to_f end |
.fault(hash) ⇒ Object
Converts the given hash
to an XMLRPC::FaultException object by passing the faultCode
and faultString
attributes of the Hash to XMLRPC::FaultException.new
Raises an Exception if the given hash
doesn’t meet the requirements. Those requirements being:
-
2 keys
-
'faultCode'
key is an Integer -
'faultString'
key is a String
175 176 177 178 179 180 181 182 183 184 |
# File 'lib/xmlrpc/parser.rb', line 175 def self.fault(hash) if hash.kind_of? Hash and hash.size == 2 and hash.has_key? "faultCode" and hash.has_key? "faultString" and hash["faultCode"].kind_of? Integer and hash["faultString"].kind_of? String XMLRPC::FaultException.new(hash["faultCode"], hash["faultString"]) else raise "wrong fault-structure: #{hash.inspect}" end end |
.int(str) ⇒ Object
Converts a String to an Integer
See also String.to_i
77 78 79 |
# File 'lib/xmlrpc/parser.rb', line 77 def self.int(str) str.to_i end |
.struct(hash) ⇒ Object
Converts the given hash
to a marshalled object.
Returns the given hash
if an exception occurs.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/xmlrpc/parser.rb', line 143 def self.struct(hash) # convert to marshalled object klass = hash["___class___"] if klass.nil? or Config::ENABLE_MARSHALLING == false hash else begin mod = Module klass.split("::").each {|const| mod = mod.const_get(const.strip)} obj = mod.allocate hash.delete "___class___" hash.each {|key, value| obj.instance_variable_set("@#{ key }", value) if key =~ /^([a-zA-Z_]\w*)$/ } obj rescue hash end end end |