Sha256: f43888095180c97291f187d5a9496df69355dc19e3869ec8ea85bc88b29b81e4

Contents?: true

Size: 839 Bytes

Versions: 1

Compression:

Stored size: 839 Bytes

Contents

require 'spec_helper'
require 'json'

# mount Rack::Reducer as middleware, let it filter data into env['rack.reduction'],
# and respond with env['rack.reduction'].to_json
module MiddlewareTest
  DEFAULTS = {
    dataset: DB[:artists].all,
    filters: [
      ->(genre:) { select { |item| item[:genre].match(/#{genre}/i) } },
      ->(name:) { select { |item| item[:name].match(/#{name}/i) } },
      ->(order:) { sort_by { |item| item[order.to_sym] } }
    ]
  }

  def self.app(options = {}, key = options[:key] || 'rack.reduction')
    Rack::Builder.new do
      use Rack::Reducer, DEFAULTS.merge(options)
      run ->(env) { [200, {}, [env[key].to_json]] }
    end
  end
end

describe MiddlewareTest.app do
  it_behaves_like Rack::Reducer
end

describe MiddlewareTest.app(key: 'some.custom.key') do
  it_behaves_like Rack::Reducer
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack-reducer-0.1.1 spec/middleware_spec.rb