Orbjson 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README CHANGED
@@ -1,8 +1,31 @@
1
1
  README for Roy Orbjson , the JSON-RPC/Ruby ORB
2
2
 
3
- Orbjson is a JSON-RPC ORB library that allows Web applications
4
- to respond to JSON-RPC requests (including, but not limited to,
5
- the XmlHttpRequest object), executing Ruby code on the server
6
- but sending back JSON-serialized objects to the client.
3
+ Orbjson is a JSON-RPC ORB (object request broker) library that allows Web applications to respond to JSON-RPC requests (including, but not limited to, the XmlHttpRequest object), executing Ruby code on the server but sending back JSON-serialized objects to the client.
4
+
5
+ Client code may then be written that treats all method invocations as local; the jsonrpc.js handles the object serialization for the client, and the ruby-json code handles the json marshaling on the server.
6
+
7
+ For more API detals, see the docs for Orbjson::System
8
+
9
+ For a more general explanation, with an example, please see http://orbjson.rubyforge.org/tutorial
10
+
11
+ For general inforamtion, please see http://orbjson.rubyforge.org
12
+
13
+ Copyright (C) 2005 James Britt & Neurogami LLC
14
+
15
+ Big thanks to Florian Franks for his ruby-json lib, and Jamis Buck for Needle
16
+
17
+ This program is free software; you can redistribute it and/or
18
+ modify it under the terms of the GNU General Public License,
19
+ version 2, as published by the Free Software Foundation.
20
+
21
+ This program is distributed in the hope that it will be
22
+ useful, but WITHOUT ANY WARRANTY; without even the implied
23
+ warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
24
+ PURPOSE. See the GNU General Public License for more details.
25
+
26
+ You should have received a copy of the GNU General Public
27
+ License along with this program; if not, write to the Free
28
+ Software Foundation, Inc., 59 Temple Place, Suite 330,
29
+ Boston, MA 02111-1307 USA.
7
30
 
8
31
 
@@ -38,6 +38,11 @@ USAGE
38
38
  end
39
39
 
40
40
 
41
+ def copy_common( dest_path )
42
+ File.copy File.join( SKEL_DIR, 'script', 'jsonrpc.js' ), File.join( dest_path , 'script'), true
43
+ File.copy File.join( SKEL_DIR, 'script', 'jsonrpc_async.js' ), File.join( dest_path , 'script'), true
44
+ end
45
+
41
46
  def run
42
47
  case command = ARGV[0] || usage()
43
48
  when 'create-cgi'
@@ -53,7 +58,8 @@ def run
53
58
  File.makedirs path
54
59
  File.makedirs File.join(path, 'script')
55
60
  File.copy File.join( src, 'json-rpc.rb' ), path, true
56
- File.copy File.join( SKEL_DIR, 'script', 'jsonrpc.js' ), File.join( path, 'script'), true
61
+ copy_common( path )
62
+
57
63
 
58
64
  when 'create-webrick'
59
65
  path = ARGV[1] || usage()
@@ -69,7 +75,7 @@ def run
69
75
  File.makedirs File.join(path, 'script')
70
76
  File.copy File.join( src, 'server.rb' ), path, true
71
77
  File.copy File.join( src, 'config.yml' ), path, true
72
- File.copy File.join( SKEL_DIR, 'script', 'jsonrpc.js' ), File.join( path, 'script'), true
78
+ copy_common( path )
73
79
  else
74
80
  usage()
75
81
  end
