class Pith::Server::OutputFinder

Public Class Methods

new(app, project) click to toggle source
# File lib/pith/server.rb, line 23
def initialize(app, project)
  @app  = app
  @project = project
end

Public Instance Methods

call(env) click to toggle source
# File lib/pith/server.rb, line 28
def call(env)

  path_info = ::Rack::Utils.unescape(env["PATH_INFO"])
  ends_with_slash = (path_info[-1] == '/')

  outputs = @project.outputs.sort_by { |output| output.path }
  outputs.each do |output|

    output_path = "/" + output.path.to_s

    if !ends_with_slash && output_path =~ %r{^#{path_info}/}
      return [
        302,
        { "Location" => path_info + "/" },
        []
      ]
    end

    ["", ".html", "index.html"].map do |ext|
      if output_path == (path_info + ext)
        output.build
        env["PATH_INFO"] += ext
        return @app.call(env)
      end
    end

  end

  @app.call(env)

end