carrot_rpc 0.3.0 → 0.4.0

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: 99b2493a7ffe385eee686f6d78b57e7e2240d370
4
- data.tar.gz: 6839c61baeafbeb0097bf89badd080d1d05c480d
3
+ metadata.gz: fa0a0dee401c7064c07550c3c601e23353d2a88c
4
+ data.tar.gz: 6994fbf5613108aed02b583bfa4f900ac2311ea5
5
5
  SHA512:
6
- metadata.gz: e611390a7dbc157412efbdd2c472c1358b1a7f6a62ca69e9637f010d46cf4382ec2a7f545e1279189aa4b1dac5426b74f7f30b7fdc326788069669470fd44d88
7
- data.tar.gz: c6ccb1a6315f64c9180b938d551a1d38ea174ff95bc2dd48d729efe9dfa162f78069e4f7a10131f96de735bcd35eb4535afd989931684ce37d026686b67e11d9
6
+ metadata.gz: 96d9d94ac495890858a71d5cf6bb5fb87390c123217c40220070cc2c56a27a67757fcd79b66041db1a9f588976b9135289b48a6c92e30f67d350f7affc47ec30
7
+ data.tar.gz: c57f379c26ea30abf880ea0c0b44d1303ab5635aad92bb7ecef87ac9e1ed6cee4e8ef1c1e8f8b49d8571481ca1792b1db5c658416ad70516136fa39de34702c3
data/CHANGELOG.md CHANGED
@@ -29,6 +29,16 @@
29
29
  # Changelog
30
30
  All significant changes in the project are documented here.
31
31
 
