Sha256: e7de0f3cfc02e92cb3ad9e16a124bfe289815330cad99becb649a45bf85467db

Contents?: true

Size: 906 Bytes

Versions: 2

Compression:

Stored size: 906 Bytes

Contents

class Brief::Server::Gateway
  attr_reader :root

  def initialize(options={})
    @root = options.fetch(:root)
    @briefcases = {}
    @briefcase_options = options.fetch(:briefcase_options, {})
    load_briefcases
  end

  def briefcase_options
    (@briefcase_options || {})
  end

  def load_briefcases
    config_path = briefcase_options.fetch(:config_path, "brief.rb")

    root.children.select(&:directory?).each do |dir|
      if dir.join(config_path).exist?
        slug = dir.basename.to_s.parameterize
        @briefcases[slug] ||= Brief::Briefcase.new(briefcase_options.merge(root: dir))
      end
    end
  end

  def call(env)
    request = Rack::Request.new(env)
    name    = request.path.match(/\/\w+\/(\w+)/)[1] rescue nil

    if name && @briefcases[name]
      @briefcases[name].server.call(env)
    else
      [404, {}, ["Not found: #{ name } -- #{ request.path }"]]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
brief-1.4.2 lib/brief/server/gateway.rb
brief-1.4.1 lib/brief/server/gateway.rb