Binary file
@@ -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:30 jamesgbritt Exp $
4
+ * $Id: jsonrpc.js,v 1.2 2005/03/15 02:29:22 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,260 @@
1
+ /*
2
+ * JSON-RPC JavaScript client
3
+ *
4
+ * $Id: jsonrpc_async.js,v 1.1 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
+
183
+ }
184
+
185
+ http.send( req_data );
186
+
187
+ }
188
+
189
+
190
+ /*******************************************************************
191
+ ******************************************************************/
192
+ JSONRpcAsyncClient.prototype.sendRequest =
193
+
194
+ function JSONRpcAsyncClient_sendRequest(methodName, args) {
195
+ // Make request object
196
+ var obj = {"method" : methodName, "params" : args};
197
+ if (this.objectID) obj.objectID = this.objectID;
198
+
199
+ // Marshall the request object to a JSON string
200
+ var req_data = obj.toJSON();
201
+
202
+ // Send the request
203
+ JSONRpcAsyncClient.http.open("POST", this.serverURL, false); // no async
204
+ // setRequestHeader is missing in Opera 8 Beta
205
+ try {
206
+ JSONRpcAsyncClient.http.setRequestHeader("Content-type", "text/plain");
207
+ }
208
+ catch(e) {}
209
+ JSONRpcAsyncClient.http.send(req_data);
210
+
211
+ // Unmarshall the response
212
+
213
+ try {
214
+ eval("var obj = " + JSONRpcAsyncClient.http.responseText);
215
+ }
216
+ catch(e) {
217
+ alert( "sendRequest error: " + e + "\nresponseText: " + JSONRpcAsyncClient.http.responseText )
218
+ obj.err = e.toString()
219
+ }
220
+ if( obj.error) {
221
+ alert( JSONRpcAsyncClient.http.responseText )
222
+ throw new JSONRpcAsyncClient.Exception(obj.error.code, obj.error.msg);
223
+ }
224
+ var res = obj.result;
225
+
226
+ // Handle CallableProxy
227
+ if(res && res.objectID && res.JSONRPCType == "CallableReference")
228
+ return new JSONRpcAsyncClient(this.serverURL, res.objectID);
229
+
230
+ return res;
231
+ }
232
+
233
+ /*******************************************************************
234
+ ******************************************************************/
235
+ JSONRpcAsyncClient.getHTTPRequest = function JSONRpcAsyncClient_getHTTPRequest() {
236
+
237
+ try { // to get the mozilla httprequest object
238
+ return new XMLHttpRequest();
239
+ }
240
+ catch(e) {}
241
+
242
+ try { // to get MS HTTP request object
243
+ return new ActiveXObject("Msxml2.XMLHTTP.4.0");
244
+ }
245
+ catch(e) {}
246
+
247
+ try { // to get MS HTTP request object
248
+ return new ActiveXObject("Msxml2.XMLHTTP");
249
+ }
250
+ catch(e) {}
251
+
252
+ try {// to get the old MS HTTP request object
253
+ return new ActiveXObject("microsoft.XMLHTTP");
254
+ }
255
+ catch(e) {}
256
+
257
+ throw new JSONRpcAsyncClient.Exception( 0, "Can't create XMLHttpRequest object");
258
+ }
259
+
260
+
@@ -61,7 +61,7 @@
61
61
  <form name='metatagForm' action='#' method="get">
62
62
  <fieldset id='metatagFields'>
63
63
  <legend>Topic info</legend>
64
- <div id='details'></div>
64
+ <div id='details'></div>
65
65
  </fieldset>
66
66
  </form>
67
67
  </div>
