activerpc 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +6 -9
- data/app/controllers/active_rpc/rpc_controller.rb +17 -16
- data/lib/active_rpc/request.rb +22 -11
- data/lib/active_rpc/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e424564ef40dfcc5c6a1e802a8e7767a7111f5de76ebd52b7f91581f61bae195
|
4
|
+
data.tar.gz: 2d27258a00946665bd4795f124c3eb63f6ce59ed5056d3b0396c422d611c22e8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1f62f60e31e8ae1e26922693d3666f4f48ebac57f1eaf4a02dc96b9254f8636ae926addf08edef1ee67759c4344510e64d23b3454ac4fdf1a64be1886537898a
|
7
|
+
data.tar.gz: '040801f682a44861de03fbaa779f5d223c21de825e0581a328d114b22f651c14d92fe523c995a8966a085d2b4402e9e198b08ebfb4469cbcb0cc17a14a9d43bd'
|
data/README.md
CHANGED
@@ -80,20 +80,17 @@ end
|
|
80
80
|
```
|
81
81
|
|
82
82
|
#### Controller Lifecycle Events
|
83
|
-
You can provide `
|
83
|
+
You can provide `after` or `before` hooks to tie into the controller
|
84
84
|
lifecycle, whether for authentication, authorization or whatever else.
|
85
85
|
|
86
|
-
To provide a hook, you
|
87
|
-
|
86
|
+
To provide a hook, you can provide a callable for the method desired to the config
|
87
|
+
object.
|
88
88
|
|
89
89
|
```ruby
|
90
90
|
ActiveRpc.configure do |config|
|
91
|
-
config.
|
92
|
-
|
93
|
-
|
94
|
-
yield
|
95
|
-
end
|
96
|
-
end
|
91
|
+
config.before_action = ->{
|
92
|
+
authenticate_user!
|
93
|
+
}
|
97
94
|
end
|
98
95
|
```
|
99
96
|
|
@@ -1,13 +1,11 @@
|
|
1
1
|
module ActiveRpc
|
2
2
|
class RpcController < ActionController::Base
|
3
3
|
if ActiveRpc.config.after_action.present?
|
4
|
-
after_action
|
5
|
-
end
|
6
|
-
if ActiveRpc.config.around_action.present?
|
7
|
-
around_action ActiveRpc.config.around_action
|
4
|
+
after_action(&ActiveRpc.config.after_action)
|
8
5
|
end
|
6
|
+
|
9
7
|
if ActiveRpc.config.before_action.present?
|
10
|
-
before_action
|
8
|
+
before_action(&ActiveRpc.config.before_action)
|
11
9
|
end
|
12
10
|
|
13
11
|
def create
|
@@ -27,8 +25,9 @@ module ActiveRpc
|
|
27
25
|
end
|
28
26
|
|
29
27
|
private def process_item(item)
|
30
|
-
req = ActiveRpc::Request.new(
|
28
|
+
req = ActiveRpc::Request.new(item.slice('id', 'method', 'params'))
|
31
29
|
res = ActiveRpc::Response.from_request(req)
|
30
|
+
ex = nil
|
32
31
|
|
33
32
|
begin
|
34
33
|
raise TypeError, 'invalid JSON-RPC request' unless req.valid?
|
@@ -39,17 +38,19 @@ module ActiveRpc
|
|
39
38
|
ex = executor.new(req.params)
|
40
39
|
raise ArgumentError, 'invalid payload' unless ex.valid?
|
41
40
|
res.result = ex.call
|
42
|
-
rescue TypeError =>
|
43
|
-
res.error = Errors::ClientError.new(message:
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
res.error = ex.
|
41
|
+
rescue TypeError => e
|
42
|
+
res.error = Errors::ClientError.new(message: e.to_s)
|
43
|
+
res.error.data = req.errors
|
44
|
+
rescue NoMethodError => e
|
45
|
+
res.error = Errors::NoMethodError.new(message: e.to_s)
|
46
|
+
rescue ArgumentError => e
|
47
|
+
res.error = Errors::ArgumentError.new(message: e.to_s)
|
48
|
+
res.error.data = ex.errors
|
49
|
+
rescue OperationFailure => e
|
50
|
+
res.error = e.rpc_error
|
50
51
|
# todo: handle rpc-level errors
|
51
|
-
rescue =>
|
52
|
-
res.error = Errors::InternalError.new(message:
|
52
|
+
rescue => e
|
53
|
+
res.error = Errors::InternalError.new(message: e.to_s)
|
53
54
|
end
|
54
55
|
|
55
56
|
res
|
data/lib/active_rpc/request.rb
CHANGED
@@ -1,19 +1,30 @@
|
|
1
1
|
module ActiveRpc
|
2
2
|
class Request
|
3
|
-
|
3
|
+
include ActiveModel::Model
|
4
|
+
include ActiveModel::Validations
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
attr_accessor :id, :method, :params
|
7
|
+
|
8
|
+
validates_each :id do |record, field, value|
|
9
|
+
unless value.is_a?(String) || value.is_a?(Integer)
|
10
|
+
record.errors.add(field, %(illegal type - received #{value.class}))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
validates_each :method do |record, field, value|
|
15
|
+
unless value.is_a?(String)
|
16
|
+
record.errors.add(field, %(illegal type - received #{value.class}))
|
17
|
+
end
|
18
|
+
|
19
|
+
unless value.present?
|
20
|
+
record.errors.add(field, 'must not be empty')
|
21
|
+
end
|
7
22
|
end
|
8
23
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
method.present?,
|
14
|
-
method.is_a?(String),
|
15
|
-
params.nil? || params.is_a?(Array) || params.is_a?(Hash),
|
16
|
-
].all?
|
24
|
+
validates_each :params do |record, field, value|
|
25
|
+
unless value.nil? || value.is_a?(Array) || value.is_a?(Hash)
|
26
|
+
record.errors.add(field, %(invalid type - received #{value.class}))
|
27
|
+
end
|
17
28
|
end
|
18
29
|
end
|
19
30
|
end
|
data/lib/active_rpc/version.rb
CHANGED