railway-ipc 3.0.0 → 4.0.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: 5002df116daa50da3bfcefc3a966901a49c04671e869f23a645482ef9cf4c619
4
- data.tar.gz: de49b0064f5b57f77893f46701007928a3e1c0fa4644a34041de50c6c4113f18
3
+ metadata.gz: 7f349dc76232b1cef6492bb2dc09cdbd58bf2ad22be64bfd1771e4e6d7192b16
4
+ data.tar.gz: 3e04b076735ca1bcc8d1cc376925311b1547b8b13a554691726f348a62625cfc
5
5
  SHA512:
6
- metadata.gz: 9ed5098e317e039d3e4ed0305aca7f571be35f1ace0d88a513ef702ec381f10fe8291378b00ca181a6d23cfcbc7882c367dacedfb68d458ff71f9c8f808128d3
7
- data.tar.gz: a30c8d4795fcd839588a8e9fc375c027b4a36e48b64eee9800a659fa4380f2d89d66f89c838c80fb70bf2072917146bcb6bd4ad4917141fa4dcfc0cfdf89db14
6
+ metadata.gz: d0dd3263f9ca6c57da0fd98c25b720f1f7bad7ec87bb4c34cd110d51824b350ca255d9a467170d420aeb323faff081f03ea62c18c080f3a52fd72f0c687be49a
7
+ data.tar.gz: 74d6f196446603d93d42050e2c672ccac22ec49cedbc2d026cf8f8f61eb0bc4d6537b179ad2d60cd37426ad6b60ab9e428d40b21f69ce21426cc86cbeefaacaf
data/.gitignore CHANGED
@@ -4,6 +4,7 @@
4
4
  /coverage/
5
5
  /doc/
6
6
  /pkg/
7
+ /rake
7
8
  /spec/reports/
8
9
  /tmp/
9
10
  Gemfile.lock
@@ -0,0 +1 @@
1
+ 2.6.5
@@ -10,6 +10,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
10
10
  ### Removed
11
11
  ### Fixed
12
12
 
13
+ ## [4.0.0] - 2021-01-11
14
+ ### Added
15
+ * JSON decoder for consumers that can handle JSON encoded Protobufs. Note that the publishers do not (yet) have the option of encoding the messages as JSON.
16
+
17
+ ### Changed
18
+ * (Breaking Change) Rename `Consumer#work` to `Consumer#work_with_params`. This was necessary so that we can support specifying different message encodings via metadata in the future. If the message encoding cannot be determined from the message metadata fall back to a default decoder (binary protobufs).
19
+
20
+ ### Fixed
21
+ * `./bin/console` script was broken because Pry wasn't a dependency; added Pry as a development dependency only.
22
+
13
23
  ## [3.0.0] - 2020-12-07
14
24
  ### Changed
15
25
  * Consumers _will no longer crash_ when an exception is raised. Instead, consumers will move the message that caused the exception to a single dead-letter exchange called 'ipc:errors'. Railway will configure the dead-letter exchange automatically.
@@ -86,7 +96,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
86
96
  ### Added
