Sha256: 110e23bb48e5f0839e8a4b640cd79c5ae455dc3a24c9a84653735ce2211d11d4

Contents?: true

Size: 1.77 KB

Versions: 3

Compression:

Stored size: 1.77 KB

Contents

module JsTestCore
  module Resources
    class WebRoot
      LOCATIONS = [
        ['', lambda do |web_root|
          web_root
        end],
        ['core', lambda do
          Resources::Dir.new(JsTestCore::Server.core_path, "/core")
        end],
        ['implementations', lambda do
          Resources::Dir.new(JsTestCore::Server.implementation_root_path, "/implementations")
        end],
        ['suites', lambda do
          Resources::Suite
        end],
        ['runners', lambda do
          Resources::Runners.new
        end]
      ]

      class << self
        attr_accessor :dispatch_strategy
        def dispatch_specs
          self.dispatch_strategy = :specs
        end
      end

      attr_reader :public_path
      def initialize(public_path)
        @public_path = ::File.expand_path(public_path)
      end

      def locate(name)
        if self.class.dispatch_strategy == :specs && name == 'specs'
          return JsTestCore::Resources::Specs::SpecDir.new(JsTestCore::Server.spec_root_path, "/specs")
        end

        location, initializer = LOCATIONS.find do |location|
          location.first == name
        end
        if initializer
          initializer.call(self)
        else
          potential_file_in_public_path = "#{public_path}/#{name}"
          if ::File.directory?(potential_file_in_public_path)
            Resources::Dir.new(potential_file_in_public_path, "/#{name}")
          elsif ::File.exists?(potential_file_in_public_path)
            Resources::File.new(potential_file_in_public_path, "/#{name}")
          else
            Resources::FileNotFound.new(name)
          end
        end
      end

      def get(request, response)
        response.status = 301
        response['Location'] = "/#{self.class.dispatch_strategy}"
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 3 rubygems

Version Path
btakita-screw-unit-server-0.3.0 vendor/js-test-core/lib/js_test_core/resources/web_root.rb
btakita-screw_unit-0.3.0 vendor/js-test-core/lib/js_test_core/resources/web_root.rb
screw-unit-server-0.3.0 vendor/js-test-core/lib/js_test_core/resources/web_root.rb