troelskn-handsoap 0.3.1 → 0.3.2

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.
Files changed (3) hide show
  1. data/VERSION.yml +1 -1
  2. data/lib/handsoap/service.rb +134 -94
  3. metadata +2 -2
data/VERSION.yml CHANGED
@@ -1,4 +1,4 @@
1
1
  ---
2
2
  :minor: 3
3
- :patch: 1
3
+ :patch: 2
4
4
  :major: 0
@@ -104,26 +104,9 @@ module Handsoap
104
104
  def self.request_content_type
105
105
  @protocol_version == 1 ? "text/xml" : "application/soap+xml"
106
106
  end
107
- def self.map_method(mapping)
108
- if @mapping.nil?
109
- @mapping = {}
110
- end
111
- @mapping.merge! mapping
112
- end
113
- def self.on_create_document(&block)
114
- @create_document_callback = block
115
- end
116
- def self.fire_on_create_document(doc)
117
- if @create_document_callback
118
- @create_document_callback.call doc
119
- end
120
- end
121
107
  def self.uri
122
108
  @uri
123
109
  end
124
- def self.get_mapping(name)
125
- @mapping[name] if @mapping
126
- end
127
110
  @@instance = {}
128
111
  def self.instance
129
112
  @@instance[self.to_s] ||= self.new
@@ -135,14 +118,6 @@ module Handsoap
135
118
  super
136
119
  end
137
120
  end
138
- def method_missing(method, *args)
139
- action = self.class.get_mapping(method)
140
- if action
141
- invoke(action, *args)
142
- else
143
- super
144
- end
145
- end
146
121
  # Creates an XML document and sends it over HTTP.
147
122
  #
148
123
  # +action+ is the QName of the rootnode of the envelope.
@@ -173,60 +148,38 @@ module Handsoap
173
148
  dispatch(doc, options[:soap_action])
174
149
  end
175
150
  end
151
+ # Hook that is called when a new request document is created.
152
+ #
153
+ # You can override this to add namespaces and other elements that are common to all requests (Such as authentication).
154
+ def on_create_document(doc)
155
+ end
176
156
  # Hook that is called before the message is dispatched.
177
157
  #
178
158
  # You can override this to provide filtering and logging.
179
159
  def on_before_dispatch
180
160
  end
181
- # Hook that is called if the dispatch returns a +Fault+.
161
+ # Hook that is called when there is a response.
182
162
  #
183
- # Default behaviour is to raise the Fault, but you can override this to provide logging and more fine-grained handling faults.
184
- def on_fault(fault)
185
- raise fault
163
+ # You can override this to register common namespaces, useful for parsing the document.
164
+ def on_response_document(doc)
186
165
  end
187
- private
188
- # Helper to serialize a node into a ruby string
166
+ # Hook that is called if there is a HTTP level error.
189
167
  #
190
- # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_s
191
- def xml_to_str(node, xquery = nil)
192
- n = xquery ? node.xpath(xquery, ns).first : node
193
- return if n.nil?
194
- n.to_utf8
168
+ # Default behaviour is to raise an error.
169
+ def on_http_error(status, content)
170
+ raise "HTTP error #{status}"
195
171
  end
196
- alias_method :xml_to_s, :xml_to_str
197
- # Helper to serialize a node into a ruby integer
198
- #
199
- # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_i
200
- def xml_to_int(node, xquery = nil)
201
- n = xquery ? node.xpath(xquery, ns).first : node
202
- return if n.nil?
203
- n.to_s.to_i
204
- end
205
- alias_method :xml_to_i, :xml_to_int
206
- # Helper to serialize a node into a ruby float
207
- #
208
- # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_f
209
- def xml_to_float(node, xquery = nil)
210
- n = xquery ? node.xpath(xquery, ns).first : node
211
- return if n.nil?
212
- n.to_s.to_f
213
- end
214
- alias_method :xml_to_f, :xml_to_float
215
- # Helper to serialize a node into a ruby boolean
172
+ # Hook that is called if the dispatch returns a +Fault+.
216
173
  #
217
- # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_boolean
218
- def xml_to_bool(node, xquery = nil)
219
- n = xquery ? node.xpath(xquery, ns).first : node
220
- return if n.nil?
221
- n.to_s == "true"
174
+ # Default behaviour is to raise the Fault, but you can override this to provide logging and more fine-grained handling faults.
175
+ def on_fault(fault)
176
+ raise fault
222
177
  end
223
- # Helper to serialize a node into a ruby Time object
178
+ # Hook that is called if the response does not contain a valid SOAP enevlope.
224
179
  #
225
- # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_date
226
- def xml_to_date(node, xquery = nil)
227
- n = xquery ? node.xpath(xquery, ns).first : node
228
- return if n.nil?
229
- Time.iso8601(n.to_s)
180
+ # Default behaviour is to raise an error
181
+ def on_missing_document(soap_response)
182
+ raise "The response is not a valid SOAP envelope"
230
183
  end
231
184
  def debug(message = nil) #:nodoc:
232
185
  if @@logger
@@ -238,7 +191,21 @@ module Handsoap
238
191
  end
239
192
  end
240
193
  end
241
- # Takes care of the HTTP level dispatch.
194
+ # Does the actual HTTP level interaction.
195
+ def send_http_request(uri, post_body, headers)
196
+ if Handsoap.http_driver == :curb
197
+ http_client = Curl::Easy.new(uri)
198
+ http_client.headers = headers
199
+ http_client.http_post post_body
200
+ return { :status => http_client.response_code, :body => http_client.body_str, :content_type => response.contenttype }
201
+ elsif Handsoap.http_driver == :httpclient
202
+ response = HTTPClient.new.post(uri, post_body, headers)
203
+ return { :status => response.status, :body => response.content, :content_type => response.content_type }
204
+ else
205
+ raise "Unknown http driver #{Handsoap.http_driver}"
206
+ end
207
+ end
208
+ # Send document and parses the response into a +Response+
242
209
  def dispatch(doc, action)
243
210
  on_before_dispatch