87
97
  - Correlation ID and message UUID are auto generated for messages for IDs are not passed in [#23](https://github.com/learn-co/railway_ipc_gem/pull/23)
88
98
 
89
- [Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/v30.0.0...HEAD
99
+ [Unreleased]: https://github.com/learn-co/railway_ipc_gem/compare/v4.0.0...HEAD
100
+ [4.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v3.0.0...v4.0.0
90
101
  [3.0.0]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.2...v3.0.0
91
102
  [2.2.2]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.1...v2.2.2
92
103
  [2.2.1]: https://github.com/learn-co/railway_ipc_gem/compare/v2.2.0...v2.2.1
@@ -14,6 +14,7 @@ require 'railway_ipc/unknown_message.pb'
14
14
  require 'railway_ipc/rabbitmq/adapter'
15
15
  require 'railway_ipc/handler'
16
16
  require 'railway_ipc/handler_store'
17
+ require 'railway_ipc/message_decoders'
17
18
  require 'railway_ipc/incoming_message'
18
19
  require 'railway_ipc/connection_manager'
19
20
  require 'railway_ipc/publisher'
@@ -56,8 +56,14 @@ module RailwayIpc
56
56
  queue.opts[:exchange]
57
57
  end
58
58
 
59
- def work(payload)
60
- message = RailwayIpc::IncomingMessage.new(payload)
59
+ # REFACTOR: Long term we should think about not leaking Sneakers
60
+ # methods as part of Railway's public API since clients can (and do)
61
+ # override them. -BN
62
+ def work_with_params(payload, _delivery_info, metadata)
63
+ message_format = metadata.fetch('headers', {})
64
+ .fetch('message_format', 'protobuf_binary')
65
+
66
+ message = RailwayIpc::IncomingMessage.new(payload, message_format: message_format)
61
67
  RailwayIpc::ProcessIncomingMessage.call(self, message)
62
68
  ack!
63
69
  rescue StandardError => e
@@ -2,9 +2,10 @@
2
2
 
3
3
  module RailwayIpc
4
4
  class IncomingMessage
5
- attr_reader :type, :payload, :parsed_payload, :errors
5
+ attr_reader :type, :message_format, :payload, :parsed_payload, :errors
6
6
 
7
- def initialize(payload)
7
+ def initialize(payload, message_format: nil)
8
+ @message_format = message_format
8
9
  @parsed_payload = JSON.parse(payload)
9
10
  @type = parsed_payload['type']
10
11
  @payload = payload
@@ -32,20 +33,23 @@ module RailwayIpc
32
33
  end
33
34
 
34
35
  def decoded
35
- @decoded ||=
36
- begin
37
- protobuf_msg = Base64.decode64(parsed_payload['encoded_message'])
38
- decoder = Kernel.const_get(type)
39
- decoder.decode(protobuf_msg)
40
- rescue Google::Protobuf::ParseError => e
41
- raise RailwayIpc::IncomingMessage::ParserError.new(e)
42
- rescue NameError
43
- RailwayIpc::Messages::Unknown.decode(protobuf_msg)
44
- end
36
+ @decoded ||= \
37
+ get_decoder(message_format).call(type, parsed_payload['encoded_message'])
45
38
  end
46
39
 
47
40
  def stringify_errors
48
41
  errors.values.join(', ')
49
42
  end
43
+
44
+ private
45
+
46
+ DEFAULT_DECODER = RailwayIpc::MessageDecoders::ProtobufBinaryDecoder
47
+
48
+ def get_decoder(name)
49
+ {
50
+ 'protobuf_binary' => RailwayIpc::MessageDecoders::ProtobufBinaryDecoder,
51
+ 'protobuf_json' => RailwayIpc::MessageDecoders::ProtobufJsonDecoder
52
+ }.fetch(name, DEFAULT_DECODER)
53
+ end
50
54
  end
51
55
  end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailwayIpc
4
+ module MessageDecoders
5
+ ProtobufBinaryDecoder = lambda do |type, encoded_message|
6
+ protobuf_msg = Base64.decode64(encoded_message)
7
+ protobuf_klass = Kernel.const_get(type)
8
+ protobuf_klass.decode(protobuf_msg)
9
+ rescue Google::Protobuf::ParseError => e
10
+ raise RailwayIpc::IncomingMessage::ParserError.new(e)
11
+ rescue NameError
12
+ RailwayIpc::Messages::Unknown.decode(protobuf_msg)
13
+ end
14
+
15
+ ProtobufJsonDecoder = lambda do |type, message_hash|
16
+ protobuf_klass = Kernel.const_get(type)
17
+ protobuf_klass.new(message_hash)
18
+ rescue ArgumentError => e
19
+ raise RailwayIpc::IncomingMessage::ParserError.new(e)
20
+ rescue NameError
21
+ # NOTE: I didn't realize this until I made this ProtobufJsonDecoder, but
22
+ # the ProtobufBinaryDecoder will ignore any unknown keys -- which is
23
+ # probably not what we want. I'm coding this the same way as the binary
24
+ # protobuf version for consistency, but we should re-think how we want to
25
+ # handle this situation. -BN
26
+ RailwayIpc::Messages::Unknown.new(
27
+ user_uuid: message_hash.fetch(:user_uuid, ''),
28
+ correlation_id: message_hash.fetch(:correlation_id, ''),
29
+ uuid: message_hash.fetch(:uuid, ''),
30
+ context: message_hash.fetch(:context, {})
31
+ )
32
+ end
33
+ end
34
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RailwayIpc
4
- VERSION = '3.0.0'
4
+ VERSION = '4.0.0'
5
5
  end
@@ -48,6 +48,7 @@ Gem::Specification.new do |spec|
48
48
  spec.add_development_dependency 'database_cleaner', '~> 1.7'
49
49
  spec.add_development_dependency 'listen', '~> 3.0.5'
50
50
  spec.add_development_dependency 'pg', '~> 0.18'
51
+ spec.add_development_dependency 'pry', '~> 0.13'
51
52
  spec.add_development_dependency 'rails', '~> 5.0.7'
52
53
  spec.add_development_dependency 'rspec-rails'
53
54
  spec.add_development_dependency 'shoulda-matchers', '~> 4.2'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: railway-ipc
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ''
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-12-07 00:00:00.000000000 Z
11
+ date: 2021-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -164,6 +164,20 @@ dependencies:
164
164
  - - "~>"
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0.18'
167
+ - !ruby/object:Gem::Dependency
168
+ name: pry
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '0.13'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '0.13'
167
181
  - !ruby/object:Gem::Dependency
168
182
  name: rails
169
183
  requirement: !ruby/object:Gem::Requirement
@@ -213,6 +227,7 @@ extensions: []
213
227
  extra_rdoc_files: []
214
228
  files:
215
229
  - ".gitignore"
230
+ - ".ruby-version"
216
231
  - CHANGELOG.md
217
232
  - CODE_OF_CONDUCT.md
218
233
  - Gemfile
@@ -233,6 +248,7 @@ files:
233
248
  - lib/railway_ipc/handler_store.rb
234
249
  - lib/railway_ipc/incoming_message.rb
235
250
  - lib/railway_ipc/logger.rb
251
+ - lib/railway_ipc/message_decoders.rb
236
252
  - lib/railway_ipc/models/consumed_message.rb
237
253
  - lib/railway_ipc/models/published_message.rb
238
254
  - lib/railway_ipc/publisher.rb