Class: ApipieDSL::FileHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/apipie_dsl/static_dispatcher.rb

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ FileHandler

Returns a new instance of FileHandler.



5
6
7
8
9
# File 'lib/apipie_dsl/static_dispatcher.rb', line 5

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

Instance Method Details

#cache_extensionObject



37
38
39
40
41
42
43
44
# File 'lib/apipie_dsl/static_dispatcher.rb', line 37

def cache_extension
  if ::ActionController::Base.respond_to?(:default_static_extension)
    ::ActionController::Base.default_static_extension
  else
    ::ActionController::Base.page_cache_extension
  end

end

#call(env) ⇒ Object



26
27
28
# File 'lib/apipie_dsl/static_dispatcher.rb', line 26

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

#extObject



30
31
32
33
34
35
# File 'lib/apipie_dsl/static_dispatcher.rb', line 30

def ext
  @ext ||= begin
    ext = cache_extension
    "{,#{ext},/index#{ext}}"
  end
end

#match?(path) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/apipie_dsl/static_dispatcher.rb', line 11

def match?(path)
  # Replace all null bytes
  path = ::Rack::Utils.unescape(path || '').gsub(/\x0/, '')

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

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