Class: Ears::Middlewares::JSON
- Inherits:
-
Ears::Middleware
- Object
- Ears::Middleware
- Ears::Middlewares::JSON
- Defined in:
- lib/ears/middlewares/json.rb
Overview
A middleware that automatically parses your JSON payload.
Instance Method Summary collapse
- #call(delivery_info, metadata, payload, app) ⇒ Object
-
#initialize(opts = {}) ⇒ JSON
constructor
A new instance of JSON.
Constructor Details
#initialize(opts = {}) ⇒ JSON
Returns a new instance of JSON.
11 12 13 14 15 |
# File 'lib/ears/middlewares/json.rb', line 11 def initialize(opts = {}) super() @on_error = opts.fetch(:on_error, Proc.new { :reject }) @symbolize_keys = opts.fetch(:symbolize_keys, true) end |
Instance Method Details
#call(delivery_info, metadata, payload, app) ⇒ Object
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/ears/middlewares/json.rb', line 17 def call(delivery_info, , payload, app) begin parsed_payload = MultiJson.load(payload, symbolize_keys: symbolize_keys) rescue => e return on_error.call(e) end app.call(delivery_info, , parsed_payload) end |