32
+ ## v0.4.0
33
+
34
+ ### Enhancements
35
+ * [#20](https://githb.com/C-S-D/carrot_rpc/pull/20) - `config.before_request` may be set with a `#call(params) :: params` that is passed the `params` and returns altered `params` that are published to the queue. - [shamil614](https://github.com/shamil614)
36
+
37
+ ### Bug Fixes
38
+ * [#19](https://githb.com/C-S-D/carrot_rpc/pull/19) - [KronicDeth](http://github.com/kronicdeth)
39
+ * Put JSONAPI errors documents into the JSONRPC error fields instead of returning as normal results as consumers, such as `Rpc.Generic.Client` are expecting all errors to be in JSONRPC's error field and not have to check if the non-error `result` contains a JSONAPI level error. This achieves parity with the behavior in the Elixir `Rpc.Generic.Server`.
40
+ * Scrub JSONAPI error fields that are `nil` so they don't get transmitted as `null`. JSONAPI spec is quite clear that `null` columns shouldn't be transmitted except in the case of `null` data to signal a missing singleton resource. This achieves compatibility with the error parsing in `Rpc.Generic.Client` in Elixir.
41
+
32
42
  ## v0.3.0
33
43
 
34
44
  ### Enhancements
data/README.md CHANGED
@@ -84,6 +84,8 @@ CarrotRpc.configure do |config|
84
84
  # Create a new logger or use the Rails logger.
85
85
  # When using Rails, use a tagged log to make it easier to track RPC.
86
86
  config.logger = CarrotRpc::TaggedLog.new(logger: Rails.logger, tags: ["Carrot RPC Client"])
87
+ # Set a Proc to allow manipulation of the params on the RpcClient before the request is sent.
88
+ config.before_request = proc { |params| params.merge(foo: "bar") }
87
89
 
88
90
  # Don't use. Server implementation only. The values below are set via CLI:
89
91
  # config.pidfile = nil
@@ -136,6 +138,8 @@ Example Client: `app/clients/cars_client.rb`
136
138
  ```ruby
137
139
  class CarClient < CarrotRpc::RpcClient
138
140
  queue_name "car_queue"
141
+ # optional hook to modify params before submission
142
+ before_request proc { |params| params.merge(foo: "bar") }
139
143
 
140
144
  # By default RpcClient defines the following Railsy inspired methods:
141
145
  # def show(params)
@@ -1,6 +1,7 @@
1
1
  # Global configuration for {CarrotRpc}. Access with {CarrotRpc.configuration}.
2
2
  class CarrotRpc::Configuration
3
- attr_accessor :logger, :logfile, :loglevel, :daemonize, :pidfile, :runloop_sleep, :autoload_rails, :bunny
3
+ attr_accessor :logger, :logfile, :loglevel, :daemonize, :pidfile, :runloop_sleep, :autoload_rails, :bunny,
4
+ :before_request
4
5
 
5
6
  # logfile - set logger to a file. overrides rails logger.
6
7
 
@@ -13,5 +14,6 @@ class CarrotRpc::Configuration
13
14
  @runloop_sleep = 0
14
15
  @autoload_rails = true
15
16
  @bunny = nil
17
+ @before_request = nil
16
18
  end
17
19
  end
@@ -10,6 +10,16 @@ class CarrotRpc::RpcClient
10
10
 
11
11
  extend CarrotRpc::ClientServer
12
12
 
13
+ def self.before_request(*proc)
14
+ if proc.length == 0
15
+ @before_request
16
+ elsif proc.length == 1
17
+ @before_request = proc.first || CarrotRpc.configuration.before_request
18
+ else
19
+ fail ArgumentError
20
+ end
21
+ end
22
+
13
23
  # Use defaults for application level connection to RabbitMQ
14
24
  # All RPC data goes over the same queue. I think that's ok....
15
25
  def initialize(config: nil)
@@ -50,6 +60,7 @@ class CarrotRpc::RpcClient
50
60
  # @return [Object] the result of the method call.
51
61
  def remote_call(remote_method, params)
52
62
  correlation_id = SecureRandom.uuid
63
+ params = self.class.before_request.call(params) if self.class.before_request
53
64
  publish(correlation_id: correlation_id, method: remote_method, params: params.rename_keys("_", "-"))
54
65
  wait_for_result(correlation_id)
55
66
  end
@@ -51,7 +51,7 @@ module CarrotRpc::RpcServer::JSONAPIResources
51
51
  primary_resource_klass: resource_klass,
52
52
  include_directives: request ? request.include_directives : nil,
53
53
  fields: request ? request.fields : nil,
54
- base_url: base_url,
54
+ base_url: base_url(operation_results, request),
55
55
  key_formatter: key_formatter,
56
56
  route_formatter: route_formatter,
57
57
  base_meta: base_meta(request),
@@ -67,11 +67,42 @@ class CarrotRpc::RpcServer
67
67
 
68
68
  # See http://www.jsonrpc.org/specification#response_object
69
69
  def reply_result(result, properties:, request_message:)
70
- response_message = { id: request_message[:id], result: result, jsonrpc: "2.0" }
70
+ if result["errors"]
71
+ reply_result_with_errors(result, properties: properties, request_message: request_message)
72
+ else
73
+ reply_result_without_errors(result, properties: properties, request_message: request_message)
74
+ end
75
+ end
76
+
77
+ def reply_result_with_errors(result, properties:, request_message:)
78
+ scrubbed_result = result.merge(
79
+ "errors" => scrub_errors(result.fetch("errors"))
80
+ )
81
+ reply_error({ code: 422, data: scrubbed_result, message: "JSONAPI error" },
82
+ properties: properties,
83
+ request_message: request_message)
84
+ end
85
+
86
+ def reply_result_without_errors(result, properties:, request_message:)
87
+ response_message = { id: request_message[:id], jsonrpc: "2.0", result: result }
71
88
 
72
89
  logger.debug "Publishing result: #{result} to #{response_message}"
73
90
 
74
91
  reply properties: properties,
75
92
  response_message: response_message
76
93
  end
94
+
95
+ # Removes `nil` values as JSONAPI spec expects unset keys not to be transmitted
96
+ def scrub_error(error)
97
+ error.reject { |_, value|
98
+ value.nil?
99
+ }
100
+ end
101
+
102
+ # Removes `nil` values as JSONAPI spec expects unset keys not to be transmitted
103
+ def scrub_errors(errors)
104
+ errors.map { |error|
105
+ scrub_error(error)
106
+ }
107
+ end
77
108
  end
@@ -1,3 +1,3 @@
1
1
  module CarrotRpc
2
- VERSION = "0.3.0".freeze
2
+ VERSION = "0.4.0".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carrot_rpc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Scott Hamilton
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-04-11 00:00:00.000000000 Z
12
+ date: 2016-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport