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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 731fbeef55a0b7a9bacbf22b09caabd6599f66032ac24e34d834889f7c810ef3
4
- data.tar.gz: 841090dce51a1285eb0f2e63e17c3f5f6c868c211caef620f7a91e2991bf267b
3
+ metadata.gz: e424564ef40dfcc5c6a1e802a8e7767a7111f5de76ebd52b7f91581f61bae195
4
+ data.tar.gz: 2d27258a00946665bd4795f124c3eb63f6ce59ed5056d3b0396c422d611c22e8
5
5
  SHA512:
6
- metadata.gz: 9c30c2aa2c713da842b21d1ce591ed350856f27bad5c154f1a60b1bfa223c4e4bd4d32ec25f2de30e9a21dcd1d52e9d05b6368dc4161f42e47185ee444c8fdbe
7
- data.tar.gz: 0fdc8cc5a86773f43835223976941e90e8222c86191bb18110380e18f0fe3a83ec594e29d6c99024a4da13f9d511e447ef2422e56a037e3ef5928cdf12fe4a54
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 `around`, `before` or `after` hooks to tie into the controller
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 need to pass something that responds to the corresponding
87
- method to the appropriate setting in the config, e.g.
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.around_action = Module.new do
92
- def self.around(controller) # method name matches the hook type
93
- puts controller.request.remote_ip
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 ActiveRpc.config.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 ActiveRpc.config.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(id: item['id'], method: item['method'], params: item['params'])
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 => ex
43
- res.error = Errors::ClientError.new(message: ex.to_s)
44
- rescue NoMethodError => ex
45
- res.error = Errors::NoMethodError.new(message: ex.to_s)
46
- rescue ArgumentError => ex
47
- res.error = Errors::ArgumentError.new(message: ex.to_s)
48
- rescue OperationFailure => ex
49
- res.error = ex.rpc_error
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 => ex
52
- res.error = Errors::InternalError.new(message: ex.to_s)
52
+ rescue => e
53
+ res.error = Errors::InternalError.new(message: e.to_s)
53
54
  end
54
55
 
55
56
  res
@@ -1,19 +1,30 @@
1
1
  module ActiveRpc
2
2
  class Request
3
- attr_reader :id, :method, :params
3
+ include ActiveModel::Model
4
+ include ActiveModel::Validations
4
5
 
5
- def initialize(id:, method:, params:)
6
- @id, @method, @params = id, method, params
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
- def valid?
10
- [
11
- id.present?,
12
- id.is_a?(String) || id.is_a?(Integer),
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
@@ -1,3 +1,3 @@
1
1
  module ActiveRpc
2
- VERSION = '0.1.1'
2
+ VERSION = '0.2.0'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - John Maxwell