jpc 1.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +77 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jpc.gemspec +24 -0
- data/lib/jpc.rb +10 -0
- data/lib/jpc/dispatcher.rb +37 -0
- data/lib/jpc/errors.rb +9 -0
- data/lib/jpc/handler.rb +30 -0
- data/lib/jpc/helpers.rb +26 -0
- data/lib/jpc/invoker.rb +53 -0
- data/lib/jpc/version.rb +3 -0
- metadata +117 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 44e0f3d3575d9354f06be32bea3a575703fcc549
|
4
|
+
data.tar.gz: 354b7950320b1d9d953b8aaa4a640da5608199ff
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a475fdce6035e2ae61af9e53abaca5ddfbf73d3942abfc9e45581a9795e920f4c5c83566a5bb5effe02cc19688d41e0006d9bbc59535c4201b273de4fe3d8786
|
7
|
+
data.tar.gz: f65d9494aa7dd571d7f32be98cbec80b26dace79cbb57f681c00e860bba76f0a2ccd5172e5ddf3e2aa58c4cdad681e031e72fbc37c6b88e7934e90a97ad4c46a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Denis Lysenko
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# JPC
|
2
|
+
|
3
|
+
This gem implements server-side support for JSON-RPC 2.0 via websockets over EventMachine.
|
4
|
+
|
5
|
+
However it implements channels support to broadcast functions like ActionCable.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add follow line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'jpc', github: 'lysenkooo/jpc-ruby'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
First of all you need to create handler.
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class MainHandler < JPC::Handler
|
25
|
+
def test_hash_params(params)
|
26
|
+
"Success with #{params.inspect}"
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_array_params(first, second, third)
|
30
|
+
"Success with #{first} #{second} #{third}"
|
31
|
+
end
|
32
|
+
|
33
|
+
def broadcast_message(message)
|
34
|
+
@dispatcher.cast('main_channel', message)
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
def procedures
|
40
|
+
%w[test_hash_params test_array_params broadcast_message]
|
41
|
+
end
|
42
|
+
end
|
43
|
+
```
|
44
|
+
|
45
|
+
After that you should create invoker instance and set up forwarding JSON
|
46
|
+
messages from your client.
|
47
|
+
|
48
|
+
```ruby
|
49
|
+
EventMachine.run do
|
50
|
+
dispatcher = JPC::Dispatcher.new
|
51
|
+
|
52
|
+
EM::WebSocket.run(host: '0.0.0.0', port: 8090) do |ws|
|
53
|
+
handler = MainHandler.new(ws, dispatcher)
|
54
|
+
rpc = JPC::Invoker.new(handler)
|
55
|
+
|
56
|
+
ws.onmessage do |message|
|
57
|
+
response = rpc.invoke(message)
|
58
|
+
ws.send(response)
|
59
|
+
end
|
60
|
+
|
61
|
+
ws.onerror do |error|
|
62
|
+
backtrace = error.backtrace.join("\n")
|
63
|
+
puts "#{error}\n#{backtrace}\n"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
Now you can use JSON-RPC 2.0 client to send messages to your websocket.
|
70
|
+
|
71
|
+
## Contributing
|
72
|
+
|
73
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/lysenkooo/jpc-ruby.
|
74
|
+
|
75
|
+
## License
|
76
|
+
|
77
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "jpc"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
data/jpc.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'jpc/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "jpc"
|
8
|
+
spec.version = JPC::VERSION
|
9
|
+
spec.authors = ["Denis Lysenko"]
|
10
|
+
spec.email = ["d@lysenkooo.ru"]
|
11
|
+
spec.summary = %q{JPC is JSON-RPC 2.0 Ruby Library.}
|
12
|
+
spec.description = %q{This library provide possibility to handle JSON requests.}
|
13
|
+
spec.homepage = "http://lysenkooo.ru"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
23
|
+
spec.add_dependency "oj", ">= 2.0.0"
|
24
|
+
end
|
data/lib/jpc.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
class JPC::Dispatcher
|
2
|
+
include JPC::Helpers
|
3
|
+
|
4
|
+
def subscribe(ws, channel)
|
5
|
+
channels[channel.to_sym] ||= []
|
6
|
+
channels[channel.to_sym] << ws
|
7
|
+
|
8
|
+
{ channel: channel, status: 'subscribed' }
|
9
|
+
end
|
10
|
+
|
11
|
+
def unsubscribe(ws, channel)
|
12
|
+
fail "Channel #{channel} not found" unless channels[channel.to_sym]
|
13
|
+
|
14
|
+
channels[channel.to_sym] -= [ws]
|
15
|
+
|
16
|
+
{ channel: channel, status: 'unsubscribed' }
|
17
|
+
end
|
18
|
+
|
19
|
+
def cast(channel, payload)
|
20
|
+
message = make_message(channel: channel, payload: payload)
|
21
|
+
|
22
|
+
sent = 0
|
23
|
+
|
24
|
+
channels[channel.to_sym] && channels[channel.to_sym].each do |ws|
|
25
|
+
ws.send(message)
|
26
|
+
sent += 1
|
27
|
+
end
|
28
|
+
|
29
|
+
{ channel: channel, sent: sent }
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def channels
|
35
|
+
@channels ||= {}
|
36
|
+
end
|
37
|
+
end
|
data/lib/jpc/errors.rb
ADDED
data/lib/jpc/handler.rb
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
class JPC::Handler
|
2
|
+
attr_accessor :token
|
3
|
+
|
4
|
+
def initialize(ws, dispatcher)
|
5
|
+
@ws = ws
|
6
|
+
@dispatcher = dispatcher
|
7
|
+
end
|
8
|
+
|
9
|
+
def ping(params = {})
|
10
|
+
"pong #{params}"
|
11
|
+
end
|
12
|
+
|
13
|
+
def subscribe(channel)
|
14
|
+
@dispatcher.subscribe(@ws, channel)
|
15
|
+
end
|
16
|
+
|
17
|
+
def unsubscribe(channel)
|
18
|
+
@dispatcher.unsubscribe(@ws, channel)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def allowed_methods
|
24
|
+
procedures + %w(ping subscribe unsubscribe)
|
25
|
+
end
|
26
|
+
|
27
|
+
def procedures
|
28
|
+
[]
|
29
|
+
end
|
30
|
+
end
|
data/lib/jpc/helpers.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module JPC
|
2
|
+
module Helpers
|
3
|
+
def make_message(data)
|
4
|
+
message = { jsonrpc: JPC::RPC_VERSION }.merge!(data)
|
5
|
+
Oj.dump(message, mode: :compat)
|
6
|
+
end
|
7
|
+
|
8
|
+
def make_response(id, data)
|
9
|
+
response = { id: id }.merge!(data)
|
10
|
+
make_message(response)
|
11
|
+
end
|
12
|
+
|
13
|
+
def make_result(id, result)
|
14
|
+
make_response(id, result: result)
|
15
|
+
end
|
16
|
+
|
17
|
+
def make_error(id, code, message)
|
18
|
+
error = {
|
19
|
+
code: code,
|
20
|
+
message: message
|
21
|
+
}
|
22
|
+
|
23
|
+
make_response(id, error: error)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/jpc/invoker.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
class JPC::Invoker
|
2
|
+
include JPC::Helpers
|
3
|
+
|
4
|
+
def initialize(handler)
|
5
|
+
@handler = handler
|
6
|
+
end
|
7
|
+
|
8
|
+
def invoke(json)
|
9
|
+
request = Oj.load(json)
|
10
|
+
response = execute(request)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def execute(request)
|
16
|
+
method = request['method']
|
17
|
+
|
18
|
+
fail "Method #{method} not allowed" unless method_allowed?(method)
|
19
|
+
|
20
|
+
@handler.token = request['token'] if request['token']
|
21
|
+
|
22
|
+
if @handler.respond_to?(:authorized?)
|
23
|
+
fail JPC::Errors::UnauthorizedError unless @handler.authorized?(method)
|
24
|
+
end
|
25
|
+
|
26
|
+
if request['params'].is_a?(Array)
|
27
|
+
result = @handler.public_send(method, *request['params'])
|
28
|
+
elsif %w(Hash String Integer).include?(request['params'].class.name)
|
29
|
+
result = @handler.public_send(method, request['params'])
|
30
|
+
else
|
31
|
+
result = @handler.public_send(method)
|
32
|
+
end
|
33
|
+
|
34
|
+
make_result(request['id'], result)
|
35
|
+
rescue => e
|
36
|
+
case e.class.name
|
37
|
+
when 'JPC::Errors::UnauthorizedError'
|
38
|
+
code = 32001
|
39
|
+
else
|
40
|
+
code = 32000
|
41
|
+
end
|
42
|
+
|
43
|
+
make_error(
|
44
|
+
request['id'],
|
45
|
+
code,
|
46
|
+
"Method #{method}: #{e.message}. See #{e.backtrace[0]}"
|
47
|
+
)
|
48
|
+
end
|
49
|
+
|
50
|
+
def method_allowed?(name)
|
51
|
+
@handler.send(:allowed_methods).include?(name.to_s)
|
52
|
+
end
|
53
|
+
end
|
data/lib/jpc/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,117 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jpc
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Denis Lysenko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-09-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.11'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.11'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: oj
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 2.0.0
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 2.0.0
|
69
|
+
description: This library provide possibility to handle JSON requests.
|
70
|
+
email:
|
71
|
+
- d@lysenkooo.ru
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- ".rspec"
|
78
|
+
- ".travis.yml"
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- bin/console
|
84
|
+
- bin/setup
|
85
|
+
- jpc.gemspec
|
86
|
+
- lib/jpc.rb
|
87
|
+
- lib/jpc/dispatcher.rb
|
88
|
+
- lib/jpc/errors.rb
|
89
|
+
- lib/jpc/handler.rb
|
90
|
+
- lib/jpc/helpers.rb
|
91
|
+
- lib/jpc/invoker.rb
|
92
|
+
- lib/jpc/version.rb
|
93
|
+
homepage: http://lysenkooo.ru
|
94
|
+
licenses:
|
95
|
+
- MIT
|
96
|
+
metadata: {}
|
97
|
+
post_install_message:
|
98
|
+
rdoc_options: []
|
99
|
+
require_paths:
|
100
|
+
- lib
|
101
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - ">="
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
requirements: []
|
112
|
+
rubyforge_project:
|
113
|
+
rubygems_version: 2.6.6
|
114
|
+
signing_key:
|
115
|
+
specification_version: 4
|
116
|
+
summary: JPC is JSON-RPC 2.0 Ruby Library.
|
117
|
+
test_files: []
|