244
211
  headers = {
@@ -254,34 +221,25 @@ module Handsoap
254
221
  logger.puts "---"
255
222
  logger.puts body
256
223
  end
257
- if Handsoap.http_driver == :curb
258
- http_client = Curl::Easy.new(self.class.uri)
259
- http_client.headers = headers
260
- http_client.http_post body
261
- debug do |logger|
262
- logger.puts "--- Response ---"
263
- logger.puts "HTTP Status: %s" % [http_client.response_code]
264
- logger.puts "Content-Type: %s" % [http_client.content_type]
265
- logger.puts "---"
266
- logger.puts Handsoap.pretty_format_envelope(http_client.body_str)
267
- end
268
- soap_response = Response.new(http_client.body_str, self.class.envelope_namespace)
269
- elsif Handsoap.http_driver == :httpclient
270
- response = HTTPClient.new.post(self.class.uri, body, headers)
271
- debug do |logger|
272
- logger.puts "--- Response ---"
273
- logger.puts "HTTP Status: %s" % [response.status]
274
- logger.puts "Content-Type: %s" % [response.contenttype]
275
- logger.puts "---"
276
- logger.puts Handsoap.pretty_format_envelope(response.content)
277
- end
278
- soap_response = Response.new(response.content, self.class.envelope_namespace)
279
- else
280
- raise "Unknown http driver #{Handsoap.http_driver}"
224
+ response = send_http_request(self.class.uri, body, headers)
225
+ debug do |logger|
226
+ logger.puts "--- Response ---"
227
+ logger.puts "HTTP Status: %s" % [response[:status]]
228
+ logger.puts "Content-Type: %s" % [response[:content_type]]
229
+ logger.puts "---"
230
+ logger.puts Handsoap.pretty_format_envelope(response[:body])
231
+ end
232
+ if response[:status] >= 300
233
+ return on_http_error(response[:status], response[:body])
281
234
  end
235
+ soap_response = Response.new(response[:body], self.class.envelope_namespace)
282
236
  if soap_response.fault?
283
- return self.on_fault(soap_response.fault)
237
+ return on_fault(soap_response.fault)
284
238
  end
239
+ unless soap_response.document?
240
+ return on_missing_document(soap_response)
241
+ end
242
+ on_response_document(soap_response.document)
285
243
  return soap_response
286
244
  end
287
245
  # Creates a standard SOAP envelope and yields the +Body+ element.
@@ -293,7 +251,8 @@ module Handsoap
293
251
  env.add "*:Body"
294
252
  end
295
253
  end
296
- self.class.fire_on_create_document doc
254
+ self.class.fire_on_create_document doc # deprecated .. use instance method
255
+ on_create_document(doc)
297
256
  if block_given?
298
257
  yield doc.find("Body")
299
258
  end
@@ -315,3 +274,84 @@ module Handsoap
315
274
  end
316
275
 
317
276
  end
277
+
278
+ # Legacy/CS code here. This shouldn't be used in new applications.
279
+ module Handsoap
280
+ class Service
281
+ # Registers a simple method mapping without any arguments and no parsing of response.
282
+ #
283
+ # This is deprecated
284
+ def self.map_method(mapping)
285
+ if @mapping.nil?
286
+ @mapping = {}
287
+ end
288
+ @mapping.merge! mapping
289
+ end
290
+ def self.get_mapping(name)
291
+ @mapping[name] if @mapping
292
+ end
293
+ def method_missing(method, *args)
294
+ action = self.class.get_mapping(method)
295
+ if action
296
+ invoke(action, *args)
297
+ else
298
+ super
299
+ end
300
+ end
301
+ # Registers a block to call when a request document is created.
302
+ #
303
+ # This is deprecated, in favour of #on_create_document
304
+ def self.on_create_document(&block)
305
+ @create_document_callback = block
306
+ end
307
+ def self.fire_on_create_document(doc)
308
+ if @create_document_callback
309
+ @create_document_callback.call doc
310
+ end
311
+ end
312
+ private
313
+ # Helper to serialize a node into a ruby string
314
+ #
315
+ # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_s
316
+ def xml_to_str(node, xquery = nil)
317
+ n = xquery ? node.xpath(xquery, ns).first : node
318
+ return if n.nil?
319
+ n.to_s
320
+ end
321
+ alias_method :xml_to_s, :xml_to_str
322
+ # Helper to serialize a node into a ruby integer
323
+ #
324
+ # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_i
325
+ def xml_to_int(node, xquery = nil)
326
+ n = xquery ? node.xpath(xquery, ns).first : node
327
+ return if n.nil?
328
+ n.to_s.to_i
329
+ end
330
+ alias_method :xml_to_i, :xml_to_int
331
+ # Helper to serialize a node into a ruby float
332
+ #
333
+ # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_f
334
+ def xml_to_float(node, xquery = nil)
335
+ n = xquery ? node.xpath(xquery, ns).first : node
336
+ return if n.nil?
337
+ n.to_s.to_f
338
+ end
339
+ alias_method :xml_to_f, :xml_to_float
340
+ # Helper to serialize a node into a ruby boolean
341
+ #
342
+ # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_boolean
343
+ def xml_to_bool(node, xquery = nil)
344
+ n = xquery ? node.xpath(xquery, ns).first : node
345
+ return if n.nil?
346
+ n.to_s == "true"
347
+ end
348
+ # Helper to serialize a node into a ruby Time object
349
+ #
350
+ # *deprecated*. Use Handsoap::XmlQueryFront::BaseDriver#to_date
351
+ def xml_to_date(node, xquery = nil)
352
+ n = xquery ? node.xpath(xquery, ns).first : node
353
+ return if n.nil?
354
+ Time.iso8601(n.to_s)
355
+ end
356
+ end
357
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: troelskn-handsoap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Troels Knak-Nielsen
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-06-23 00:00:00 -07:00
12
+ date: 2009-06-24 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15