jpc 1.2.0 → 1.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 34736193a4d8924161740400cdcfcc5bd595ddc3
4
- data.tar.gz: 4b22af972d7bd5a457762bb54cf089aac617f084
3
+ metadata.gz: 9838964a3c402a026bdd5a0058d94f9838c202c0
4
+ data.tar.gz: f89c1cac62eeddb98c83e1dd7c7a4f129da8077a
5
5
  SHA512:
6
- metadata.gz: a53734a7c53d9eb556d6b3d67e975291185bec5fca033d478b4521dce84104fa69f59fb1259aab22149da877e745fca5a3ef5878685f9e289fde6fd115b0a02c
7
- data.tar.gz: bf4c3f98c74774df37d5c2df2369c681895604694ca56c77c8fff04e534356d89408146e3014998a4e0b81a637a1f51ff15381cdb2dbfd0232fb0eca669ad36d
6
+ metadata.gz: 38787ea9e618b415f140c52d51003c1df4e4a1d92f932cd888c676eb5aa9cef072cb6b8e3ed99533c0dffb7e2620b2a14c38f46b59023acb99885412dc1715de
7
+ data.tar.gz: 32175cd39b99bb034d6c4bd8e9d288a75869aabe198c90b28f6853f5fca34fb479dd56fd1b10c293e6a3fadbf78c6cc856e175a70eaef341804f6a5ae40a6c42
data/.rspec CHANGED
@@ -1,3 +1,4 @@
1
1
  --format documentation
2
2
  --color
3
3
  --order random
4
+ --require spec_helper
data/CONTRIBUTING.md ADDED
@@ -0,0 +1,5 @@
1
+ # How to contribute
2
+
3
+ If you want to contribute you are welcome!
4
+
5
+ Just make your changes and create pull request.
data/Rakefile CHANGED
@@ -1,6 +1,6 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
3
 
4
4
  RSpec::Core::RakeTask.new(:spec)
5
5
 
6
- task :default => :spec
6
+ task default: :spec
data/bin/console CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require "bundler/setup"
4
- require "jpc"
3
+ require 'bundler/setup'
4
+ require 'jpc'
5
5
 
6
6
  # You can add fixtures and/or initialization code here to make experimenting
7
7
  # with your gem easier. You can also use a different console, if you like.
@@ -10,5 +10,5 @@ require "jpc"
10
10
  # require "pry"
11
11
  # Pry.start
12
12
 
13
- require "irb"
13
+ require 'irb'
14
14
  IRB.start
data/jpc.gemspec CHANGED
@@ -4,22 +4,22 @@ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
4
  require 'jpc/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
- spec.name = "jpc"
7
+ spec.name = 'jpc'
8
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 provides possibility to handle JSON requests.}
13
- spec.homepage = "http://lysenkooo.ru"
14
- spec.license = "MIT"
9
+ spec.authors = ['Denis Lysenko']
10
+ spec.email = ['d@lysenkooo.ru']
11
+ spec.summary = 'JPC is JSON-RPC 2.0 Ruby Library.'
12
+ spec.description = 'This library provides possibility to handle JSON requests.'
13
+ spec.homepage = 'http://lysenkooo.ru'
14
+ spec.license = 'MIT'
15
15
 
16
16
  spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
- spec.bindir = "exe"
17
+ spec.bindir = 'exe'
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
- spec.require_paths = ["lib"]
19
+ spec.require_paths = ['lib']
20
20
 
21
- spec.add_runtime_dependency "oj", ">= 2.0.0"
22
- spec.add_development_dependency "bundler", "~> 1.11"
23
- spec.add_development_dependency "rake", "~> 10.0"
24
- spec.add_development_dependency "rspec", "~> 3.0"
21
+ spec.add_runtime_dependency 'oj', '>= 2.0.0'
22
+ spec.add_development_dependency 'bundler', '~> 1.11'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
25
  end
data/lib/jpc.rb CHANGED
@@ -1,10 +1,10 @@
1
- require "oj"
2
- require "jpc/version"
3
- require "jpc/errors"
4
- require "jpc/helpers"
5
- require "jpc/invoker"
6
- require "jpc/handler"
7
- require "jpc/dispatcher"
1
+ require 'oj'
2
+ require 'jpc/version'
3
+ require 'jpc/errors'
4
+ require 'jpc/helpers'
5
+ require 'jpc/invoker'
6
+ require 'jpc/handler'
7
+ require 'jpc/dispatcher'
8
8
 
9
9
  module JPC
10
10
  RPC_VERSION = '2.0'.freeze
@@ -1,37 +1,41 @@
1
- class JPC::Dispatcher
2
- include JPC::Helpers
1
+ module JPC
2
+ class Dispatcher
3
+ include JPC::Helpers
3
4
 
4
- def subscribe(ws, channel)
5
- channels[channel.to_sym] ||= []
6
- channels[channel.to_sym] << ws
5
+ def subscribe(ws, channel)
6
+ channels[channel.to_sym] ||= []
7
+ channels[channel.to_sym].push(ws) unless channels[channel.to_sym].include?(ws)
7
8
 
8
- { channel: channel, status: 'subscribed' }
9
- end
9
+ { channel: channel, status: 'subscribed' }
10
+ end
10
11
 
11
- def unsubscribe(ws, channel)
12
- fail "Channel #{channel} not found" unless channels[channel.to_sym]
12
+ def unsubscribe(ws, channel)
13
+ raise "Channel #{channel} not found" unless channels[channel.to_sym]
13
14
 
14
- channels[channel.to_sym] -= [ws]
15
+ channels[channel.to_sym].each_with_index do |object, index|
16
+ channels[channel.to_sym].delete_at(index) if object == ws
17
+ end
15
18
 
16
- { channel: channel, status: 'unsubscribed' }
17
- end
19
+ { channel: channel, status: 'unsubscribed' }
20
+ end
18
21
 
19
- def cast(channel, payload)
20
- message = make_message(channel: channel, payload: payload)
22
+ def cast(channel, payload)
23
+ message = make_message(channel: channel, payload: payload)
21
24
 
22
- sent = 0
25
+ sent = 0
23
26
 
24
- channels[channel.to_sym] && channels[channel.to_sym].each do |ws|
25
- ws.send(message)
26
- sent += 1
27
- end
27
+ channels[channel.to_sym] && channels[channel.to_sym].each do |ws|
28
+ ws.send(message)
29
+ sent += 1
30
+ end
28
31
 
29
- { channel: channel, sent: sent }
30
- end
32
+ { channel: channel, sent: sent }
33
+ end
31
34
 
32
- private
35
+ private
33
36
 
34
- def channels
35
- @channels ||= {}
37
+ def channels
38
+ @channels ||= {}
39
+ end
36
40
  end
37
41
  end
data/lib/jpc/errors.rb CHANGED
@@ -2,7 +2,7 @@ module JPC
2
2
  module Errors
3
3
  class UnauthorizedError < RuntimeError
4
4
  def message
5
- "Unauthorized Request"
5
+ 'Unauthorized Request'
6
6
  end
7
7
  end
8
8
  end
data/lib/jpc/handler.rb CHANGED
@@ -1,30 +1,32 @@
1
- class JPC::Handler
2
- attr_accessor :token
1
+ module JPC
2
+ class Handler
3
+ attr_accessor :token
3
4
 
4
- def initialize(ws = nil, dispatcher = nil)
5
- @ws = ws
6
- @dispatcher = dispatcher
7
- end
5
+ def initialize(ws, dispatcher)
6
+ @ws = ws
7
+ @dispatcher = dispatcher
8
+ end
8
9
 
9
- def ping(params = {})
10
- "pong #{params}"
11
- end
10
+ def ping(params = {})
11
+ "pong #{params}"
12
+ end
12
13
 
13
- def subscribe(channel)
14
- @dispatcher.subscribe(@ws, channel)
15
- end
14
+ def subscribe(channel)
15
+ @dispatcher.subscribe(@ws, channel)
16
+ end
16
17
 
17
- def unsubscribe(channel)
18
- @dispatcher.unsubscribe(@ws, channel)
19
- end
18
+ def unsubscribe(channel)
19
+ @dispatcher.unsubscribe(@ws, channel)
20
+ end
20
21
 
21
- private
22
+ private
22
23
 
23
- def allowed_methods
24
- procedures + %w(ping subscribe unsubscribe)
25
- end
24
+ def allowed_methods
25
+ procedures + %w(ping subscribe unsubscribe)
26
+ end
26
27
 
27
- def procedures
28
- []
28
+ def procedures
29
+ []
30
+ end
29
31
  end
30
32
  end
data/lib/jpc/invoker.rb CHANGED
@@ -1,53 +1,55 @@
1
- class JPC::Invoker
2
- include JPC::Helpers
1
+ module JPC
2
+ class Invoker
3
+ include JPC::Helpers
3
4
 
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)
5
+ def initialize(handler)
6
+ @handler = handler
24
7
  end
25
8
 
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)
9
+ def invoke(json)
10
+ request = Oj.load(json)
11
+ execute(request)
32
12
  end
33
13
 
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
14
+ private
15
+
16
+ def execute(request)
17
+ method = request['method']
18
+
19
+ raise "Method #{method} not allowed" unless method_allowed?(method)
20
+
21
+ @handler.token = request['token'] if request['token']
22
+
23
+ if @handler.respond_to?(:authorized?)
24
+ raise JPC::Errors::UnauthorizedError unless @handler.authorized?(method)
25
+ end
26
+
27
+ result = if request['params'].is_a?(Array)
28
+ @handler.public_send(method, *request['params'])
29
+ elsif %w(Hash String Integer).include?(request['params'].class.name)
30
+ @handler.public_send(method, request['params'])
31
+ else
32
+ @handler.public_send(method)
33
+ end
34
+
35
+ make_result(request['id'], result)
36
+ rescue => e
37
+ code = case e.class.name
38
+ when 'JPC::Errors::UnauthorizedError'
39
+ 32001
40
+ else
41
+ 32000
42
+ end
43
+
44
+ make_error(
45
+ request['id'],
46
+ code,
47
+ "Method #{method}: #{e.message}. See #{e.backtrace[0]}"
48
+ )
41
49
  end
42
50
 
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)
51
+ def method_allowed?(name)
52
+ @handler.send(:allowed_methods).include?(name.to_s)
53
+ end
52
54
  end
53
55
  end
data/lib/jpc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module JPC
2
- VERSION = "1.2.0"
2
+ VERSION = '1.2.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.0
4
+ version: 1.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denis Lysenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-09 00:00:00.000000000 Z
11
+ date: 2016-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: oj
@@ -76,6 +76,7 @@ files:
76
76
  - ".gitignore"
77
77
  - ".rspec"
78
78
  - ".travis.yml"
79
+ - CONTRIBUTING.md
79
80
  - Gemfile
80
81
  - LICENSE.txt
81
82
  - README.md