polytalk 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +50 -3
- data/examples/server.rb +1 -0
- data/lib/polytalk/server.rb +21 -4
- data/lib/polytalk/version.rb +1 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -1,6 +1,18 @@
|
|
1
1
|
# Polytalk
|
2
2
|
|
3
|
-
|
3
|
+
Polytalk is a simple protocol which allows communication between different languages via TCP.
|
4
|
+
|
5
|
+
Polytalk currently supports PHP, Node.js and Ruby.
|
6
|
+
|
7
|
+
## Protocol
|
8
|
+
|
9
|
+
The protocol is a simple language agnostic JSON object containing the class, method and arguments. It will then return an response as either a string or JSON object.
|
10
|
+
|
11
|
+
Key | Value
|
12
|
+
------------ | -------------
|
13
|
+
class | The class to call the method on. Namespaced classes require the `::` separator.
|
14
|
+
method | The method you want to call.
|
15
|
+
arguments | The arguments to inject into the method in key value pairs.
|
4
16
|
|
5
17
|
## Installation
|
6
18
|
|
@@ -16,9 +28,40 @@ Or install it yourself as:
|
|
16
28
|
|
17
29
|
$ gem install polytalk
|
18
30
|
|
19
|
-
##
|
31
|
+
## Server Example
|
32
|
+
|
33
|
+
Be sure that any classes you want to be exposed by the server to the client are included/required from the server.
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
server = Polytalk::Server.new({ port: 9090 })
|
37
|
+
server.run do |connection, request|
|
38
|
+
response = server.call(request)
|
39
|
+
server.push(connection, response)
|
40
|
+
end
|
41
|
+
```
|
20
42
|
|
21
|
-
|
43
|
+
## Client Example
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
request = {
|
47
|
+
class: 'Model::Order',
|
48
|
+
method: 'findBySize',
|
49
|
+
arguments: {
|
50
|
+
size: 'large',
|
51
|
+
limit: 3
|
52
|
+
}
|
53
|
+
}
|
54
|
+
|
55
|
+
client = Polytalk::Client.new({ port: 9090 })
|
56
|
+
|
57
|
+
puts client.call(request)
|
58
|
+
|
59
|
+
# Passing a block
|
60
|
+
first = client.call(request) do |r|
|
61
|
+
r.first
|
62
|
+
end
|
63
|
+
puts first
|
64
|
+
```
|
22
65
|
|
23
66
|
## Contributing
|
24
67
|
|
@@ -27,3 +70,7 @@ TODO: Write usage instructions here
|
|
27
70
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
71
|
4. Push to the branch (`git push origin my-new-feature`)
|
29
72
|
5. Create new Pull Request
|
73
|
+
|
74
|
+
### License
|
75
|
+
|
76
|
+
MIT, see LICENSE.
|
data/examples/server.rb
CHANGED
data/lib/polytalk/server.rb
CHANGED
@@ -14,22 +14,39 @@ module Polytalk
|
|
14
14
|
loop do
|
15
15
|
Thread.start(server.accept) do |connection|
|
16
16
|
request = connection.recv(2048)
|
17
|
-
yield(connection, JSON.parse(request))
|
17
|
+
yield(connection, JSON.parse(request, symbolize_names: true))
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
22
22
|
def call(request)
|
23
23
|
required_constant = nil
|
24
|
-
request[
|
24
|
+
request[:class].split('::').each_with_index do |c, index|
|
25
25
|
if index == 0
|
26
26
|
required_constant = Kernel.const_get(c)
|
27
27
|
else
|
28
28
|
required_constant = required_constant.const_get(c)
|
29
29
|
end
|
30
30
|
end
|
31
|
-
|
32
|
-
|
31
|
+
|
32
|
+
# Convert string prefixed with `:` to symbols
|
33
|
+
if request[:arguments].is_a? Hash
|
34
|
+
request[:arguments].each do |key, arg|
|
35
|
+
if arg.is_a? String
|
36
|
+
if arg.match(/^:/)
|
37
|
+
request[:arguments][key] = arg[1..-1].to_sym
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
required_method = required_constant.method(request[:method])
|
44
|
+
|
45
|
+
if request[:arguments].is_a? Hash
|
46
|
+
required_method.call *request[:arguments].values
|
47
|
+
else
|
48
|
+
required_method.call
|
49
|
+
end
|
33
50
|
end
|
34
51
|
|
35
52
|
def push(connection, response)
|
data/lib/polytalk/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: polytalk
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-08 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: Polytalk is a simple protocol which allows communication between different
|
15
15
|
languages via TCP.
|
@@ -52,7 +52,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
52
52
|
version: '0'
|
53
53
|
requirements: []
|
54
54
|
rubyforge_project:
|
55
|
-
rubygems_version: 1.8.
|
55
|
+
rubygems_version: 1.8.24
|
56
56
|
signing_key:
|
57
57
|
specification_version: 3
|
58
58
|
summary: Polytalk is a simple protocol which allows communication between different
|