rubysl-xmlrpc 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,40 @@
1
+ #
2
+ # $Id: config.rb 11708 2007-02-12 23:01:19Z shyouhei $
3
+ # Configuration file for XML-RPC for Ruby
4
+ #
5
+
6
+ module XMLRPC
7
+
8
+ module Config
9
+
10
+ DEFAULT_WRITER = XMLWriter::Simple # or XMLWriter::XMLParser
11
+
12
+ # available parser:
13
+ # * XMLParser::NQXMLTreeParser
14
+ # * XMLParser::NQXMLStreamParser
15
+ # * XMLParser::XMLTreeParser
16
+ # * XMLParser::XMLStreamParser (fastest)
17
+ # * XMLParser::REXMLStreamParser
18
+ # * XMLParser::XMLScanStreamParser
19
+ DEFAULT_PARSER = XMLParser::REXMLStreamParser
20
+
21
+ # enable <nil/> tag
22
+ ENABLE_NIL_CREATE = false
23
+ ENABLE_NIL_PARSER = false
24
+
25
+ # allows integers greater than 32-bit if true
26
+ ENABLE_BIGINT = false
27
+
28
+ # enable marshalling ruby objects which include XMLRPC::Marshallable
29
+ ENABLE_MARSHALLING = true
30
+
31
+ # enable multiCall extension by default
32
+ ENABLE_MULTICALL = false
33
+
34
+ # enable Introspection extension by default
35
+ ENABLE_INTROSPECTION = false
36
+
37
+ end
38
+
39
+ end
40
+
@@ -0,0 +1,290 @@
1
+ #
2
+ # Creates XML-RPC call/response documents
3
+ #
4
+ # Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
5
+ #
6
+ # $Id: create.rb 11818 2007-02-23 03:45:55Z knu $
7
+ #
8
+
9
+ require "date"
10
+ require "xmlrpc/base64"
11
+
12
+ module XMLRPC
13
+
14
+ module XMLWriter
15
+
16
+ class Abstract
17
+ def ele(name, *children)
18
+ element(name, nil, *children)
19
+ end
20
+
21
+ def tag(name, txt)
22
+ element(name, nil, text(txt))
23
+ end
24
+ end
25
+
26
+
27
+ class Simple < Abstract
28
+
29
+ def document_to_str(doc)
30
+ doc
31
+ end
32
+
33
+ def document(*params)
34
+ params.join("")
35
+ end
36
+
37
+ def pi(name, *params)
38
+ "<?#{name} " + params.join(" ") + " ?>"
39
+ end
40
+
41
+ def element(name, attrs, *children)
42
+ raise "attributes not yet implemented" unless attrs.nil?
43
+ if children.empty?
44
+ "<#{name}/>"
45
+ else
46
+ "<#{name}>" + children.join("") + "</#{name}>"
47
+ end
48
+ end
49
+
50
+ def text(txt)
51
+ cleaned = txt.dup
52
+ cleaned.gsub!(/&/, '&amp;')
53
+ cleaned.gsub!(/</, '&lt;')
54
+ cleaned.gsub!(/>/, '&gt;')
55
+ cleaned
56
+ end
57
+
58
+ end # class Simple
59
+
60
+
61
+ class XMLParser < Abstract
62
+
63
+ def initialize
64
+ require "xmltreebuilder"
65
+ end
66
+
67
+ def document_to_str(doc)
68
+ doc.to_s
69
+ end
70
+
71
+ def document(*params)
72
+ XML::SimpleTree::Document.new(*params)
73
+ end
74
+
75
+ def pi(name, *params)
76
+ XML::SimpleTree::ProcessingInstruction.new(name, *params)
77
+ end
78
+
79
+ def element(name, attrs, *children)
80
+ XML::SimpleTree::Element.new(name, attrs, *children)
81
+ end
82
+
83
+ def text(txt)
84
+ XML::SimpleTree::Text.new(txt)
85
+ end
86
+
87
+ end # class XMLParser
88
+
89
+ Classes = [Simple, XMLParser]
90
+
91
+ # yields an instance of each installed XML writer
92
+ def self.each_installed_writer
93
+ XMLRPC::XMLWriter::Classes.each do |klass|
94
+ begin
95
+ yield klass.new
96
+ rescue LoadError
97
+ end
98
+ end
99
+ end
100
+
101
+ end # module XMLWriter
102
+
103
+ class Create
104
+
105
+ def initialize(xml_writer = nil)
106
+ @writer = xml_writer || Config::DEFAULT_WRITER.new
107
+ end
108
+
109
+
110
+ def methodCall(name, *params)
111
+ name = name.to_s
112
+
113
+ if name !~ /[a-zA-Z0-9_.:\/]+/
114
+ raise ArgumentError, "Wrong XML-RPC method-name"
115
+ end
116
+
117
+ parameter = params.collect do |param|
118
+ @writer.ele("param", conv2value(param))
119
+ end
120
+
121
+ tree = @writer.document(
122
+ @writer.pi("xml", 'version="1.0"'),
123
+ @writer.ele("methodCall",
124
+ @writer.tag("methodName", name),
125
+ @writer.ele("params", *parameter)
126
+ )
127
+ )
128
+
129
+ @writer.document_to_str(tree) + "\n"
130
+ end
131
+
132
+
133
+
134
+ #
135
+ # generates a XML-RPC methodResponse document
136
+ #
137
+ # if is_ret == false then the params array must
138
+ # contain only one element, which is a structure
139
+ # of a fault return-value.
140
+ #
141
+ # if is_ret == true then a normal
142
+ # return-value of all the given params is created.
143
+ #
144
+ def methodResponse(is_ret, *params)
145
+
146
+ if is_ret
147
+ resp = params.collect do |param|
148
+ @writer.ele("param", conv2value(param))
149
+ end
150
+
151
+ resp = [@writer.ele("params", *resp)]
152
+ else
153
+ if params.size != 1 or params[0] === XMLRPC::FaultException
154
+ raise ArgumentError, "no valid fault-structure given"
155
+ end
156
+ resp = @writer.ele("fault", conv2value(params[0].to_h))
157
+ end
158
+
159
+
160
+ tree = @writer.document(
161
+ @writer.pi("xml", 'version="1.0"'),
162
+ @writer.ele("methodResponse", resp)
163
+ )
164
+
165
+ @writer.document_to_str(tree) + "\n"
166
+ end
167
+
168
+
169
+
170
+ #####################################
171
+ private
172
+ #####################################
173
+
174
+ #
175
+ # converts a Ruby object into
176
+ # a XML-RPC <value> tag
177
+ #
178
+ def conv2value(param)
179
+
180
+ val = case param
181
+ when Fixnum
182
+ @writer.tag("i4", param.to_s)
183
+
184
+ when Bignum
185
+ if Config::ENABLE_BIGINT
186
+ @writer.tag("i4", param.to_s)
187
+ else
188
+ if param >= -(2**31) and param <= (2**31-1)
189
+ @writer.tag("i4", param.to_s)
190
+ else
191
+ raise "Bignum is too big! Must be signed 32-bit integer!"
192
+ end
193
+ end
194
+ when TrueClass, FalseClass
195
+ @writer.tag("boolean", param ? "1" : "0")
196
+
197
+ when String
198
+ @writer.tag("string", param)
199
+
200
+ when Symbol
201
+ @writer.tag("string", param.to_s)
202
+
203
+ when NilClass
204
+ if Config::ENABLE_NIL_CREATE
205
+ @writer.ele("nil")
206
+ else
207
+ raise "Wrong type NilClass. Not allowed!"
208
+ end
209
+
210
+ when Float
211
+ @writer.tag("double", param.to_s)
212
+
213
+ when Struct
214
+ h = param.members.collect do |key|
215
+ value = param[key]
216
+ @writer.ele("member",
217
+ @writer.tag("name", key.to_s),
218
+ conv2value(value)
219
+ )
220
+ end
221
+
222
+ @writer.ele("struct", *h)
223
+
224
+ when Hash
225
+ # TODO: can a Hash be empty?
226
+
227
+ h = param.collect do |key, value|
228
+ @writer.ele("member",
229
+ @writer.tag("name", key.to_s),
230
+ conv2value(value)
231
+ )
232
+ end
233
+
234
+ @writer.ele("struct", *h)
235
+
236
+ when Array
237
+ # TODO: can an Array be empty?
238
+ a = param.collect {|v| conv2value(v) }
239
+
240
+ @writer.ele("array",
241
+ @writer.ele("data", *a)
242
+ )
243
+
244
+ when Time, Date, ::DateTime
245
+ @writer.tag("dateTime.iso8601", param.strftime("%Y%m%dT%H:%M:%S"))
246
+
247
+ when XMLRPC::DateTime
248
+ @writer.tag("dateTime.iso8601",
249
+ format("%.4d%02d%02dT%02d:%02d:%02d", *param.to_a))
250
+
251
+ when XMLRPC::Base64
252
+ @writer.tag("base64", param.encoded)
253
+
254
+ else
255
+ if Config::ENABLE_MARSHALLING and param.class.included_modules.include? XMLRPC::Marshallable
256
+ # convert Ruby object into Hash
257
+ ret = {"___class___" => param.class.name}
258
+ param.instance_variables.each {|v|
259
+ name = v[1..-1]
260
+ val = param.instance_variable_get(v)
261
+
262
+ if val.nil?
263
+ ret[name] = val if Config::ENABLE_NIL_CREATE
264
+ else
265
+ ret[name] = val
266
+ end
267
+ }
268
+ return conv2value(ret)
269
+ else
270
+ ok, pa = wrong_type(param)
271
+ if ok
272
+ return conv2value(pa)
273
+ else
274
+ raise "Wrong type!"
275
+ end
276
+ end
277
+ end
278
+
279
+ @writer.ele("value", val)
280
+ end
281
+
282
+ def wrong_type(value)
283
+ false
284
+ end
285
+
286
+
287
+ end # class Create
288
+
289
+ end # module XMLRPC
290
+
@@ -0,0 +1,142 @@
1
+ =begin
2
+ = xmlrpc/datetime.rb
3
+ Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
4
+
5
+ Released under the same term of license as Ruby.
6
+
7
+ = Classes
8
+ * ((<XMLRPC::DateTime>))
9
+
10
+ = XMLRPC::DateTime
11
+ == Description
12
+ This class is important to handle XMLRPC (('dateTime.iso8601')) values,
13
+ correcly, because normal UNIX-dates (class (({Date}))) only handle dates
14
+ from year 1970 on, and class (({Time})) handles dates without the time
15
+ component. (({XMLRPC::DateTime})) is able to store a XMLRPC
16
+ (('dateTime.iso8601')) value correctly.
17
+
18
+ == Class Methods
19
+ --- XMLRPC::DateTime.new( year, month, day, hour, min, sec )
20
+ Creates a new (({XMLRPC::DateTime})) instance with the
21
+ parameters ((|year|)), ((|month|)), ((|day|)) as date and
22
+ ((|hour|)), ((|min|)), ((|sec|)) as time.
23
+ Raises (({ArgumentError})) if a parameter is out of range, or ((|year|)) is not
24
+ of type (({Integer})).
25
+
26
+ == Instance Methods
27
+ --- XMLRPC::DateTime#year
28
+ --- XMLRPC::DateTime#month
29
+ --- XMLRPC::DateTime#day
30
+ --- XMLRPC::DateTime#hour
31
+ --- XMLRPC::DateTime#min
32
+ --- XMLRPC::DateTime#sec
33
+ Return the value of the specified date/time component.
34
+
35
+ --- XMLRPC::DateTime#mon
36
+ Alias for ((<XMLRPC::DateTime#month>)).
37
+
38
+ --- XMLRPC::DateTime#year=( value )
39
+ --- XMLRPC::DateTime#month=( value )
40
+ --- XMLRPC::DateTime#day=( value )
41
+ --- XMLRPC::DateTime#hour=( value )
42
+ --- XMLRPC::DateTime#min=( value )
43
+ --- XMLRPC::DateTime#sec=( value )
44
+ Set ((|value|)) as the new date/time component.
45
+ Raises (({ArgumentError})) if ((|value|)) is out of range, or in the case
46
+ of (({XMLRPC::DateTime#year=})) if ((|value|)) is not of type (({Integer})).
47
+
48
+ --- XMLRPC::DateTime#mon=( value )
49
+ Alias for ((<XMLRPC::DateTime#month=>)).
50
+
51
+ --- XMLRPC::DateTime#to_time
52
+ Return a (({Time})) object of the date/time which (({self})) represents.
53
+ If the (('year')) is below 1970, this method returns (({nil})),
54
+ because (({Time})) cannot handle years below 1970.
55
+ The used timezone is GMT.
56
+
57
+ --- XMLRPC::DateTime#to_date
58
+ Return a (({Date})) object of the date which (({self})) represents.
59
+ The (({Date})) object do ((*not*)) contain the time component (only date).
60
+
61
+ --- XMLRPC::DateTime#to_a
62
+ Returns all date/time components in an array.
63
+ Returns (({[year, month, day, hour, min, sec]})).
64
+ =end
65
+
66
+ require "date"
67
+
68
+ module XMLRPC
69
+
70
+ class DateTime
71
+
72
+ attr_reader :year, :month, :day, :hour, :min, :sec
73
+
74
+ def year= (value)
75
+ raise ArgumentError, "date/time out of range" unless value.is_a? Integer
76
+ @year = value
77
+ end
78
+
79
+ def month= (value)
80
+ raise ArgumentError, "date/time out of range" unless (1..12).include? value
81
+ @month = value
82
+ end
83
+
84
+ def day= (value)
85
+ raise ArgumentError, "date/time out of range" unless (1..31).include? value
86
+ @day = value
87
+ end
88
+
89
+ def hour= (value)
90
+ raise ArgumentError, "date/time out of range" unless (0..24).include? value
91
+ @hour = value
92
+ end
93
+
94
+ def min= (value)
95
+ raise ArgumentError, "date/time out of range" unless (0..59).include? value
96
+ @min = value
97
+ end
98
+
99
+ def sec= (value)
100
+ raise ArgumentError, "date/time out of range" unless (0..59).include? value
101
+ @sec = value
102
+ end
103
+
104
+ alias mon month
105
+ alias mon= month=
106
+
107
+
108
+ def initialize(year, month, day, hour, min, sec)
109
+ self.year, self.month, self.day = year, month, day
110
+ self.hour, self.min, self.sec = hour, min, sec
111
+ end
112
+
113
+ def to_time
114
+ if @year >= 1970
115
+ Time.gm(*to_a)
116
+ else
117
+ nil
118
+ end
119
+ end
120
+
121
+ def to_date
122
+ Date.new(*to_a[0,3])
123
+ end
124
+
125
+ def to_a
126
+ [@year, @month, @day, @hour, @min, @sec]
127
+ end
128
+
129
+ def ==(o)
130
+ Array(self) == Array(o)
131
+ end
132
+
133
+ end
134
+
135
+
136
+ end # module XMLRPC
137
+
138
+
139
+ =begin
140
+ = History
141
+ $Id: datetime.rb 11708 2007-02-12 23:01:19Z shyouhei $
142
+ =end