aforward-actionwebservice 2.3.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (94) hide show
  1. data/.gitignore +3 -0
  2. data/Gemfile +5 -0
  3. data/History.txt +325 -0
  4. data/Manifest.txt +12 -0
  5. data/PostInstall.txt +2 -0
  6. data/README.rdoc +410 -0
  7. data/Rakefile +2 -0
  8. data/actionwebservice.gemspec +19 -0
  9. data/autotest/discover.rb +2 -0
  10. data/examples/googlesearch/README +143 -0
  11. data/examples/googlesearch/autoloading/google_search_api.rb +50 -0
  12. data/examples/googlesearch/autoloading/google_search_controller.rb +57 -0
  13. data/examples/googlesearch/delegated/google_search_service.rb +108 -0
  14. data/examples/googlesearch/delegated/search_controller.rb +7 -0
  15. data/examples/googlesearch/direct/google_search_api.rb +50 -0
  16. data/examples/googlesearch/direct/search_controller.rb +58 -0
  17. data/examples/metaWeblog/README +17 -0
  18. data/examples/metaWeblog/apis/blogger_api.rb +60 -0
  19. data/examples/metaWeblog/apis/blogger_service.rb +34 -0
  20. data/examples/metaWeblog/apis/meta_weblog_api.rb +67 -0
  21. data/examples/metaWeblog/apis/meta_weblog_service.rb +48 -0
  22. data/examples/metaWeblog/controllers/xmlrpc_controller.rb +16 -0
  23. data/generators/web_service/USAGE +28 -0
  24. data/generators/web_service/templates/api_definition.rb +5 -0
  25. data/generators/web_service/templates/controller.rb +8 -0
  26. data/generators/web_service/templates/functional_test.rb +19 -0
  27. data/generators/web_service/web_service_generator.rb +29 -0
  28. data/lib/action_web_service/api.rb +297 -0
  29. data/lib/action_web_service/base.rb +38 -0
  30. data/lib/action_web_service/casting.rb +149 -0
  31. data/lib/action_web_service/client/base.rb +28 -0
  32. data/lib/action_web_service/client/soap_client.rb +113 -0
  33. data/lib/action_web_service/client/xmlrpc_client.rb +58 -0
  34. data/lib/action_web_service/client.rb +3 -0
  35. data/lib/action_web_service/container/action_controller_container.rb +93 -0
  36. data/lib/action_web_service/container/delegated_container.rb +86 -0
  37. data/lib/action_web_service/container/direct_container.rb +69 -0
  38. data/lib/action_web_service/container.rb +3 -0
  39. data/lib/action_web_service/dispatcher/abstract.rb +207 -0
  40. data/lib/action_web_service/dispatcher/action_controller_dispatcher.rb +379 -0
  41. data/lib/action_web_service/dispatcher.rb +2 -0
  42. data/lib/action_web_service/invocation.rb +202 -0
  43. data/lib/action_web_service/protocol/abstract.rb +112 -0
  44. data/lib/action_web_service/protocol/discovery.rb +37 -0
  45. data/lib/action_web_service/protocol/soap_protocol/marshaler.rb +242 -0
  46. data/lib/action_web_service/protocol/soap_protocol.rb +176 -0
  47. data/lib/action_web_service/protocol/xmlrpc_protocol.rb +122 -0
  48. data/lib/action_web_service/protocol.rb +4 -0
  49. data/lib/action_web_service/scaffolding.rb +281 -0
  50. data/lib/action_web_service/struct.rb +64 -0
  51. data/lib/action_web_service/support/class_inheritable_options.rb +26 -0
  52. data/lib/action_web_service/support/signature_types.rb +226 -0
  53. data/lib/action_web_service/templates/scaffolds/layout.html.erb +65 -0
  54. data/lib/action_web_service/templates/scaffolds/methods.html.erb +6 -0
  55. data/lib/action_web_service/templates/scaffolds/parameters.html.erb +29 -0
  56. data/lib/action_web_service/templates/scaffolds/result.html.erb +30 -0
  57. data/lib/action_web_service/test_invoke.rb +110 -0
  58. data/lib/action_web_service/version.rb +10 -0
  59. data/lib/action_web_service.rb +50 -0
  60. data/lib/actionwebservice.rb +1 -0
  61. data/old/Rakefile +173 -0
  62. data/old/Rakefile2 +25 -0
  63. data/old/TODO +32 -0
  64. data/old/actionwebservice.gemspec +37 -0
  65. data/old/install.rb +30 -0
  66. data/old/setup.rb +1379 -0
  67. data/script/console +10 -0
  68. data/script/destroy +14 -0
  69. data/script/generate +14 -0
  70. data/test/abstract_client.rb +183 -0
  71. data/test/abstract_dispatcher.rb +548 -0
  72. data/test/abstract_unit.rb +39 -0
  73. data/test/api_test.rb +102 -0
  74. data/test/apis/auto_load_api.rb +3 -0
  75. data/test/apis/broken_auto_load_api.rb +2 -0
  76. data/test/base_test.rb +42 -0
  77. data/test/casting_test.rb +94 -0
  78. data/test/client_soap_test.rb +155 -0
  79. data/test/client_xmlrpc_test.rb +153 -0
  80. data/test/container_test.rb +73 -0
  81. data/test/dispatcher_action_controller_soap_test.rb +138 -0
  82. data/test/dispatcher_action_controller_xmlrpc_test.rb +59 -0
  83. data/test/fixtures/db_definitions/mysql.sql +8 -0
  84. data/test/fixtures/users.yml +12 -0
  85. data/test/gencov +3 -0
  86. data/test/invocation_test.rb +185 -0
  87. data/test/run +6 -0
  88. data/test/scaffolded_controller_test.rb +146 -0
  89. data/test/struct_test.rb +52 -0
  90. data/test/test_action_web_service.rb +11 -0
  91. data/test/test_actionwebservice.rb +11 -0
  92. data/test/test_helper.rb +3 -0
  93. data/test/test_invoke_test.rb +112 -0
  94. metadata +184 -0
