saorin 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bc3152fea207123c2b0babcc3e7b09067eaf63c4
4
- data.tar.gz: a42b9af27a59e05304e70e39db95cd671ba1702e
3
+ metadata.gz: 9790f4170f9f9740a494ce87226a6f67d0e1a613
4
+ data.tar.gz: 4bf5efd3af1127f67e420a69e2ecd1587474d102
5
5
  SHA512:
6
- metadata.gz: b04131232693dd6d87c73a5a844fb88ee407ca60579af48d17af751726e813a0b82e47815397e51f338e5579cc6576400ea0f914a6ebfa4968d5ac22b3993014
7
- data.tar.gz: 16b6b406fe99842586234e1473ac11c66eb8dfe89367aa34f107a7c9f6584ad2b1a305a060e73ef6ffed8f6ab7446d97ca7df966d6efab31943665f481bf5f9c
6
+ metadata.gz: bcef4861617f4ceeeeddcc0aa41d373d98a897b125de545a8835ea4d0db8565cc289bda0125272a07d9cfc14fac28e0b3dffba430c83b24799565c405ef8bcaa
7
+ data.tar.gz: ffdc00163550c32c3a39102e1ad9299da7a4d5a4cfcdebda4ad7ddfdf2a4a729135d6a8ce0ab8e6ec6dd9d74633a167db7381b18aed131739107a08e54edcbda
@@ -0,0 +1,18 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 1.9.3
5
+ - 2.0.0
6
+ - 2.1.1
7
+
8
+ branches:
9
+ only:
10
+ - master
11
+
12
+ notifications:
13
+ irc:
14
+ channels:
15
+ - "irc.freenode.net#mashiro-dev"
16
+ use_notice: true
17
+ skip_join: true
18
+
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Saorin
1
+ # Saorin [![Build Status](https://travis-ci.org/mashiro/saorin.png?branch=master)](https://travis-ci.org/mashiro/saorin)
2
2
 
3
3
  JSON-RPC server and client library.
4
4
 
@@ -8,7 +8,7 @@ module Saorin
8
8
 
9
9
  class << self
10
10
  def new(options = {}, &block)
11
- adapter = options.delete(:adapter) || :faraday
11
+ adapter = options[:adapter] || :faraday
12
12
  adapter_class = guess adapter
13
13
  adapter_class.new options, &block
14
14
  end
@@ -7,6 +7,14 @@ require 'saorin/client'
7
7
  module Saorin
8
8
  module Client
9
9
  module Base
10
+ module UUID
11
+ def uuid
12
+ require 'securerandom'
13
+ SecureRandom.uuid
14
+ end
15
+ end
16
+
17
+ include UUID
10
18
  include Formatter
11
19
 
12
20
  attr_reader :options
@@ -30,6 +38,33 @@ module Saorin
30
38
  content
31
39
  end
32
40
 
41
+ class Batch < ::Array
42
+ include UUID
43
+ attr_reader :client
44
+
45
+ def initialize(client)
46
+ super()
47
+ @client = client
48
+ end
49
+
50
+ def call(method, *args)
51
+ push Saorin::Request.new(method, args, :id => uuid)
52
+ end
53
+
54
+ def notify(method, *args)
55
+ push Saorin::Request.new(method, args)
56
+ end
57
+
58
+ def apply
59
+ return [] if empty?
60
+ @client.apply(self) || []
61
+ end
62
+ end
63
+
64
+ def batch
65
+ Batch.new self
66
+ end
67
+
33
68
  protected
34
69
 
35
70
  def send_request(content)
@@ -48,6 +83,7 @@ module Saorin
48
83
  end
49
84
 
50
85
  def parse_response(content)
86
+ return nil if content.nil? || content.empty?
51
87
  formatter.load content
52
88
  end
53
89
 
@@ -73,11 +109,6 @@ module Saorin
73
109
  response.validate
74
110
  to_content response
75
111
  end
76
-
77
- def uuid
78
- require 'securerandom'
79
- SecureRandom.uuid
80
- end
81
112
  end
82
113
  end
83
114
  end
@@ -58,13 +58,19 @@ module Saorin
58
58
  end
59
59
  end
60
60
 
61
+ class UserDefinedError < RPCError
62
+ end
63
+
61
64
  class << self
62
65
  def code_to_error(code)
63
- if (-32099..-32000).include?(code)
66
+ case code
67
+ when nil
68
+ nil
69
+ when (-32099..-32000)
64
70
  ServerError
65
71
  else
66
- @map ||= Hash[JSON_RPC_ERRORS.map { |c, n, m| [c, const_get(n)] }]
67
- @map[code]
72
+ @error_code_map ||= Hash[JSON_RPC_ERRORS.map { |c, n, m| [c, const_get(n)] }]
73
+ @error_code_map[code] || UserDefinedError
68
74
  end
69
75
  end
70
76
  end
@@ -8,7 +8,7 @@ module Saorin
8
8
 
9
9
  class << self
10
10
  def new(handler, options = {})
11
- adapter = options.delete(:adapter) || :rack
11
+ adapter = options[:adapter] || :rack
12
12
  adapter_class = guess adapter
13
13
  adapter_class.new handler, options
14
14
  end
@@ -87,6 +87,8 @@ module Saorin
87
87
  else
88
88
  @handler.__send__ method, *params
89
89
  end
90
+ rescue Saorin::RPCError
91
+ raise
90
92
  rescue ArgumentError
91
93
  raise Saorin::InvalidParams
92
94
  rescue Exception => e
@@ -78,6 +78,10 @@ module Saorin
78
78
  @client.notify *args
79
79
  end
80
80
 
81
+ def test_batch
82
+ @client.batch
83
+ end
84
+
81
85
  shared_context 'setup rpc server client' do |options|
82
86
  before(:all) do
83
87
  create_test_server options[:server] || {}
@@ -89,37 +93,102 @@ module Saorin
89
93
  end
90
94
 
91
95
  shared_examples 'rpc communicatable' do
92
- it 'string' do
93
- value = '123'
94
- test_call('identity', value).should eq value
95
- end
96
-
97
- it 'number' do
98
- value = 123
99
- test_call('identity', value).should eq value
100
- end
101
-
102
- it 'array' do
103
- value = [1, 2, 3]
104
- test_call('identity', value).should eq value
105
- end
106
-
107
- it 'hash' do
108
- value = {'foo' => 1, 'bar' => 2}
109
- test_call('identity', value).should eq value
110
- end
111
-
112
- it 'nil' do
113
- value = nil
114
- test_call('identity', value).should eq value
115
- end
116
-
117
- it 'not found' do
118
- lambda { test_call('xxx') }.should raise_error MethodNotFound
119
- end
120
-
121
- it 'invalid params' do
122
- lambda { test_call('identity', 1, 2, 3, 4, 5) }.should raise_error InvalidParams
96
+ describe '#call' do
97
+ it 'string' do
98
+ v = '123'
99
+ expect(test_call('identity', v)).to eq v
100
+ end
101
+
102
+ it 'number' do
103
+ v = 123
104
+ expect(test_call('identity', v)).to eq v
105
+ end
106
+
107
+ it 'array' do
108
+ v = [1, 2, 3]
109
+ expect(test_call('identity', v)).to eq v
110
+ end
111
+
112
+ it 'hash' do
113
+ v = {'foo' => 1, 'bar' => 2}
114
+ expect(test_call('identity', v)).to eq v
115
+ end
116
+
117
+ it 'nil' do
118
+ v = nil
119
+ expect(test_call('identity', v)).to eq v
120
+ end
121
+
122
+ it 'not found' do
123
+ expect { test_call('xx') }.to raise_error MethodNotFound
124
+ end
125
+
126
+ it 'invalid params' do
127
+ expect { test_call('identity', 1, 2, 3, 4, 5) }.to raise_error InvalidParams
128
+ end
129
+ end
130
+
131
+ describe '#notify' do
132
+ it 'string' do
133
+ v = '123'
134
+ expect(test_notify('identity', v)).to eq nil
135
+ end
136
+
137
+ it 'not found' do
138
+ expect(test_notify('xxx')).to eq nil
139
+ end
140
+
141
+ it 'invalid params' do
142
+ expect(test_notify('identity', 1, 2, 3, 4, 5)).to eq nil
143
+ end
144
+ end
145
+
146
+ describe '#batch' do
147
+ it 'batch call' do
148
+ b = test_batch
149
+ b.call 'identity', 1
150
+ b.call 'identity', '2'
151
+ b.call 'identity', [3]
152
+ b.call 'xxx'
153
+
154
+ ret = b.apply
155
+ expect(ret).to have(4).items
156
+ expect(ret[0]).to eq 1
157
+ expect(ret[1]).to eq '2'
158
+ expect(ret[2]).to eq [3]
159
+ expect(ret[3]).to be_instance_of MethodNotFound
160
+ end
161
+
162
+ it 'batch call with notify' do
163
+ b = test_batch
164
+ b.call 'identity', 1
165
+ b.notify 'identity', '2'
166
+ b.call 'identity', [3]
167
+ b.notify 'xxx'
168
+
169
+ ret = b.apply
170
+ expect(ret).to have(2).items
171
+ expect(ret[0]).to eq 1
172
+ expect(ret[1]).to eq [3]
173
+ end
174
+
175
+ it 'all notify' do
176
+ b = test_batch
177
+ b.notify 'identity', 1
178
+ b.notify 'identity', '2'
179
+ b.notify 'identity', [3]
180
+ b.notify 'xxx'
181
+
182
+ ret = b.apply
183
+ expect(ret).to have(0).items
184
+ end
185
+
186
+ it 'empty' do
187
+ b = test_batch
188
+
189
+ ret = b.apply
190
+ expect(ret).to have(0).items
191
+ end
123
192
  end
124
193
  end
125
194
  end
@@ -1,3 +1,3 @@
1
1
  module Saorin
