Class: Fewer::App

Inherits:
Object
  • Object
show all
Defined in:
lib/fewer/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of App.



5
6
7
8
9
10
11
# File 'lib/fewer/app.rb', line 5

def initialize(app, options = {})
  @app = app
  @engine_klass = options[:engine]
  @mount = options[:mount]
  @root = options[:root]
  @cache = options[:cache] || 3600 * 24 * 365
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/fewer/app.rb', line 3

def app
  @app
end

#cacheObject (readonly)

Returns the value of attribute cache.



3
4
5
# File 'lib/fewer/app.rb', line 3

def cache
  @cache
end

#engine_klassObject (readonly)

Returns the value of attribute engine_klass.



3
4
5
# File 'lib/fewer/app.rb', line 3

def engine_klass
  @engine_klass
end

#mountObject (readonly)

Returns the value of attribute mount.



3
4
5
# File 'lib/fewer/app.rb', line 3

def mount
  @mount
end

#rootObject (readonly)

Returns the value of attribute root.



3
4
5
# File 'lib/fewer/app.rb', line 3

def root
  @root
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  return app.call(env) unless env['PATH_INFO'] =~ /^#{mount}/

  names = names_from_path(env['PATH_INFO'])
  engine = engine_klass.new(root, names)
  headers = {
    'Content-Type' => engine.content_type,
    'Cache-Control' => "public, max-age=#{cache}"
  }

  [200, headers, [engine.read]]
rescue Fewer::MissingSourceFileError => e
  [404, { 'Content-Type' => 'text/plain' }, [e.message]]
rescue => e
  [500, { 'Content-Type' => 'text/plain' }, ["#{e.class}: #{e.message}"]]
end