jsonrpc2 0.2.0 → 0.3.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: 6656e3e753bbf3ae9c6d64fc70f1f98ecabe3f8854651d9d4574f6d1b4a3a6c2
4
- data.tar.gz: 9ecd6c561d1c037f755331d8ca9c968f085e76479853dccbd239e89cb5e76b84
3
+ metadata.gz: 38157bffab895f13838433b4564add0f49d5823a9220eafc0b8d9e402bad7931
4
+ data.tar.gz: 5ff1f88ca328078754a7fec05f386ab7308ddef82fbb4f8e357b4eceeef0d373
5
5
  SHA512:
6
- metadata.gz: 00fc45704df91c0a9b836c9ae7167a2117709e52275d47233c44e724b0f245de35ed29f445c2c4b383fc864f14511577f7e49a023d562e6ae753e29ca95114dd
7
- data.tar.gz: 76b803ba32848432dee6797b733ce75e775c151a9e1cf18047ce84dbe5aff6c8ea0d62bbb56e55b66f128e81a5522176db331a078b63247014b4270a00078c74
6
+ metadata.gz: b59d216b880ebeb4988573873090e9bd0807b77a8c7235ee28cfd84a6aeef219d5a7c7c9e16109a93b55d1857a01d3bf7e5ccc10f6bde4e62972199e05f2bccc
7
+ data.tar.gz: 507d80c0b89143a72c092dc994bc31d72977d5289dfb2c446d879af4b8790f0456457bc8d649a21cdcae9d632f2e8d3acb121c9f24999dcf276d39c18122a05f
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## [0.3.0] - 2023-02-09
2
+ ### Added
3
+ - Possibility to define callback for request calls prior to validation `JSONRPC2::Interface#before_validation`
4
+
1
5
  ## [0.2.0] - 2022-04-07
2
6
  ### Added
3
7
  - Possibility to define callback for server errors with `JSONRPC2::Interface#on_server_error`
@@ -239,6 +239,7 @@ module JSONRPC2
239
239
  # @param [Hash] params Method parameters
240
240
  # @return [Hash] JSON response
241
241
  def call(method, id, params)
242
+ invoke_before_validation_hook(method, id, params)
242
243
  if api_methods.include?(method)
243
244
  begin
244
245
  Types.valid_params?(self.class, method, params)
@@ -398,10 +399,19 @@ module JSONRPC2
398
399
  log_error("Server error hook failed - #{error.class}: #{error.message} #{error.backtrace.join("\n ")}")
399
400
  end
400
401
 
402
+ def invoke_before_validation_hook(method, id, params)
403
+ before_validation(method: method, id: id, params: params)
404
+ rescue => error
405
+ log_error("Before validation hook failed - #{error.class}: #{error.message} #{error.backtrace.join("\n ")}")
406
+ end
407
+
401
408
  # Available for reimplementation by a subclass, noop by default
402
409
  def on_server_error(request_id:, error:)
403
410
  end
404
411
 
412
+ def before_validation(method:, id:, params:)
413
+ end
414
+
405
415
  def log_error(message)
406
416
  logger.error("#{env['json.request-id']} #{message}") if logger.respond_to?(:error)
407
417
  end
@@ -1,5 +1,5 @@
1
1
  # JSONRPC2 namespace module
2
2
  module JSONRPC2
3
3
  # Version
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
@@ -118,6 +118,30 @@ RSpec.describe JSONRPC2::Interface do
118
118
  )
119
119
  end
120
120
  end
121
+
122
+ context 'with a correctly configured before_validation hook' do
123
+ before do
124
+ rpc_api_class.class_eval do
125
+ attr_reader :test_before_validation_data
126
+
127
+ def before_validation(method:, id:, params:)
128
+ @test_before_validation_data = {
129
+ method: method,
130
+ id: id,
131
+ params: params
132
+ }
133
+ end
134
+ end
135
+ end
136
+
137
+ it 'invokes the hook' do
138
+ dispatch_result
139
+
140
+ expect(instance.test_before_validation_data[:method]).to eq('hello')
141
+ expect(instance.test_before_validation_data[:id]).to eq(123)
142
+ expect(instance.test_before_validation_data[:params]).to eq({ 'name' => 'Geoff' })
143
+ end
144
+ end
121
145
  end
122
146
 
123
147
  context 'with an unhandled server error' do
@@ -131,6 +155,28 @@ RSpec.describe JSONRPC2::Interface do
131
155
  )
132
156
  end
133
157
 
158
+ context 'with a correctly configured error hook' do
159
+ before do
160
+ rpc_api_class.class_eval do
161
+ attr_reader :test_error_data
162
+
163
+ def on_server_error(request_id:, error:)
164
+ @test_error_data = {
165
+ request_id: request_id,
166
+ error: error
167
+ }
168
+ end
169
+ end
170
+ end
171
+
172
+ it 'invokes the hook' do
173
+ dispatch_result
174
+
175
+ expect(instance.test_error_data[:error].message).to eq('He-Must-Not-Be-Named')
176
+ expect(instance.test_error_data[:request_id]).to eq(nil) # Request_id is generated higher in the stack
177
+ end
178
+ end
179
+
134
180
  context 'with error hook raising an error' do
135
181
  let(:initialization_params) { super().merge('rack.logger' => rack_logger) }
136
182
  let(:rack_logger) { instance_double(::Logger, :rack_logger, info: nil, error: nil) }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonrpc2
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Geoff Youngs
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-04-14 00:00:00.000000000 Z
11
+ date: 2023-02-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpclient