Class: GlassOctopus::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/glass_octopus/builder.rb

Overview

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.

Examples:

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

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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Builder

Returns a new instance of Builder.



51
52
53
54
55
56
# File 'lib/glass_octopus/builder.rb', line 51

def initialize(&block)
  @entries = []
  @app = ->(ctx) {}

  instance_eval(&block) if block_given?
end

Instance Method Details

#call(context) ⇒ void

Note:

This method instantiates a new middleware stack on every call.

This method returns an undefined value.

Generate the middleware stack and call it with the passed in context.

Parameters:

  • context (Context)

    message context



94
95
96
# File 'lib/glass_octopus/builder.rb', line 94

def call(context)
  to_app.call(context)
end

#run(app) ⇒ Builder

Sets the final step in the middleware pipeline, essentially the application itself. Takes a parameter that responds to #call(ctx).

Parameters:

  • app (#call)

    the application to process messages

Returns:

  • (Builder)

    returns self so calls are chainable



74
75
76
77
# File 'lib/glass_octopus/builder.rb', line 74

def run(app)
  @app = app
  self
end

#to_app#call

Generate a new middleware stack from the registered entries.

Returns:

  • (#call)

    the entry point of the middleware stack which is callable and when called runs the whole stack.



83
84
85
86
87
# 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

#use(klass, *args, &block) ⇒ Builder

Append a middleware to the stack.

Parameters:

  • klass (Class, #call)

    a middleware class or a callable object

  • args (Array)

    arguments to be passed to the klass constructor

  • block

    a block to be passed to klass constructor

Returns:

  • (Builder)

    returns self so calls are chainable



64
65
66
67
# File 'lib/glass_octopus/builder.rb', line 64

def use(klass, *args, &block)
  @entries << Entry.new(klass, args, block)
  self
end