Sha256: 024f667b706b40880a1f311879efc4392f6c281a70d63cf4b58ff0895d4d8584

Contents?: true

Size: 1.4 KB

Versions: 7

Compression:

Stored size: 1.4 KB

Contents

module Apipie

  class FileHandler
    def initialize(root)
      @root          = root.chomp('/')
      @compiled_root = /^#{Regexp.escape(root)}/
      @file_server   = ::Rack::File.new(@root)
    end

    def match?(path)
      path = path.dup

      full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
      paths = "#{full_path}#{ext}"

      matches = Dir[paths]
      match = matches.detect { |m| File.file?(m) }
      if match
        match.sub!(@compiled_root, '')
        match
      end
    end

    def call(env)
      @file_server.call(env)
    end

    def ext
      @ext ||= begin
        ext = ::ActionController::Base.page_cache_extension
        "{,#{ext},/index#{ext}}"
      end
    end
  end

  class StaticDispatcher
    # Dispatches the statis files. Simillar to ActionDispatch::Static, but
    # it supports different baseurl configurations
    def initialize(app, path, baseurl)
      @app = app
      @baseurl = baseurl
      @file_handler = Apipie::FileHandler.new(path)
    end

    def call(env)
      case env['REQUEST_METHOD']
      when 'GET', 'HEAD'
        path = env['PATH_INFO'].sub("#{@baseurl}/","/apipie/").chomp('/')
        path.sub!("#{ENV["RAILS_RELATIVE_URL_ROOT"]}",'')

        if match = @file_handler.match?(path)
          env["PATH_INFO"] = match
          return @file_handler.call(env)
        end
      end

      @app.call(env)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
apipie-rails-0.0.13 lib/apipie/static_dispatcher.rb
apipie-rails-0.0.12 lib/apipie/static_dispatcher.rb
apipie-rails-0.0.11 lib/apipie/static_dispatcher.rb
apipie-rails-0.0.10 lib/apipie/static_dispatcher.rb
apipie-rails-0.0.9 lib/apipie/static_dispatcher.rb
apipie-rails-0.0.8 lib/apipie/static_dispatcher.rb
apipie-rails-0.0.7 lib/apipie/static_dispatcher.rb