Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 79 lines (50 sloc) 2.064 kB
e962a61 @yankov description of nourl
authored
1 Nourl
2 =====
3
4 Nourl is a simple library that allows you to call models' methods on server from
5 your javascript as if they were local.
6
7 It consists of two parts: a simple ruby JSON-RPC server and javascript client.
8 JS client automatically creates stubs for backend classes and methods you want to use and allows you to call
9 them as if they were defined in javascript.
10
11 ..What?
12 -------
13 So, say, on your backend side you have a model `User` with the method `get`.
14
bd025d3 @yankov fix md formatting
authored
15 class User
16 def self.get(name)
17 User.find_by_name(name)
18 end
e962a61 @yankov description of nourl
authored
19 end
20
1ddf729 @yankov update manual
authored
21 Now, from my client JS you want to be able to call user.get without making any API calls,
22 creating controllers and dealing with RPC manually. It works just like this:
e962a61 @yankov description of nourl
authored
23
bd025d3 @yankov fix md formatting
authored
24 user.get("John", function(result) {
25 console.log(result);
26 })
e962a61 @yankov description of nourl
authored
27
28 That's it.
29
30 How to use it
31 -------------
32
bd025d3 @yankov fix md formatting
authored
33 `gem install nourl`
e962a61 @yankov description of nourl
authored
34 `git clone https://github.com/yankov/nourl.git`
35
36 Then you can run an example server.rb from example folder:
37
bd025d3 @yankov fix md formatting
authored
38 `cd ./nourl/examples`
e962a61 @yankov description of nourl
authored
39 `ruby -rubygems server.rb`
40
a718e9c @yankov update manual.. forgot to mention sinatra
authored
41 To use server.rb from examples you may need to install sinatra first: `gem install sinatra`, but
1319498 @yankov update manual
authored
42 it's easy to build your own server using Rails or whatever you like. You just have to have a /rpc endpoint, pass parameters to to Nourl.exec and respond with the json string.
a718e9c @yankov update manual.. forgot to mention sinatra
authored
43
1ddf729 @yankov update manual
authored
44 You can include your classes that you want to provide RPC access to in server.rb.
45 Then include Nourl::RPCable in your class and list methods that allowed for remote calls.
46
47 class User
48 include Nourl::RPCable
49
50 allow_rpc_for :create, :destroy
51 ...
52 end
53
54 Then in your JS application.
55
56 <script type="text/javascript">
57
58 var settings = {
59 rpcUrl: "http://0.0.0.0:4567/rpc", //host and port where your server is running
60 transport: "ajax",
61 require: ["user"] // list all classes that you wanna access to
62 }
63
64 nourl.run(settings, function(){
65
66 user.get('john', function(result) {
67 console.log(result);
68 });
69
70 });
71
72 </script>
73
e962a61 @yankov description of nourl
authored
74
75
76
77
78
Something went wrong with that request. Please try again.