Class: Goliath::Rack::Builder

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

Class Method Summary (collapse)

Class Method Details

+ (Object) build(klass, api)

Builds the rack middleware chain for the given API

Parameters:

  • (Class) klass

    The API class to build the middlewares for

  • (Object) api

    The instantated API

Returns:

  • (Object)

    The Rack middleware chain



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/goliath/rack/builder.rb', line 9

def self.build(klass, api)
  ::Rack::Builder.new do
    klass.middlewares.each do |mw|
      use(*(mw[0..1].compact), &mw[2])
    end

    # If you use map you can't use run as
    # the rack builder will blowup.
    if klass.maps.empty?
      run api
    else
      klass.maps.each do |(path, route_klass, blk)|
        if route_klass
          map(path) do
            route_klass.middlewares.each do |mw|
              use(*(mw[0..1].compact), &mw[2])
            end
            run klass.new
          end
        else
          map(path, &blk)
        end
      end
    end
  end
end