@@ -1,69 +1,77 @@
1
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
- <html xmlns="http://www.w3.org/1999/xhtml">
3
- <head>
4
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
- <title>Metatagging</title>
6
- <script type="text/javascript" src="./script/jsonrpc.js"></script>
7
- <script language="javascript" type="text/javascript">
8
- var jsonurl = "http://127.0.0.1:2222/json-rpc";
9
- var jsonrpc = null;
10
-
11
- function init() {
12
- jsonrpc = new JSONRpcClient( jsonurl );
13
- }
14
-
15
- function show_details( topic ) {
16
- var el = document.getElementById( 'details' );
17
- var html = jsonrpc.details.fetch( topic)
18
- el.innerHTML = html;
19
- }
20
-
21
- </script>
22
-
23
- <style type="text/css">
24
- #metatagFields #reset {
25
- text-align: right;
26
- }
27
- body {
28
- font-family: Verdana, Arial, Helvetica, sans-serif;
29
- color: #666666;
30
- }
31
- a {
32
- text-decoration: none;
33
- }
34
-
35
- fieldset {
36
- background-image: url(yellow_crossfade_01.gif);
37
- background-repeat: repeat-y;
38
- background-position: left top;
39
- }
40
-
41
- #metatagFields {
42
- width: 300px;
43
- margin-top: 10px;
44
- }
45
-
46
- #metatagFields legend {
47
- background-color: #FFFFFF;
48
- border: 1px dashed #333333;
49
- }
50
- </style>
51
-
52
- </head>
53
- <body onload='init()' >
54
-
55
- <div id='instructions'>Select a topic to seee more about it</div>
56
- <div id='curentItemList'>
57
- <a href='javascript:show_details( "JSON")'>JSON</a>, <a href='javascript:show_details( "JSON-RPC")'>JSON-RPC</a>, <a href='javascript:show_details( "Ruby")' >Ruby</a>
58
- </div>
59
-
60
- <div id='currentSelectionSet'>
61
- <form name='metatagForm' action='#' method="get">
62
- <fieldset id='metatagFields'>
63
- <legend>Topic info</legend>
64
- <div id='details'></div>
65
- </fieldset>
66
- </form>
67
- </div>
68
- </body>
69
- </html>
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+ <html xmlns="http://www.w3.org/1999/xhtml">
3
+ <head>
4
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+ <title>Metatagging</title>
6
+ <script type="text/javascript" src="./script/jsonrpc_async.js"></script>
7
+ <script language="javascript" type="text/javascript">
8
+
9
+ var jsonurl = "http://127.0.0.1:2222/json-rpc";
10
+ var jsonrpc = null;
11
+ var details = null;
12
+
13
+ function init() {
14
+ jsonrpc = new JSONRpcAsyncClient( jsonurl );
15
+ details = jsonrpc.details
16
+ }
17
+
18
+ function show_details( topic ) {
19
+ var el = document.getElementById( 'details' );
20
+
21
+ f = function( res ){
22
+ el.innerHTML = res
23
+ }
24
+
25
+ details.fetch( f, topic )
26
+
27
+ }
28
+
29
+ </script>
30
+
31
+ <style type="text/css">
32
+ #metatagFields #reset {
33
+ text-align: right;
34
+ }
35
+ body {
36
+ font-family: Verdana, Arial, Helvetica, sans-serif;
37
+ color: #666666;
38
+ }
39
+ a {
40
+ text-decoration: none;
41
+ }
42
+
43
+ fieldset {
44
+ background-image: url(yellow_crossfade_01.gif);
45
+ background-repeat: repeat-y;
46
+ background-position: left top;
47
+ }
48
+
49
+ #metatagFields {
50
+ width: 300px;
51
+ margin-top: 10px;
52
+ }
53
+
54
+ #metatagFields legend {
55
+ background-color: #FFFFFF;
56
+ border: 1px dashed #333333;
57
+ }
58
+ </style>
59
+
60
+ </head>
61
+ <body onload='init()' >
62
+
63
+ <div id='instructions'>Select a topic to seee more about it</div>
64
+ <div id='curentItemList'>
65
+ <a href='javascript:show_details( "JSON")'>JSON</a>, <a href='javascript:show_details( "JSON-RPC")'>JSON-RPC</a>, <a href='javascript:show_details( "Ruby")' >Ruby</a>
66
+ </div>
67
+
68
+ <div id='currentSelectionSet'>
69
+ <form name='metatagForm' action='#' method="get">
70
+ <fieldset id='metatagFields'>
71
+ <legend>Topic info</legend>
72
+ <div id='details'></div>
73
+ </fieldset>
74
+ </form>
75
+ </div>
76
+ </body>
77
+ </html>