Class: Lepus::Producers::Middlewares::JSON
- Inherits:
-
Middleware
- Object
- Middleware
- Lepus::Producers::Middlewares::JSON
- Defined in:
- lib/lepus/producers/middlewares/json.rb
Overview
A middleware that serializes Hash payloads to JSON and sets the content_type.
Instance Method Summary collapse
- #call(message, app) ⇒ Object
-
#initialize(**opts) ⇒ JSON
constructor
A new instance of JSON.
Constructor Details
#initialize(**opts) ⇒ JSON
Returns a new instance of JSON.
12 13 14 15 |
# File 'lib/lepus/producers/middlewares/json.rb', line 12 def initialize(**opts) super @only_hash = opts.fetch(:only_hash, true) end |
Instance Method Details
#call(message, app) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/lepus/producers/middlewares/json.rb', line 17 def call(, app) payload = .payload if should_serialize?(payload) serialized_payload = MultiJson.dump(payload) = (., content_type: "application/json") = .mutate(payload: serialized_payload, metadata: ) end app.call() end |