data/README.rdoc ADDED
@@ -0,0 +1,410 @@
1
+ = actionwebservice
2
+
3
+ * https://github.com/aforward/actionwebservice
4
+
5
+ = FORKED BASED ON ISSUES WITH RAILES 2.3.8
6
+
7
+ Our project team was moving from managing gems in the environment.rb file to bundler, and an incompatibility cropped up.
8
+
9
+ The old gem was included via (environment.rb):
10
+ config.gem 'datanoise-actionwebservice', :lib => 'actionwebservice'
11
+
12
+ The new gem was included via (Gemfile):
13
+ gem 'datanoise-actionwebservice'
14
+
15
+ But the following error arose:
16
+
17
+ Bundler could not find compatible versions for gem "actionpack":
18
+ In Gemfile:
19
+ datanoise-actionwebservice depends on
20
+ actionpack (= 2.1.0, )
21
+
22
+ rails (= 2.3.8) depends on
23
+ actionpack (2.3.8)
24
+
25
+
26
+ == DESCRIPTION:
27
+
28
+ Action Web Service -- Serving APIs on rails
29
+
30
+ Action Web Service provides a way to publish interoperable web service APIs with
31
+ Rails without spending a lot of time delving into protocol details.
32
+
33
+
34
+ == FEATURES/PROBLEMS:
35
+
36
+ * SOAP RPC protocol support
37
+ * Dynamic WSDL generation for APIs
38
+ * XML-RPC protocol support
39
+ * Clients that use the same API definitions as the server for
40
+ easy interoperability with other Action Web Service based applications
41
+ * Type signature hints to improve interoperability with static languages
42
+ * Active Record model class support in signatures
43
+
44
+ You specify the methods you want to make available as API methods in an
45
+ ActionWebService::API::Base derivative, and then specify this API
46
+ definition class wherever you want to use that API.
47
+
48
+ The implementation of the methods is done separately from the API
49
+ specification.
50
+
51
+ Action Web Service will camelcase the method names according to Rails Inflector
52
+ rules for the API visible to public callers. What this means, for example,
53
+ is that the method names in generated WSDL will be camelcased, and callers will
54
+ have to supply the camelcased name in their requests for the request to
55
+ succeed.
56
+
57
+ If you do not desire this behaviour, you can turn it off with the
58
+ ActionWebService::API::Base +inflect_names+ option.
59
+
60
+ == SYNOPSIS:
61
+
62
+ ==== Inflection examples
63
+
64
+ :add => Add
65
+ :find_all => FindAll
66
+
67
+
68
+ ==== Disabling inflection
69
+
70
+ class PersonAPI < ActionWebService::API::Base
71
+ inflect_names false
72
+ end
73
+
74
+
75
+ ==== API definition example
76
+
77
+ class PersonAPI < ActionWebService::API::Base
78
+ api_method :add, :expects => [:string, :string, :bool], :returns => [:int]
79
+ api_method :remove, :expects => [:int], :returns => [:bool]
80
+ end
81
+
82
+ ==== API usage example
83
+
84
+ class PersonController < ActionController::Base
85
+ web_service_api PersonAPI
86
+
87
+ def add
88
+ end
89
+
90
+ def remove
91
+ end
92
+ end
93
+
94
+
95
+ == Publishing your APIs
96
+
97
+ Action Web Service uses Action Pack to process protocol requests. There are two
98
+ modes of dispatching protocol requests, _Direct_, and _Delegated_.
99
+
100
+
101
+ === Direct dispatching
102
+
103
+ This is the default mode. In this mode, public controller instance methods
104
+ implement the API methods, and parameters are passed through to the methods in
105
+ accordance with the API specification.
106
+
107
+ The return value of the method is sent back as the return value to the
108
+ caller.
109
+
110
+ In this mode, a special <tt>api</tt> action is generated in the target
111
+ controller to unwrap the protocol request, forward it on to the relevant method
112
+ and send back the wrapped return value. <em>This action must not be
113
+ overridden.</em>
114
+
115
+ ==== Direct dispatching example
116
+
117
+ class PersonController < ApplicationController
118
+ web_service_api PersonAPI
119
+
120
+ def add
121
+ end
122
+
123
+ def remove
124
+ end
125
+ end
126
+
127
+ class PersonAPI < ActionWebService::API::Base
128
+ ...
129
+ end
130
+
131
+
132
+ For this example, protocol requests for +Add+ and +Remove+ methods sent to
133
+ <tt>/person/api</tt> will be routed to the controller methods +add+ and +remove+.
134
+
135
+
136
+ === Delegated dispatching
137
+
138
+ This mode can be turned on by setting the +web_service_dispatching_mode+ option
139
+ in a controller to <tt>:delegated</tt>.
140
+
141
+ In this mode, the controller contains one or more web service objects (objects
142
+ that implement an ActionWebService::API::Base definition). These web service
143
+ objects are each mapped onto one controller action only.
144
+
145
+ ==== Delegated dispatching example
146
+
147
+ class ApiController < ApplicationController
148
+ web_service_dispatching_mode :delegated
149
+
150
+ web_service :person, PersonService.new
151
+ end
152
+
153
+ class PersonService < ActionWebService::Base
154
+ web_service_api PersonAPI
155
+
156
+ def add
157
+ end
158
+
159
+ def remove
160
+ end
161
+ end
162
+
163
+ class PersonAPI < ActionWebService::API::Base
164
+ ...
165
+ end
166
+
167
+
168
+ For this example, all protocol requests for +PersonService+ are
169
+ sent to the <tt>/api/person</tt> action.
170
+
171
+ The <tt>/api/person</tt> action is generated when the +web_service+
172
+ method is called. <em>This action must not be overridden.</em>
173
+
174
+ Other controller actions (actions that aren't the target of a +web_service+ call)
175
+ are ignored for ActionWebService purposes, and can do normal action tasks.
176
+
177
+
178
+ === Layered dispatching
179
+
180
+ This mode can be turned on by setting the +web_service_dispatching_mode+ option
181
+ in a controller to <tt>:layered</tt>.
182
+
183
+ This mode is similar to _delegated_ mode, in that multiple web service objects
184
+ can be attached to one controller, however, all protocol requests are sent to a
185
+ single endpoint.
186
+
187
+ Use this mode when you want to share code between XML-RPC and SOAP clients,
188
+ for APIs where the XML-RPC method names have prefixes. An example of such
189
+ a method name would be <tt>blogger.newPost</tt>.
190
+
191
+
192
+ ==== Layered dispatching example
193
+
194
+
195
+ class ApiController < ApplicationController
196
+ web_service_dispatching_mode :layered
197
+
198
+ web_service :mt, MovableTypeService.new
199
+ web_service :blogger, BloggerService.new
200
+ web_service :metaWeblog, MetaWeblogService.new
201
+ end
202
+
203
+ class MovableTypeService < ActionWebService::Base
204
+ ...
205
+ end
206
+
207
+ class BloggerService < ActionWebService::Base
208
+ ...
209
+ end
210
+
211
+ class MetaWeblogService < ActionWebService::API::Base
212
+ ...
213
+ end
214
+
215
+
216
+ For this example, an XML-RPC call for a method with a name like
217
+ <tt>mt.getCategories</tt> will be sent to the <tt>getCategories</tt>
218
+ method on the <tt>:mt</tt> service.
219
+
220
+
221
+ == Customizing WSDL generation
222
+
223
+ You can customize the names used for the SOAP bindings in the generated
224
+ WSDL by using the wsdl_service_name option in a controller:
225
+
226
+ class WsController < ApplicationController
227
+ wsdl_service_name 'MyApp'
228
+ end
229
+
230
+ You can also customize the namespace used in the generated WSDL for
231
+ custom types and message definition types:
232
+
233
+ class WsController < ApplicationController
234
+ wsdl_namespace 'http://my.company.com/app/wsapi'
235
+ end
236
+
237
+ The default namespace used is 'urn:ActionWebService', if you don't supply
238
+ one.
239
+
240
+
241
+ == ActionWebService and UTF-8
242
+
243
+ If you're going to be sending back strings containing non-ASCII UTF-8
244
+ characters using the <tt>:string</tt> data type, you need to make sure that
245
+ Ruby is using UTF-8 as the default encoding for its strings.
246
+
247
+ The default in Ruby is to use US-ASCII encoding for strings, which causes a string
248
+ validation check in the Ruby SOAP library to fail and your string to be sent
249
+ back as a Base-64 value, which may confuse clients that expected strings
250
+ because of the WSDL.
251
+
252
+ Two ways of setting the default string encoding are:
253
+
254
+ * Start Ruby using the <tt>-Ku</tt> command-line option to the Ruby executable
255
+ * Set the <tt>$KCODE</tt> flag in <tt>config/environment.rb</tt> to the
256
+ string <tt>'UTF8'</tt>
257
+
258
+
259
+ == Testing your APIs
260
+
261
+
262
+ === Functional testing
263
+
264
+ You can perform testing of your APIs by creating a functional test for the
265
+ controller dispatching the API, and calling #invoke in the test case to
266
+ perform the invocation.
267
+
268
+ Example:
269
+
270
+ class PersonApiControllerTest < Test::Unit::TestCase
271
+ def setup
272
+ @controller = PersonController.new
273
+ @request = ActionController::TestRequest.new
274
+ @response = ActionController::TestResponse.new
275
+ end
276
+
277
+ def test_add
278
+ result = invoke :remove, 1
279
+ assert_equal true, result
280
+ end
281
+ end
282
+
283
+ This example invokes the API method <tt>test</tt>, defined on
284
+ the PersonController, and returns the result.
285
+
286
+ If you're not using SOAP (or you're having serialisation difficulties),
287
+ you can test XMLRPC like this:
288
+
289
+ class PersonApiControllerTest < Test::Unit::TestCase
290
+ def setup
291
+ @controller = PersonController.new
292
+ @request = ActionController::TestRequest.new
293
+ @response = ActionController::TestResponse.new
294
+
295
+ @protocol = :xmlrpc # can also be :soap, the default
296
+ end
297
+
298
+ def test_add
299
+ result = invoke :remove, 1 # no change here
300
+ assert_equal true, result
301
+ end
302
+ end
303
+
304
+ === Scaffolding
305
+
306
+ You can also test your APIs with a web browser by attaching scaffolding
307
+ to the controller.
308
+
309
+ Example:
310
+
311
+ class PersonController
312
+ web_service_scaffold :invocation
313
+ end
314
+
315
+ This creates an action named <tt>invocation</tt> on the PersonController.
316
+
317
+ Navigating to this action lets you select the method to invoke, supply the parameters,
318
+ and view the result of the invocation.
319
+
320
+
321
+ == Using the client support
322
+
323
+ Action Web Service includes client classes that can use the same API
324
+ definition as the server. The advantage of this approach is that your client
325
+ will have the same support for Active Record and structured types as the
326
+ server, and can just use them directly, and rely on the marshaling to Do The
327
+ Right Thing.
328
+
329
+ *Note*: The client support is intended for communication between Ruby on Rails
330
+ applications that both use Action Web Service. It may work with other servers, but
331
+ that is not its intended use, and interoperability can't be guaranteed, especially
332
+ not for .NET web services.
333
+
334
+ Web services protocol specifications are complex, and Action Web Service client
335
+ support can only be guaranteed to work with a subset.
336
+
337
+
338
+ ==== Factory created client example
339
+
340
+ class BlogManagerController < ApplicationController
341
+ web_client_api :blogger, :xmlrpc, 'http://url/to/blog/api/RPC2', :handler_name => 'blogger'
342
+ end
343
+
344
+ class SearchingController < ApplicationController
345
+ web_client_api :google, :soap, 'http://url/to/blog/api/beta', :service_name => 'GoogleSearch'
346
+ end
347
+
348
+ See ActionWebService::API::ActionController::ClassMethods for more details.
349
+
350
+ ==== Manually created client example
351
+
352
+ class PersonAPI < ActionWebService::API::Base
353
+ api_method :find_all, :returns => [[Person]]
354
+ end
355
+
356
+ soap_client = ActionWebService::Client::Soap.new(PersonAPI, "http://...")
357
+ persons = soap_client.find_all
358
+
359
+ class BloggerAPI < ActionWebService::API::Base
360
+ inflect_names false
361
+ api_method :getRecentPosts, :returns => [[Blog::Post]]
362
+ end
363
+
364
+ blog = ActionWebService::Client::XmlRpc.new(BloggerAPI, "http://.../xmlrpc", :handler_name => "blogger")
365
+ posts = blog.getRecentPosts
366
+
367
+
368
+ See ActionWebService::Client::Soap and ActionWebService::Client::XmlRpc for more details.
369
+
370
+ == Dependencies
371
+
372
+ Action Web Service requires that the Action Pack and Active Record are either
373
+ available to be required immediately or are accessible as GEMs.
374
+
375
+ It also requires a version of Ruby that includes SOAP support in the standard
376
+ library. At least version 1.8.2 final (2004-12-25) of Ruby is recommended; this
377
+ is the version tested against.
378
+
379
+ == REQUIREMENTS:
380
+
381
+ * FIX (list of requirements)
382
+
383
+ == INSTALL:
384
+
385
+ gem install actionwebservice
386
+
387
+ == LICENSE:
388
+
389
+ (The MIT License)
390
+
391
+ Copyright (c) 2010 Andrew Forward
392
+
393
+ Permission is hereby granted, free of charge, to any person obtaining
394
+ a copy of this software and associated documentation files (the
395
+ 'Software'), to deal in the Software without restriction, including
396
+ without limitation the rights to use, copy, modify, merge, publish,
397
+ distribute, sublicense, and/or sell copies of the Software, and to
398
+ permit persons to whom the Software is furnished to do so, subject to
399
+ the following conditions:
400
+
401
+ The above copyright notice and this permission notice shall be
402
+ included in all copies or substantial portions of the Software.
403
+
404
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
405
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
406
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
407
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
408
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
409
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
410
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "action_web_service/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "aforward-actionwebservice"
7
+ s.version = ActionWebService::VERSION::STRING
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Andrew Forward (forked from Leon Breedt, Kent Sibilev)"]
10
+ s.email = ["andrew.forward@cenx.com"]
11
+ s.homepage = "http://github.com/aforward/actionwebservice"
12
+ s.summary = %q{Porting actionwebservice to work with Rails 2.3.8}
13
+ s.description = %q{Porting actionwebservice to work with Rails 2.3.8}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,2 @@
1
+ #Autotest.add_discovery { "rails" }
2
+ Autotest.add_discovery { "rspec2" }
@@ -0,0 +1,143 @@
1
+ = Google Service example
2
+
3
+ This example shows how one would implement an API like Google
4
+ Search that uses lots of structured types.
5
+
6
+ There are examples for "Direct" and "Delegated" dispatching
7
+ modes.
8
+
9
+ There is also an example for API definition file autoloading.
10
+
11
+
12
+ = Running the examples
13
+
14
+ 1. Add the files to an Action Web Service enabled Rails project.
15
+
16
+ "Direct" example:
17
+
18
+ * Copy direct/search_controller.rb to "app/controllers"
19
+ in a Rails project.
20
+ * Copy direct/google_search_api.rb to "app/apis"
21
+ in a Rails project
22
+
23
+ "Delegated" example:
24
+
25
+ * Copy delegated/search_controller.rb to "app/controllers"
26
+ in a Rails project.
27
+ * Copy delegated/google_search_service.rb to "lib"
28
+ in a Rails project.
29
+
30
+ "Autoloading" example:
31
+
32
+ * Copy autoloading/google_search_api.rb to "app/apis" (create the directory
33
+ if it doesn't exist) in a Rails project.
34
+
35
+ * Copy autoloading/google_search_controller.rb "app/controllers"
36
+ in a Rails project.
37
+
38
+
39
+ 2. Go to the WSDL url in a browser, and check that it looks correct.
40
+
41
+ "Direct" and "Delegated" examples:
42
+ http://url_to_project/search/wsdl
43
+
44
+ "Autoloading" example:
45
+ http://url_to_project/google_search/wsdl
46
+
47
+ You can compare it to Google's hand-coded WSDL at http://api.google.com/GoogleSearch.wsdl
48
+ and see how close (or not) the generated version is.
49
+
50
+ Note that I used GoogleSearch as the canonical "best practice"
51
+ interoperable example when implementing WSDL/SOAP support, which might
52
+ explain extreme similarities :)
53
+
54
+
55
+ 3. Test that it works with .NET (Mono in this example):
56
+
57
+ $ wget WSDL_URL
58
+ $ mv wsdl GoogleSearch.wsdl
59
+ $ wsdl -out:GoogleSearch.cs GoogleSearch.wsdl
60
+
61
+ Add these lines to the GoogleSearchService class body (be mindful of the
62
+ wrapping):
63
+
64
+ public static void Main(string[] args)
65
+ {
66
+ GoogleSearchResult result;
67
+ GoogleSearchService service;
68
+
69
+ service = new GoogleSearchService();
70
+ result = service.doGoogleSearch("myApiKey", "my query", 10, 30, true, "restrict", false, "lr", "ie", "oe");
71
+ System.Console.WriteLine("documentFiltering: {0}", result.documentFiltering);
72
+ System.Console.WriteLine("searchComments: {0}", result.searchComments);
73
+ System.Console.WriteLine("estimatedTotalResultsCount: {0}", result.estimatedTotalResultsCount);
74
+ System.Console.WriteLine("estimateIsExact: {0}", result.estimateIsExact);
75
+ System.Console.WriteLine("resultElements:");
76
+ foreach (ResultElement element in result.resultElements) {
77
+ System.Console.WriteLine("\tsummary: {0}", element.summary);
78
+ System.Console.WriteLine("\tURL: {0}", element.URL);
79
+ System.Console.WriteLine("\tsnippet: {0}", element.snippet);
80
+ System.Console.WriteLine("\ttitle: {0}", element.title);
81
+ System.Console.WriteLine("\tcachedSize: {0}", element.cachedSize);
82
+ System.Console.WriteLine("\trelatedInformationPresent: {0}", element.relatedInformationPresent);
83
+ System.Console.WriteLine("\thostName: {0}", element.hostName);
84
+ System.Console.WriteLine("\tdirectoryCategory: {0}", element.directoryCategory.fullViewableName);
85
+ System.Console.WriteLine("\tdirectoryTitle: {0}", element.directoryTitle);
86
+ }
87
+ System.Console.WriteLine("searchQuery: {0}", result.searchQuery);
88
+ System.Console.WriteLine("startIndex: {0}", result.startIndex);
89
+ System.Console.WriteLine("endIndex: {0}", result.endIndex);
90
+ System.Console.WriteLine("searchTips: {0}", result.searchTips);
91
+ System.Console.WriteLine("directoryCategories:");
92
+ foreach (DirectoryCategory cat in result.directoryCategories) {
93
+ System.Console.WriteLine("\t{0} ({1})", cat.fullViewableName, cat.specialEncoding);
94
+ }
95
+ System.Console.WriteLine("searchTime: {0}", result.searchTime);
96
+ }
97
+
98
+ Now compile and run:
99
+
100
+ $ mcs -reference:System.Web.Services GoogleSearch.cs
101
+ $ mono GoogleSearch.exe
102
+
103
+
104
+ If you had the application running (on the same host you got
105
+ the WSDL from), you should see something like this:
106
+
107
+
108
+ documentFiltering: True
109
+ searchComments:
110
+ estimatedTotalResultsCount: 322000
111
+ estimateIsExact: False
112
+ resultElements:
113
+ summary: ONlamp.com: Rolling with Ruby on Rails
114
+ URL: http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html
115
+ snippet: Curt Hibbs shows off Ruby on Rails by building a simple ...
116
+ title: Teh Railz0r
117
+ cachedSize: Almost no lines of code!
118
+ relatedInformationPresent: True
119
+ hostName: rubyonrails.com
120
+ directoryCategory: Web Development
121
+ directoryTitle:
122
+ searchQuery: http://www.google.com/search?q=ruby+on+rails
123
+ startIndex: 10
124
+ endIndex: 40
125
+ searchTips: "on" is a very common word and was not included in your search [details]
126
+ directoryCategories:
127
+ Web Development (UTF-8)
128
+ Programming (US-ASCII)
129
+ searchTime: 1E-06
130
+
131
+
132
+ Also, if an API method throws an exception, it will be sent back to the
133
+ caller in the protocol's exception format, so they should get an exception
134
+ thrown on their side with a meaningful error message.
135
+
136
+ If you don't like this behaviour, you can do:
137
+
138
+ class MyController < ActionController::Base
139
+ web_service_exception_reporting false
140
+ end
141
+
142
+ 4. Crack open a beer. Publishing APIs for working with the same model as
143
+ your Rails web app should be easy from now on :)
@@ -0,0 +1,50 @@
1
+ class DirectoryCategory < ActionWebService::Struct
2
+ member :fullViewableName, :string
3
+ member :specialEncoding, :string
4
+ end
5
+
6
+ class ResultElement < ActionWebService::Struct
7
+ member :summary, :string
8
+ member :URL, :string
9
+ member :snippet, :string
10
+ member :title, :string
11
+ member :cachedSize, :string
12
+ member :relatedInformationPresent, :bool
13
+ member :hostName, :string
14
+ member :directoryCategory, DirectoryCategory
15
+ member :directoryTitle, :string
16
+ end
17
+
18
+ class GoogleSearchResult < ActionWebService::Struct
19
+ member :documentFiltering, :bool
20
+ member :searchComments, :string
21
+ member :estimatedTotalResultsCount, :int
22
+ member :estimateIsExact, :bool
23
+ member :resultElements, [ResultElement]
24
+ member :searchQuery, :string
25
+ member :startIndex, :int
26
+ member :endIndex, :int
27
+ member :searchTips, :string
28
+ member :directoryCategories, [DirectoryCategory]
29
+ member :searchTime, :float
30
+ end
31
+
32
+ class GoogleSearchAPI < ActionWebService::API::Base
33
+ inflect_names false
34
+
35
+ api_method :doGetCachedPage, :returns => [:string], :expects => [{:key=>:string}, {:url=>:string}]
36
+ api_method :doGetSpellingSuggestion, :returns => [:string], :expects => [{:key=>:string}, {:phrase=>:string}]
37
+
38
+ api_method :doGoogleSearch, :returns => [GoogleSearchResult], :expects => [
39
+ {:key=>:string},
40
+ {:q=>:string},
41
+ {:start=>:int},
42
+ {:maxResults=>:int},
43
+ {:filter=>:bool},
44
+ {:restrict=>:string},
45
+ {:safeSearch=>:bool},
46
+ {:lr=>:string},
47
+ {:ie=>:string},
48
+ {:oe=>:string}
49
+ ]
50
+ end
@@ -0,0 +1,57 @@
1
+ class GoogleSearchController < ApplicationController
2
+ wsdl_service_name 'GoogleSearch'
3
+
4
+ def doGetCachedPage
5
+ "<html><body>i am a cached page. my key was %s, url was %s</body></html>" % [@params['key'], @params['url']]
6
+ end
7
+
8
+ def doSpellingSuggestion
9
+ "%s: Did you mean '%s'?" % [@params['key'], @params['phrase']]
10
+ end
11
+
12
+ def doGoogleSearch
13
+ resultElement = ResultElement.new
14
+ resultElement.summary = "ONlamp.com: Rolling with Ruby on Rails"
15
+ resultElement.URL = "http://www.onlamp.com/pub/a/onlamp/2005/01/20/rails.html"
16
+ resultElement.snippet = "Curt Hibbs shows off Ruby on Rails by building a simple application that requires " +
17
+ "almost no Ruby experience. ... Rolling with Ruby on Rails. ..."
18
+ resultElement.title = "Teh Railz0r"
19
+ resultElement.cachedSize = "Almost no lines of code!"
20
+ resultElement.relatedInformationPresent = true
21
+ resultElement.hostName = "rubyonrails.com"
22
+ resultElement.directoryCategory = category("Web Development", "UTF-8")
23
+
24
+ result = GoogleSearchResult.new
25
+ result.documentFiltering = @params['filter']
26
+ result.searchComments = ""
27
+ result.estimatedTotalResultsCount = 322000
28
+ result.estimateIsExact = false
29
+ result.resultElements = [resultElement]
30
+ result.searchQuery = "http://www.google.com/search?q=ruby+on+rails"
31
+ result.startIndex = @params['start']
32
+ result.endIndex = @params['start'] + @params['maxResults']
33
+ result.searchTips = "\"on\" is a very common word and was not included in your search [details]"
34
+ result.searchTime = 0.000001
35
+
36
+ # For Mono, we have to clone objects if they're referenced by more than one place, otherwise
37
+ # the Ruby SOAP collapses them into one instance and uses references all over the
38
+ # place, confusing Mono.
39
+ #
40
+ # This has recently been fixed:
41
+ # http://bugzilla.ximian.com/show_bug.cgi?id=72265
42
+ result.directoryCategories = [
43
+ category("Web Development", "UTF-8"),
44
+ category("Programming", "US-ASCII"),
45
+ ]
46
+
47
+ result
48
+ end
49
+
50
+ private
51
+ def category(name, encoding)
52
+ cat = DirectoryCategory.new
53
+ cat.fullViewableName = name.dup
54
+ cat.specialEncoding = encoding.dup
55
+ cat
56
+ end
57
+ end