class JsonEmitter::Context
By using JsonEmitter.wrap
and JsonEmitter.error
you can wrap your streams within a special “context”.
This is probably most useful when your stream is being consumed by Rack/Rails/Sinatra/Grape/etc. Your app is probably depending on certain Rack middlewars to provide before/after behavior and error handling. All those middlewares will over by the time Rack consumes your stream, but you can use JsonEmitter.wrap
and JsonEmitter.error
to add critical behavior back in.
Attributes
rack_env[R]
@return [Hash] The Rack environment Hash (if present)
Public Class Methods
new(rack_env: nil)
click to toggle source
# File lib/json-emitter/context.rb, line 14 def initialize(rack_env: nil) @rack_env = rack_env @wrappers = JsonEmitter.wrappers.map(&:call).compact @error_handlers = JsonEmitter.error_handlers @pass_through_errors = [] @pass_through_errors << Puma::ConnectionError if defined? Puma::ConnectionError end
Public Instance Methods
error(&handler)
click to toggle source
Add an error handler. TODO better docs and examples.
# File lib/json-emitter/context.rb, line 32 def error(&handler) @error_handlers += [handler] end
execute(&inner)
click to toggle source
Execute a block within this context.
# File lib/json-emitter/context.rb, line 44 def execute(&inner) @wrappers.reduce(inner) { |f, outer_wrapper| ->() { outer_wrapper.call(f) } }.call rescue *@pass_through_errors => e raise e rescue => e @error_handlers.each { |h| h.call(e, self) } raise e end
request()
click to toggle source
wrap(&block)
click to toggle source
Wrap the enumeration in a block. It will be passed a callback which it must call to continue. TODO better docs and examples.
# File lib/json-emitter/context.rb, line 24 def wrap(&block) if (wrapper = block.call) @wrappers.unshift wrapper end end