<commit_before>require 'rack'
require 'rack/ssl'
require 'rack/cache'
require 'delegate'
require 'time'

class Travis::Web::App
  # Simple Rack router that behaves like a hash.
  # Key is the path, value the response.
  class Router < DelegateClass(Hash)
    def initialize
      super({})
    end

    def call(env)
      self[env['PATH_INFO']]
    end
  end

  def self.new(options = {})
    return super unless options[:environment] == 'development'
    proc { |e| super.call(e) } # poor man's reloader
  end

  attr_reader :app, :router, :environment, :version, :last_modified, :age, :options, :root

  def initialize(options = {})
    @options       = options
    @environment   = options.fetch(:environment)
    @root          = options.fetch(:root)
    @router        = Router.new
    @app           = builder.to_app
    @version       = File.read File.expand_path('version', root)
    @last_modified = Time.now
    @age           = 60 * 60 * 24 * 365
    load_routes
  end

  def call(env)
    app.call(env)
  end

  private

    def load_routes
      each_file { |f| router[route_for(f)] = response_for(f) }
      router.default = router['/']
    end

    def response_for(file)
      content = File.read(file)
      set_config(content) if index? file

      headers = {
        'Content-Length'   => content.bytesize.to_s,
        'Content-Location' => route_for(file),
        'Cache-Control'    => cache_control(file),
        'Content-Location' => route_for(file),
        'Content-Type'     => mime_type(file),
        'ETag'             => version,
        'Last-Modified'    => last_modified.httpdate,
        'Expires'          => (last_modified + age).httpdate,
        'Vary'             => ''
      }

      [ 200, headers, [ content ] ]
    end

    def each_file
      pattern = File.join(root, '**/*')
      Dir.glob(pattern) { |f| yield f if File.file? f }
    end

    def prefix?(file)
      file =~ /^(styles|scripts)\//
    end

    def index?(file)<commit_msg>Fix spec.html serving<commit_after>require 'rack'
require 'rack/ssl'
require 'rack/cache'
require 'delegate'
require 'time'

class Travis::Web::App
  # Simple Rack router that behaves like a hash.
  # Key is the path, value the response.
  class Router < DelegateClass(Hash)
    def initialize
      super({})
    end

    def call(env)
      self[env['PATH_INFO']]
    end
  end

  def self.new(options = {})
    return super unless options[:environment] == 'development'
    proc { |e| super.call(e) } # poor man's reloader
  end

  attr_reader :app, :router, :environment, :version, :last_modified, :age, :options, :root

  def initialize(options = {})
    @options       = options
    @environment   = options.fetch(:environment)
    @root          = options.fetch(:root)
    @router        = Router.new
    @app           = builder.to_app
    @version       = File.read File.expand_path('version', root)
    @last_modified = Time.now
    @age           = 60 * 60 * 24 * 365
    load_routes
  end

  def call(env)
    app.call(env)
  end

  private

    def load_routes
      each_file { |f| router[route_for(f)] = response_for(f) }
      router.default = router['/']
    end

    def response_for(file)
      content = File.read(file)
      set_config(content) if config_needed? file

      headers = {
        'Content-Length'   => content.bytesize.to_s,
        'Content-Location' => route_for(file),
        'Cache-Control'    => cache_control(file),
        'Content-Location' => route_for(file),
        'Content-Type'     => mime_type(file),
        'ETag'             => version,
        'Last-Modified'    => last_modified.httpdate,
        'Expires'          => (last_modified + age).httpdate,
        'Vary'             => ''
      }

      [ 200, headers, [ content ] ]
    end

    def each_file
      pattern = File.join(root, '**/*')
      Dir.glob(pattern) { |f| yield f if File.file? f }
    end

    def prefix?(file)
      file =~ /^(styles|scripts)\//
    end

    def config_needed?(file)
      index?(file) || file.end_with?('spec.html')
    end

    def index?(file)
{'fraction_non_alphanumeric': 0.13228809407153355, 'fraction_numerical': 0.005879470847623714, 'mean_word_length': 2.26378896882494, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2603243', 'n_tokens_mistral': 1356, 'n_tokens_neox': 1297, 'n_words': 308}