class Rack::Reducer::Middleware

Mount Rack::Reducer as middleware @example A microservice that filters artists

ArtistService = Rack::Builder.new do
  use(
    Rack::Reducer::Middleware,
    dataset: Artist.all,
    filters: [
      lambda { |name:| where(name: name) },
      lambda { |genre:| where(genre: genre) },
    ]
  )

  run ->(env) {  [200, {}, [env['rack.reduction'].to_json]] }
end

Public Class Methods

new(app, options = {}) click to toggle source
# File lib/rack/reducer/middleware.rb, line 22
def initialize(app, options = {})
  @app = app
  @key = options[:key] || 'rack.reduction'
  @reducer = Rack::Reducer.new(options[:dataset], *options[:filters])
end

Public Instance Methods

call(env) click to toggle source

Call the next app in the middleware stack, with `env` set to the ouput of a reduction

# File lib/rack/reducer/middleware.rb, line 30
def call(env)
  params = Rack::Request.new(env).params
  reduction = @reducer.apply(params)
  @app.call env.merge(@key => reduction)
end