Class: Fewer::App

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of App.



15
16
17
18
19
20
21
22
23
24
# File 'lib/fewer/app.rb', line 15

def initialize(name, options = {})
  @engine_klass = options[:engine]
  @engine_options = options[:engine_options] || {}
  @mount = options[:mount]
  @root = options[:root]
  @cache = options[:cache] || 3600 * 24 * 365
  raise 'You need to define an :engine class' unless @engine_klass
  raise 'You need to define a :root path' unless @root
  self.class.apps[name] = self
end

Instance Attribute Details

#cacheObject (readonly)

Returns the value of attribute cache.



13
14
15
# File 'lib/fewer/app.rb', line 13

def cache
  @cache
end

#engine_klassObject (readonly)

Returns the value of attribute engine_klass.



13
14
15
# File 'lib/fewer/app.rb', line 13

def engine_klass
  @engine_klass
end

#engine_optionsObject (readonly)

Returns the value of attribute engine_options.



13
14
15
# File 'lib/fewer/app.rb', line 13

def engine_options
  @engine_options
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/fewer/app.rb', line 13

def name
  @name
end

#rootObject (readonly)

Returns the value of attribute root.



13
14
15
# File 'lib/fewer/app.rb', line 13

def root
  @root
end

Class Method Details

.[](name) ⇒ Object



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

def [](name)
  apps[name]
end

.appsObject



8
9
10
# File 'lib/fewer/app.rb', line 8

def apps
  @apps ||= {}
end

Instance Method Details

#call(env) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fewer/app.rb', line 26

def call(env)
  eng = engine(names_from_path(env['PATH_INFO']))
  headers = {
    'Content-Type' => eng.content_type,
    'Cache-Control' => "public, max-age=#{cache}",
    'Last-Modified' => eng.mtime.rfc2822
  }

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

#engine(names) ⇒ Object



41
42
43
# File 'lib/fewer/app.rb', line 41

def engine(names)
  engine_klass.new(root, names, engine_options)
end