Class: Frank::Middleware::Statik

Inherits:
Object
  • Object
show all
Defined in:
lib/frank/middleware/statik.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Statik

Returns a new instance of Statik.



5
6
7
8
9
10
11
# File 'lib/frank/middleware/statik.rb', line 5

def initialize(app, options={})
  @app = app
  frank_root = File.expand_path(File.dirname(File.dirname(__FILE__))) + '/templates'
  root = options[:root] || Dir.pwd
  @frank_server = Rack::File.new(frank_root)
  @static_server = Rack::File.new(root)
end

Instance Method Details

#call(env) ⇒ Object

handles serving from __frank__ looks for static access, if not found, passes request to frank



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/frank/middleware/statik.rb', line 16

def call(env)
  path = env['PATH_INFO'].dup

  if path.include? '__frank__'
    env['PATH_INFO'].gsub!('/__frank__', '')
    result = @frank_server.call(env)
  else
    env['PATH_INFO'] << '/' unless path.match(/(\.\w+|\/)$/)
    env['PATH_INFO'] << 'index.html' if path[-1..-1] == '/'
    result = @static_server.call(env)
  end

  # return if static assets found
  # else reset the path and pass to frank
  if result[0] == 200 || result[0] == 304
    result
  else
    env['PATH_INFO'] = path
    @app.call(env)
  end

end