polytalk 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.DS_Store
2
+ *.gem
3
+ *.rbc
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in polytalk.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Andrew Weir
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # Polytalk
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'polytalk'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install polytalk
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,40 @@
1
+ require_relative '../lib/Polytalk/client.rb'
2
+
3
+ request = {
4
+ class: 'Model::Order',
5
+ method: 'findBySize',
6
+ arguments: {
7
+ size: 'large',
8
+ limit: 3
9
+ }
10
+ }
11
+
12
+ request2 = {
13
+ class: 'Test',
14
+ method: 'add',
15
+ arguments: {
16
+ a: 2,
17
+ b: 3
18
+ }
19
+ }
20
+
21
+ request3 = {
22
+ class: 'Test',
23
+ method: 'shout',
24
+ arguments: {
25
+ words: 'hello how are you doing?'
26
+ }
27
+ }
28
+
29
+ client = Polytalk::Client.new({ port: 9090 })
30
+
31
+ puts client.call(request)
32
+
33
+ # Passing a block
34
+ first = client.call(request) do |r|
35
+ r.first
36
+ end
37
+ puts first
38
+
39
+ puts client.call(request2)
40
+ puts client.call(request3)
@@ -0,0 +1,16 @@
1
+ module Model
2
+ class Order
3
+ def self.findBySize(size, limit)
4
+ orders = [
5
+ {id: 1, name: 'Order 1', size: 'large'},
6
+ {id: 2, name: 'Order 2', size: 'small'},
7
+ {id: 3, name: 'Order 3', size: 'large'},
8
+ {id: 4, name: 'Order 4', size: 'large'},
9
+ {id: 5, name: 'Order 5', size: 'medium'},
10
+ {id: 6, name: 'Order 6', size: 'medium'},
11
+ {id: 7, name: 'Order 7', size: 'small'}
12
+ ]
13
+ orders.find_all{|order| size == order[:size]}[0,limit]
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,18 @@
1
+ require_relative '../lib/Polytalk/server.rb'
2
+ require_relative 'model/order.rb'
3
+
4
+ class Test
5
+ def self.add (a, b)
6
+ return a + b
7
+ end
8
+
9
+ def self.shout (words)
10
+ words.upcase
11
+ end
12
+ end
13
+
14
+ server = Polytalk::Server.new({ port: 9090 })
15
+ server.run do |connection, request|
16
+ response = server.call(request)
17
+ server.push(connection, response)
18
+ end
@@ -0,0 +1,31 @@
1
+ module Polytalk
2
+ class Client
3
+
4
+ require 'socket'
5
+ require 'json'
6
+
7
+ def initialize(config = {})
8
+ @port = config['port'] || 9090
9
+ @host = config['host'] || '127.0.0.1'
10
+ end
11
+
12
+ def call(request)
13
+ client = TCPSocket.new(@host, @port)
14
+ client.print request.to_json
15
+ response = client.recv(2048)
16
+ client.close
17
+
18
+ if response.kind_of?(String) && (response.match(/^{/) || response.match(/^\[{/))
19
+ response = JSON.parse(response)
20
+ end
21
+
22
+ if block_given?
23
+ yield(response)
24
+ else
25
+ return response
26
+ end
27
+
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,41 @@
1
+ module Polytalk
2
+ class Server
3
+
4
+ require 'socket'
5
+ require 'json'
6
+
7
+ def initialize(config = {})
8
+ @port = config['port'] || 9090
9
+ @host = config['host'] || '127.0.0.1'
10
+ end
11
+
12
+ def run
13
+ server = TCPServer.new(@host, @port)
14
+ loop do
15
+ Thread.start(server.accept) do |connection|
16
+ request = connection.recv(2048)
17
+ yield(connection, JSON.parse(request))
18
+ end
19
+ end
20
+ end
21
+
22
+ def call(request)
23
+ required_constant = nil
24
+ request['class'].split('::').each_with_index do |c, index|
25
+ if index == 0
26
+ required_constant = Kernel.const_get(c)
27
+ else
28
+ required_constant = required_constant.const_get(c)
29
+ end
30
+ end
31
+ required_method = required_constant.method(request['method'])
32
+ required_method.call *request['arguments'].values
33
+ end
34
+
35
+ def push(connection, response)
36
+ connection.write(response.to_json)
37
+ connection.close
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,3 @@
1
+ module Polytalk
2
+ VERSION = "0.0.1"
3
+ end
data/lib/polytalk.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "polytalk/version"
2
+ require "polytalk/server"
3
+ require "polytalk/client"
4
+
5
+ module Polytalk
6
+ end
data/polytalk.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'polytalk/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "polytalk"
8
+ gem.version = Polytalk::VERSION
9
+ gem.authors = ["Andrew Weir"]
10
+ gem.email = ["andru.weir@gmail.com"]
11
+ gem.description = %q{Polytalk is a simple protocol which allows communication between different languages via TCP.}
12
+ gem.summary = %q{Polytalk is a simple protocol which allows communication between different languages via TCP.}
13
+ gem.homepage = ""
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
metadata ADDED
@@ -0,0 +1,61 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: polytalk
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Andrew Weir
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-19 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Polytalk is a simple protocol which allows communication between different
15
+ languages via TCP.
16
+ email:
17
+ - andru.weir@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - examples/client.rb
28
+ - examples/model/order.rb
29
+ - examples/server.rb
30
+ - lib/polytalk.rb
31
+ - lib/polytalk/client.rb
32
+ - lib/polytalk/server.rb
33
+ - lib/polytalk/version.rb
34
+ - polytalk.gemspec
35
+ homepage: ''
36
+ licenses: []
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ required_rubygems_version: !ruby/object:Gem::Requirement
48
+ none: false
49
+ requirements:
50
+ - - ! '>='
51
+ - !ruby/object:Gem::Version
52
+ version: '0'
53
+ requirements: []
54
+ rubyforge_project:
55
+ rubygems_version: 1.8.11
56
+ signing_key:
57
+ specification_version: 3
58
+ summary: Polytalk is a simple protocol which allows communication between different
59
+ languages via TCP.
60
+ test_files: []
61
+ has_rdoc: