Orbjson 0.0.3 → 0.0.4

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.
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * JSON-RPC JavaScript client
3
3
  *
4
- * $Id: jsonrpc.js,v 1.1.1.1 2005/02/28 03:06:28 jamesgbritt Exp $
4
+ * $Id: jsonrpc.js,v 1.3 2005/03/15 01:37:15 jamesgbritt Exp $
5
5
  *
6
6
  * Copyright (c) 2003-2004 Jan-Klaas Kollhof
7
7
  * Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd
@@ -0,0 +1,261 @@
1
+ /*
2
+ * JSON-RPC JavaScript client
3
+ *
4
+ * $Id: jsonrpc_async.js,v 1.4 2005/03/15 02:29:22 jamesgbritt Exp $
5
+ *
6
+ * Copyright (c) 2003-2004 Jan-Klaas Kollhof
7
+ * Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd
8
+ * Copyright (c) 2005 James Britt, Neurogami, LLC
9
+ *
10
+ * This code is based on Michael Clark's' version of Jan-Klaas' jsonrpc.js
11
+ * file fom the JavaScript o lait library (jsolait).
12
+
13
+ * The Clark version seemed easier to use the original Module-based
14
+ * code, but did not do asyncronous requests. The current version
15
+ * is essentialy the same code, but with the async requests and callback
16
+ * hooks
17
+ *
18
+ * This library is free software; you can redistribute it and/or
19
+ * modify it under the terms of the GNU Lesser General Public (LGPL)
20
+ * License as published by the Free Software Foundation; either
21
+ * version 2.1 of the License, or (at your option) any later version.
22
+ *
23
+ * This library is distributed in the hope that it will be useful,
24
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
26
+ * Lesser General Public License for more details: http://www.gnu.org/
27
+ *
28
+ */
29
+
30
+ // TODO: Add async
31
+ // TODO: Add HTTP auth (user, password)
32
+
33
+ Object.prototype.toJSON = function Object_toJSON() {
34
+ var v = [];
35
+ for(attr in this) {
36
+ if(this[attr] == null) v.push("\"" + attr + "\": null");
37
+ else if(typeof this[attr] == "function"); // skip
38
+ else v.push("\"" + attr + "\": " + this[attr].toJSON());
39
+ }
40
+ return "{" + v.join(", ") + "}";
41
+ }
42
+
43
+ String.prototype.toJSON = function String_toJSON() {
44
+ return "\"" + this.replace(/([\"\\])/g, "\\$1") + "\"";
45
+ }
46
+
47
+ Number.prototype.toJSON = function Number_toJSON() {
48
+ return this.toString();
49
+ }
50
+
51
+ Boolean.prototype.toJSON = function Boolean_toJSON() {
52
+ return this.toString();
53
+ }
54
+
55
+ Date.prototype.toJSON = function Date_toJSON() {
56
+ this.valueOf().toString();
57
+ }
58
+
59
+ Array.prototype.toJSON = function Array_toJSON() {
60
+ var v = [];
61
+ for(var i=0; i<this.length; i++) {
62
+ if(this[i] == null) v.push("null");
63
+ else v.push(this[i].toJSON());
64
+ }
65
+ return "[" + v.join(", ") + "]";
66
+ }
67
+
68
+
69
+ /*******************************************************************
70
+ ******************************************************************/
71
+ JSONRpcAsyncClient = function JSONRpcAsyncClient_ctor( serverURL, objectID ) {
72
+ this.serverURL = serverURL;
73
+ this.objectID = objectID;
74
+ if( !JSONRpcAsyncClient.http )
75
+ JSONRpcAsyncClient.http = JSONRpcAsyncClient.getHTTPRequest();
76
+ this.addAsyncMethods( ["system.listMethods"] );
77
+ var m = this.sendRequest( "system.listMethods", [] );
78
+ this.addAsyncMethods( m );
79
+
80
+ }
81
+
82
+ /*******************************************************************
83
+ ******************************************************************/
84
+ JSONRpcAsyncClient.Exception = function JSONRpcAsyncClient_Exception_ctor(code, msg) {
85
+ this.code = code;
86
+ this.msg = msg;
87
+ }
88
+
89
+ /*******************************************************************
90
+ ******************************************************************/
91
+ JSONRpcAsyncClient.Exception.prototype.toString =
92
+ function JSONRpcAsyncClient_Exception_toString(code, msg) {
93
+ return "JSONRpcAsyncClientException: " + this.msg;
94
+ }
95
+
96
+
97
+
98
+
99
+ JSONRpcAsyncClient.AsyncMethod =
100
+ function JSONRpcAsyncClient_Method_ctor( client, methodName ) {
101
+
102
+ var fn=function() {
103
+ var args = [];
104
+ var callback = arguments[0]
105
+ for(var i=1; i< arguments.length;i++) {
106
+ args.push(arguments[i]);
107
+ }
108
+ return fn.client.sendAsyncRequest.call( fn.client, fn.methodName, callback, args);
109
+ }
110
+
111
+ fn.client = client;
112
+ fn.methodName = methodName;
113
+ return fn;
114
+ }
115
+
116
+ JSONRpcAsyncClient.prototype.addAsyncMethods =
117
+ function JSONRpcAsyncClient_addAsyncMethods(methodNames) {
118
+ for(var i=0; i<methodNames.length; i++) {
119
+ var obj = this;
120
+ var names = methodNames[i].split(".");
121
+ for(var n=0; n<names.length-1; n++){
122
+ var name = names[n];
123
+ if(obj[name]){
124
+ obj = obj[name];
125
+ }
126
+ else {
127
+ obj[name] = new Object();
128
+ obj = obj[name];
129
+ }
130
+ }
131
+ var name = names[names.length-1];
132
+ if(!obj[name]){
133
+ var method = new JSONRpcAsyncClient.AsyncMethod(this, methodNames[i]);
134
+ obj[name] = method;
135
+ }
136
+ }
137
+ }
138
+
139
+
140
+ /*******************************************************************
141
+ ******************************************************************/
142
+ // Async JSON-RPC call. The tricky part may be catching the
143
+ // state change when the call is complete, and unmarshalling the
144
+ // response.
145
+ // Possibility: Have the user pass in a caqllback method; this method
146
+ // assumes it will get called with the unnarshalled response.
147
+ // When the reqadyState goes to 4, grab the JSON response, unmarshal,
148
+ // and invokve the callback
149
+
150
+ JSONRpcAsyncClient.prototype.sendAsyncRequest =
151
+ function JSONRpcAsyncClient_sendAsyncRequest( methodName, call_back, args ) {
152
+ var obj = {"method" : methodName, "params" : args };
153
+ if (this.objectID) obj.objectID = this.objectID;
154
+
155
+ var req_data = obj.toJSON();
156
+
157
+ var http = JSONRpcAsyncClient.getHTTPRequest();
158
+
159
+ http.open("POST", this.serverURL, true); // async
160
+
161
+ try {
162
+ http.setRequestHeader( "Content-type", "text/plain" );
163
+ }
164
+ catch(e) {}
165
+
166
+
167
+ http.onreadystatechange = function() {
168
+ if ( http.readyState == 4 && http.status == 200) {
169
+ try {
170
+ s = "var obj = " + http.responseText
171
+ eval( s );
172
+ }
173
+ catch( e ) {
174
+ var e_msg = "onreadystatechange Error: " + e + "\nresponseText: " + http.responseText
175
+ obj.err = e.toString()
176
+ }
177
+ if( obj.error ) {
178
+ throw new JSONRpcAsyncClient.Exception( obj.error.code, obj.error.msg );
179
+ }
180
+ return call_back( obj.result );
181
+ }
182
+ else {
183
+ }
184
+ }
185
+
186
+ http.send( req_data );
187
+
188
+ }
189
+
190
+
191
+ /*******************************************************************
192
+ ******************************************************************/
193
+ JSONRpcAsyncClient.prototype.sendRequest =
194
+
195
+ function JSONRpcAsyncClient_sendRequest(methodName, args) {
196
+ // Make request object
197
+ var obj = {"method" : methodName, "params" : args};
198
+ if (this.objectID) obj.objectID = this.objectID;
199
+
200
+ // Marshall the request object to a JSON string
201
+ var req_data = obj.toJSON();
202
+
203
+ // Send the request
204
+ JSONRpcAsyncClient.http.open("POST", this.serverURL, false); // no async
205
+ // setRequestHeader is missing in Opera 8 Beta
206
+ try {
207
+ JSONRpcAsyncClient.http.setRequestHeader("Content-type", "text/plain");
208
+ }
209
+ catch(e) {}
210
+ JSONRpcAsyncClient.http.send(req_data);
211
+
212
+ // Unmarshall the response
213
+
214
+ try {
215
+ eval("var obj = " + JSONRpcAsyncClient.http.responseText);
216
+ }
217
+ catch(e) {
218
+ alert( "sendRequest error: " + e + "\nresponseText: " + JSONRpcAsyncClient.http.responseText )
219
+ obj.err = e.toString()
220
+ }
221
+ if( obj.error) {
222
+ alert( JSONRpcAsyncClient.http.responseText )
223
+ throw new JSONRpcAsyncClient.Exception(obj.error.code, obj.error.msg);
224
+ }
225
+ var res = obj.result;
226
+
227
+ // Handle CallableProxy
228
+ if(res && res.objectID && res.JSONRPCType == "CallableReference")
229
+ return new JSONRpcAsyncClient(this.serverURL, res.objectID);
230
+
231
+ return res;
232
+ }
233
+
234
+ /*******************************************************************
235
+ ******************************************************************/
236
+ JSONRpcAsyncClient.getHTTPRequest = function JSONRpcAsyncClient_getHTTPRequest() {
237
+
238
+ try { // to get the mozilla httprequest object
239
+ return new XMLHttpRequest();
240
+ }
241
+ catch(e) {}
242
+
243
+ try { // to get MS HTTP request object
244
+ return new ActiveXObject("Msxml2.XMLHTTP.4.0");
245
+ }
246
+ catch(e) {}
247
+
248
+ try { // to get MS HTTP request object
249
+ return new ActiveXObject("Msxml2.XMLHTTP");
250
+ }
251
+ catch(e) {}
252
+
253
+ try {// to get the old MS HTTP request object
254
+ return new ActiveXObject("microsoft.XMLHTTP");
255
+ }
256
+ catch(e) {}
257
+
258
+ throw new JSONRpcAsyncClient.Exception( 0, "Can't create XMLHttpRequest object");
259
+ }
260
+
261
+
@@ -1,2 +1,3 @@
1
- services/sample:
2
- - Details
1
+ path/to/file/name:
2
+ - ClassToRegister
3
+ - OtherClassToRegister
@@ -0,0 +1,29 @@
1
+
2
+
3
+ # Helper method to make it easier to grab the raw POST data,
4
+ # which where the JSON-RPC request is transported
5
+ class CGI
6
+ def raw_post
7
+ self.params.keys[0]
8
+ end
9
+ end
10
+
11
+
12
+
13
+ # Helper method that instatiates an object given a class name
14
+ def new_from_name( classname, *args)
15
+ cname = String.new( classname.untaint )
16
+ obj = nil
17
+ begin
18
+ obj = Object.const_get( cname ).new( *args )
19
+ rescue Exception
20
+ begin
21
+ require cname
22
+ obj = Object.const_get( cname ).new( *args )
23
+ rescue Exception
24
+ raise "Cannot create object #{cname}: #{$!}" unless obj
25
+ end
26
+ end
27
+ obj
28
+ end
29
+
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.6
3
3
  specification_version: 1
4
4
  name: Orbjson
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.0.3
7
- date: 2005-02-28
6
+ version: 0.0.4
7
+ date: 2005-03-14
8
8
  summary: Lib for creating JSON-RPC server applications.
9
9
  require_paths:
10
10
  - lib
@@ -28,56 +28,41 @@ authors:
28
28
  - James Britt
29
29
  files:
30
30
  - README
31
- - docs/todo.txt
31
+ - docs/getting_started_with_orbjson_short_version.pdf
32
+ - docs/tutorial_code.tgz
32
33
  - bin/orbjson
33
34
  - lib/skeleton
35
+ - lib/utils.rb
34
36
  - lib/orbjson.rb
35
37
  - lib/skeleton/cgi
36
38
  - lib/skeleton/script
37
39
  - lib/skeleton/webrick
38
- - lib/skeleton/cgi/json-rpc.rb,bak
39
40
  - lib/skeleton/cgi/json-rpc.rb
40
- - lib/skeleton/script/jsonrpc.js,bak
41
+ - lib/skeleton/script/jsonrpc_async.js
41
42
  - lib/skeleton/script/jsonrpc.js
42
- - lib/skeleton/webrick/config.yml
43
- - lib/skeleton/webrick/server.rb,bak
44
43
  - lib/skeleton/webrick/server.rb
44
+ - lib/skeleton/webrick/config.yml
45
45
  - examples/cgi
46
46
  - examples/webrick
47
- - examples/cgi/Orbjson.log
48
47
  - examples/cgi/httpd.conf
49
- - examples/cgi/orbjson_cgi.log
50
48
  - examples/cgi/yellow_crossfade_01.gif
51
49
  - examples/cgi/script
52
50
  - examples/cgi/services
53
- - examples/cgi/json-rpc.rb,bak
54
51
  - examples/cgi/json-rpc.rb
55
- - examples/cgi/index.html,bak
56
52
  - examples/cgi/index.html
57
- - examples/cgi/script/jsonrpc.js,bak
53
+ - examples/cgi/script/jsonrpc_async.js
58
54
  - examples/cgi/script/jsonrpc.js
59
- - examples/cgi/services/sample.rb,bak
60
55
  - examples/cgi/services/sample.rb
61
56
  - examples/webrick/config.yml
62
57
  - examples/webrick/yellow_crossfade_01.gif
63
58
  - examples/webrick/script
64
59
  - examples/webrick/services
65
- - examples/webrick/server.rb,bak
66
- - examples/webrick/server.rb
67
- - examples/webrick/index.html,bak
68
60
  - examples/webrick/index.html
69
- - examples/webrick/Orbjson.log
70
- - examples/webrick/script/jsonrpc.js,bak
61
+ - examples/webrick/index_async.html
62
+ - examples/webrick/server.rb
71
63
  - examples/webrick/script/jsonrpc.js
72
- - examples/webrick/services/sample.rb,bak
64
+ - examples/webrick/script/jsonrpc_async.js
73
65
  - examples/webrick/services/sample.rb
74
- - dep/handy
75
- - dep/required
76
- - dep/handy/script
77
- - dep/handy/script/jsonrpc.js,bak
78
- - dep/handy/script/jsonrpc.js
79
- - dep/required/ruby-json
80
- - dep/required/ruby-json/ruby-json-1.1.1.gem
81
66
  test_files: []
82
67
  rdoc_options: []
83
68
  extra_rdoc_files: []
@@ -1,187 +0,0 @@
1
- /*
2
- * JSON-RPC JavaScript client
3
- *
4
- * $Id: jsonrpc.js,v 1.1.1.1 2005/02/28 03:06:30 jamesgbritt Exp $
5
- *
6
- * Copyright (c) 2003-2004 Jan-Klaas Kollhof
7
- * Copyright (c) 2005 Michael Clark, Metaparadigm Pte Ltd
8
- *
9
- * This code is based on Jan-Klaas' JavaScript o lait library (jsolait).
10
- *
11
- * This library is free software; you can redistribute it and/or
12
- * modify it under the terms of the GNU Lesser General Public (LGPL)
13
- * License as published by the Free Software Foundation; either
14
- * version 2.1 of the License, or (at your option) any later version.
15
- *
16
- * This library is distributed in the hope that it will be useful,
17
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
- * Lesser General Public License for more details: http://www.gnu.org/
20
- *
21
- */
22
-
23
- // TODO: Add async
24
- // TODO: Add HTTP auth (user, password)
25
-
26
- Object.prototype.toJSON = function Object_toJSON() {
27
- var v = [];
28
- for(attr in this) {
29
- if(this[attr] == null) v.push("\"" + attr + "\": null");
30
- else if(typeof this[attr] == "function"); // skip
31
- else v.push("\"" + attr + "\": " + this[attr].toJSON());
32
- }
33
- return "{" + v.join(", ") + "}";
34
- }
35
-
36
- String.prototype.toJSON = function String_toJSON() {
37
- return "\"" + this.replace(/([\"\\])/g, "\\$1") + "\"";
38
- }
39
-
40
- Number.prototype.toJSON = function Number_toJSON() {
41
- return this.toString();
42
- }
43
-
44
- Boolean.prototype.toJSON = function Boolean_toJSON() {
45
- return this.toString();
46
- }
47
-
48
- Date.prototype.toJSON = function Date_toJSON() {
49
- this.valueOf().toString();
50
- }
51
-
52
- Array.prototype.toJSON = function Array_toJSON() {
53
- var v = [];
54
- for(var i=0; i<this.length; i++) {
55
- if(this[i] == null) v.push("null");
56
- else v.push(this[i].toJSON());
57
- }
58
- return "[" + v.join(", ") + "]";
59
- }
60
-
61
- JSONRpcClient = function JSONRpcClient_ctor(serverURL, objectID) {
62
- this.serverURL = serverURL;
63
- this.objectID = objectID;
64
-
65
- // Lazy initialization of the XMLHttpRequest singleton
66
- if(!JSONRpcClient.http)
67
- JSONRpcClient.http = JSONRpcClient.getHTTPRequest();
68
-
69
- // Add standard methods
70
- this.addMethods(["system.listMethods"]);
71
-
72
- // Query the methods on the server and add them to this object
73
- var m = this.sendRequest("system.listMethods", []);
74
- this.addMethods(m);
75
- }
76
-
77
- JSONRpcClient.Exception = function JSONRpcClient_Exception_ctor(code, msg) {
78
- this.code = code;
79
- this.msg = msg;
80
- }
81
-
82
- JSONRpcClient.Exception.prototype.toString =
83
- function JSONRpcClient_Exception_toString(code, msg) {
84
- return "JSONRpcClientException: " + this.msg;
85
- }
86
-
87
- JSONRpcClient.Method =
88
- function JSONRpcClient_Method_ctor(client, methodName) {
89
- var fn=function() {
90
- var args = [];
91
- for(var i=0;i<arguments.length;i++) {
92
- args.push(arguments[i]);
93
- }
94
- return fn.client.sendRequest.call(fn.client, fn.methodName, args);
95
- }
96
- fn.client = client;
97
- fn.methodName = methodName;
98
- return fn;
99
- }
100
-
101
- JSONRpcClient.prototype.addMethods =
102
- function JSONRpcClient_addMethods(methodNames) {
103
- for(var i=0; i<methodNames.length; i++) {
104
- var obj = this;
105
- var names = methodNames[i].split(".");
106
- for(var n=0; n<names.length-1; n++){
107
- var name = names[n];
108
- if(obj[name]){
109
- obj = obj[name];
110
- }
111
- else {
112
- obj[name] = new Object();
113
- obj = obj[name];
114
- }
115
- }
116
- var name = names[names.length-1];
117
- if(!obj[name]){
118
- var method = new JSONRpcClient.Method(this, methodNames[i]);
119
- obj[name] = method;
120
- }
121
- }
122
- }
123
-
124
- JSONRpcClient.prototype.sendRequest =
125
- function JSONRpcClient_sendRequest(methodName, args) {
126
- // Make request object
127
- var obj = {"method" : methodName, "params" : args};
128
- if (this.objectID) obj.objectID = this.objectID;
129
-
130
- // Marshall the request object to a JSON string
131
- var req_data = obj.toJSON();
132
-
133
- // Send the request
134
- JSONRpcClient.http.open("POST", this.serverURL, false); // no async
135
- // setRequestHeader is missing in Opera 8 Beta
136
- try {
137
- JSONRpcClient.http.setRequestHeader("Content-type", "text/plain");
138
- }
139
- catch(e) {}
140
- JSONRpcClient.http.send(req_data);
141
-
142
- // Unmarshall the response
143
- // DEBUG
144
- try {
145
- eval("var obj = " + JSONRpcClient.http.responseText);
146
- }
147
- catch(e) {
148
- alert( e )
149
- alert( JSONRpcClient.http.responseText )
150
- obj.err = e.toString()
151
- }
152
- if( obj.error) {
153
- alert( JSONRpcClient.http.responseText )
154
- throw new JSONRpcClient.Exception(obj.error.code, obj.error.msg);
155
- }
156
- var res = obj.result;
157
-
158
- // Handle CallableProxy
159
- if(res && res.objectID && res.JSONRPCType == "CallableReference")
160
- return new JSONRpcClient(this.serverURL, res.objectID);
161
-
162
- return res;
163
- }
164
-
165
- JSONRpcClient.getHTTPRequest = function JSONRpcClient_getHTTPRequest() {
166
- try { // to get the mozilla httprequest object
167
- return new XMLHttpRequest();
168
- }
169
- catch(e) {}
170
-
171
- try { // to get MS HTTP request object
172
- return new ActiveXObject("Msxml2.XMLHTTP.4.0");
173
- }
174
- catch(e) {}
175
-
176
- try { // to get MS HTTP request object
177
- return new ActiveXObject("Msxml2.XMLHTTP");
178
- }
179
- catch(e) {}
180
-
181
- try {// to get the old MS HTTP request object
182
- return new ActiveXObject("microsoft.XMLHTTP");
183
- }
184
- catch(e) {}
185
-
186
- throw new JSONRpcClient.Exception(0, "Can't create XMLHttpRequest object");
187
- }