Class: Rack::Deckr

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/deckr.rb

Constant Summary collapse

F =
::File

Instance Method Summary collapse

Constructor Details

#initialize(app, opts = {}) ⇒ Deckr



8
9
10
11
12
# File 'lib/rack/deckr.rb', line 8

def initialize(app, opts={})
  @app = app
  @url = opts[:url] || "/deck"
  @path = opts[:path] || F.join(Dir.pwd, "deck")
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rack/deckr.rb', line 14

def call(env)
  env["deckr.deck"] = ::Deckr::Deck.new(@url, @path)

  path = env["PATH_INFO"]

  return @app.call(env) unless path.start_with?(@url)

  path = Pathname(path).relative_path_from(Pathname(@url))
  path = F.expand_path(F.join(@path, path))

  return @app.call(env) unless path.start_with?(@path) && F.file?(path)

  file = Rack::File.new(@path)
  file.path = path
  file.serving(env)
end