2
- VERSION = '0.5.0'
2
+ VERSION = '0.6.0'
3
3
  end
@@ -19,8 +19,8 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ['lib']
20
20
 
21
21
  spec.add_dependency 'multi_json'
22
- spec.add_development_dependency 'rake'
23
- spec.add_development_dependency 'rspec'
24
- spec.add_development_dependency 'rack'
25
- spec.add_development_dependency 'faraday'
22
+ spec.add_development_dependency 'rake', '~> 10.4.2'
23
+ spec.add_development_dependency 'rspec', '~> 2.14.1'
24
+ spec.add_development_dependency 'rack', '~> 1.5.2'
25
+ spec.add_development_dependency 'faraday', '~> 0.9.0'
26
26
  end
@@ -58,7 +58,7 @@ describe Saorin::Request do
58
58
  it do
59
59
  r = create_request
60
60
  r.stub(:valid?).and_return(true)
61
- lambda { r.validate }.should_not raise_error Saorin::InvalidRequest
61
+ lambda { r.validate }.should_not raise_error
62
62
  end
63
63
  end
64
64
 
@@ -82,7 +82,7 @@ describe Saorin::Response do
82
82
  it do
83
83
  r = create_response
84
84
  r.stub(:valid?).and_return(true)
85
- lambda { r.validate }.should_not raise_error Saorin::InvalidResponse
85
+ lambda { r.validate }.should_not raise_error
86
86
  end
87
87
  end
88
88
 
@@ -9,6 +9,9 @@ RSpec.configure do |config|
9
9
  config.treat_symbols_as_metadata_keys_with_true_values = true
10
10
  config.run_all_when_everything_filtered = true
11
11
  config.filter_run :focus
12
+ config.expect_with :rspec do |c|
13
+ c.syntax = [:should, :expect]
14
+ end
12
15
 
13
16
  # Run specs in random order to surface order dependencies. If you find an
14
17
  # order dependency and want to debug it, you can fix the order by providing
metadata CHANGED
@@ -1,85 +1,85 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: saorin
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - mashiro
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-05-09 00:00:00.000000000 Z
11
+ date: 2014-12-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: multi_json
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '0'
33
+ version: 10.4.2
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '0'
40
+ version: 10.4.2
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: 2.14.1
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: 2.14.1
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - "~>"
60
60
  - !ruby/object:Gem::Version
61
- version: '0'
61
+ version: 1.5.2
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - "~>"
67
67
  - !ruby/object:Gem::Version
68
- version: '0'
68
+ version: 1.5.2
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: faraday
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - '>='
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: 0.9.0
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - '>='
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: 0.9.0
83
83
  description: JSON-RPC 2.0 implementation
84
84
  email:
85
85
  - mail@mashiro.org
@@ -87,8 +87,9 @@ executables: []
87
87
  extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
- - .gitignore
91
- - .rspec
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
92
93
  - Gemfile
93
94
  - LICENSE.txt
94
95
  - README.md
@@ -110,10 +111,10 @@ files:
110
111
  - lib/saorin/utility.rb
111
112
  - lib/saorin/version.rb
112
113
  - saorin.gemspec
113
- - spec/request_spec.rb
114
- - spec/response_spec.rb
115
- - spec/server/base_spec.rb
116
- - spec/server/rack_spec.rb
114
+ - spec/saorin/request_spec.rb
115
+ - spec/saorin/response_spec.rb
116
+ - spec/saorin/server/base_spec.rb
117
+ - spec/saorin/server/rack_spec.rb
117
118
  - spec/spec_helper.rb
118
119
  - spec/support/response.rb
119
120
  - spec/support/utils.rb
@@ -127,25 +128,25 @@ require_paths:
127
128
  - lib
128
129
  required_ruby_version: !ruby/object:Gem::Requirement
129
130
  requirements:
130
- - - '>='
131
+ - - ">="
131
132
  - !ruby/object:Gem::Version
132
133
  version: '0'
133
134
  required_rubygems_version: !ruby/object:Gem::Requirement
134
135
  requirements:
135
- - - '>='
136
+ - - ">="
136
137
  - !ruby/object:Gem::Version
137
138
  version: '0'
138
139
  requirements: []
139
140
  rubyforge_project:
140
- rubygems_version: 2.0.0
141
+ rubygems_version: 2.2.2
141
142
  signing_key:
142
143
  specification_version: 4
143
144
  summary: JSON-RPC 2.0 server and client implementation for any protocols
144
145
  test_files:
145
- - spec/request_spec.rb
146
- - spec/response_spec.rb
147
- - spec/server/base_spec.rb
148
- - spec/server/rack_spec.rb
146
+ - spec/saorin/request_spec.rb
147
+ - spec/saorin/response_spec.rb
148
+ - spec/saorin/server/base_spec.rb
149
+ - spec/saorin/server/rack_spec.rb
149
150
  - spec/spec_helper.rb
150
151
  - spec/support/response.rb
151
152
  - spec/support/utils.rb