class GlassOctopus::Builder
GlassOctopus::Builder
is a small DLS to build processing pipelines. It is very similar to Rack::Builder.
Middleware
can be a class with a similar signature to Rack middleware. The constructor needs to take an app
object which is basically the next middleware in the stack and the instance of the class has to respond to +#call(ctx)+.
Lambdas/procs can also be used as middleware. In this case the middleware does not have control over the execution of the next middleware: the next one is always called.
@example
require "glass_octopus" app = GlassOctopus::Builder.new do use GlassOctopus::Middleware::CommonLogger use GlassOctopus::Middleware::JsonParser run Proc.new { |ctx| puts "Hello, #{ctx.params['name']}" } end.to_app GlassOctopus.run(app) do |config| # set config here end
@example Using lambdas
require "glass_octopus" logger = Logger.new("log/example.log") app = GlassOctopus::Builder.new do use lambda { |ctx| ctx.logger = logger } run Proc.new { |ctx| ctx.logger.info "Hello, #{ctx.params['name']}" } end.to_app GlassOctopus.run(app) do |config| # set config here end
Constants
- Entry
Represents an entry in the middleware stack. @api private
Public Class Methods
# File lib/glass_octopus/builder.rb, line 51 def initialize(&block) @entries = [] @app = ->(ctx) {} instance_eval(&block) if block_given? end
Public Instance Methods
Generate the middleware stack and call it with the passed in context.
@param context [Context] message context @return [void] @note This method instantiates a new middleware stack on every call.
# File lib/glass_octopus/builder.rb, line 94 def call(context) to_app.call(context) end
Sets the final step in the middleware pipeline, essentially the application itself. Takes a parameter that responds to +#call(ctx)+.
@param app [#call] the application to process messages @return [Builder] returns self so calls are chainable
# File lib/glass_octopus/builder.rb, line 74 def run(app) @app = app self end
Generate a new middleware stack from the registered entries.
@return [#call] the entry point of the middleware stack which is callable
and when called runs the whole stack.
# File lib/glass_octopus/builder.rb, line 83 def to_app @entries.reverse_each.reduce(@app) do |app, current_entry| current_entry.build(app) end end
Append a middleware to the stack.
@param klass [Class, call
] a middleware class or a callable object @param args [Array] arguments to be passed to the klass constructor @param block a block to be passed to klass constructor @return [Builder] returns self so calls are chainable
# File lib/glass_octopus/builder.rb, line 64 def use(klass, *args, &block) @entries << Entry.new(klass, args, block) self end