Sha256: b1bb694e1b8a484cde58f7e5bc72b717a2011285fe39ce845916b25ecda76c12

Contents?: true

Size: 1.39 KB

Versions: 20

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require 'rack/builder'
class Rack::App::Endpoint::Builder
  def initialize(config)
    @config = config
  end

  def to_app
    build.to_app
  end

  protected

  def build
    builder = Rack::Builder.new
    apply_middleware_build_blocks(builder)
    builder.run(app)
    builder
  end

  def app
    case @config.type
    when :endpoint
      Rack::App::Endpoint::Executor.new(@config)
    else
      @config.callable
    end
  end

  def apply_middleware_build_blocks(builder)
    @config.middlewares.each do |builder_block|
      builder.instance_exec(builder, &builder_block)
    end
    builder.use(Rack::App::Middlewares::Configuration, @config)

    apply_catcher_on_need(builder)
    @config.ancestor_apps.reverse_each do |app_class|
      apply_hook_middlewares(app_class, builder)
    end
  end

  def apply_catcher_on_need(builder)
    at_least_one_hook_requested = @config.ancestor_apps.any? do |app_class|
      app_class.before.length + app_class.after.length > 0
    end

    if at_least_one_hook_requested
      builder.use(Rack::App::Endpoint::Catcher, @config)
    end
  end

  def apply_hook_middlewares(app_class, builder)
    app_class.before.each do |before_block|
      builder.use(Rack::App::Middlewares::Hooks::Before, before_block)
    end

    app_class.after.each do |after_block|
      builder.use(Rack::App::Middlewares::Hooks::After, after_block)
    end
  end
end

Version data entries

20 entries across 20 versions & 2 rubygems

Version Path
rack-app-11.0.2 lib/rack/app/endpoint/builder.rb
rack-app-11.0.1 lib/rack/app/endpoint/builder.rb
rack-app-11.0.0 lib/rack/app/endpoint/builder.rb
rack-app-10.0.0 lib/rack/app/endpoint/builder.rb
rack-app-9.0.0 lib/rack/app/endpoint/builder.rb
rack-app-8.1.0 lib/rack/app/endpoint/builder.rb
rack-app-8.0.1 lib/rack/app/endpoint/builder.rb
rack-app-8.0.0 lib/rack/app/endpoint/builder.rb
mountapi-0.11.1 vendor/bundle/ruby/2.7.0/gems/rack-app-7.7.0/lib/rack/app/endpoint/builder.rb
rack-app-7.7.0 lib/rack/app/endpoint/builder.rb
rack-app-7.6.5 lib/rack/app/endpoint/builder.rb
rack-app-7.6.4 lib/rack/app/endpoint/builder.rb
rack-app-7.6.3 lib/rack/app/endpoint/builder.rb
rack-app-7.6.2 lib/rack/app/endpoint/builder.rb
rack-app-7.6.1 lib/rack/app/endpoint/builder.rb
rack-app-7.6.0 lib/rack/app/endpoint/builder.rb
rack-app-7.5.2 lib/rack/app/endpoint/builder.rb
rack-app-7.5.1 lib/rack/app/endpoint/builder.rb
rack-app-7.5.0 lib/rack/app/endpoint/builder.rb
rack-app-7.4.0 lib/rack/app/endpoint/builder.rb