Class: Servel::Middleware

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Middleware.



2
3
4
5
6
# File 'lib/servel/middleware.rb', line 2

def initialize(app, options = {})
  @app = app
  @root = Pathname.new(options[:root])
  @haml_context = Servel::HamlContext.new
end

Instance Method Details

#call(env) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/servel/middleware.rb', line 8

def call(env)
  path = env["PATH_INFO"]
  url_path = url_path_for(path)
  fs_path = @root + url_path[1..-1]

  unless fs_path.directory?
    return @app.call(env)
  end

  if path != "" && !path.end_with?("/")
    return [302, { "Location" => "#{url_path}/" }, []]
  end

  url_path << "/" if url_path != "" && !url_path.end_with?("/")

  klass = if env['QUERY_STRING'] == "gallery"
    Servel::GalleryView
  else
    Servel::IndexView
  end

  [200, {}, StringIO.new(klass.new(url_path, fs_path).render(@haml_context))]
end

#url_path_for(url_path) ⇒ Object



32
33
34
35
36
37
# File 'lib/servel/middleware.rb', line 32

def url_path_for(url_path)
  url_path = Rack::Utils.unescape_path(url_path)
  raise unless Rack::Utils.valid_path?(url_path)

  Rack::Utils.clean_path_info(url_path)
end