Class: Bunch::Rack

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

Instance Method Summary collapse

Constructor Details

#initialize(path, opts = {}) ⇒ Rack

Returns a new instance of Rack.



3
4
5
6
7
8
9
10
11
12
13
14
# File 'lib/bunch/rack.rb', line 3

def initialize(path, opts={})
  @root = Pathname.new(path)
  @headers = {}

  if opts.delete(:no_cache)
    @headers['Cache-Control'] = 'private, max-age=0, must-revalidate'
    @headers['Pragma'] = 'no-cache'
    @headers['Expires'] = 'Thu, 01 Dec 1994 16:00:00 GMT'
  end

  Bunch.load_ignores(@root)
end

Instance Method Details

#call(env) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/bunch/rack.rb', line 16

def call(env)
  path = @root.join(env['PATH_INFO'].sub(/^\//, '')).to_s
  type = MIME::Types.type_for(path).first || 'text/plain'

  [200, headers(type), [content_for(path)]]
rescue => e
  [500, headers('text/plain'), [error_log(e)]]
end

#content_for(path) ⇒ Object (private)



26
27
28
# File 'lib/bunch/rack.rb', line 26

def content_for(path)
  Bunch.content_for(path, :root => @root)
end

#error_log(e) ⇒ Object (private)



34
35
36
# File 'lib/bunch/rack.rb', line 34

def error_log(e)
  "#{e.class}: #{e.message}\n  #{e.backtrace.join("\n  ")}"
end

#headers(mime_type) ⇒ Object (private)



30
31
32
# File 'lib/bunch/rack.rb', line 30

def headers(mime_type)
  @headers.merge('Content-Type' => mime_type.to_s)
end