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.

Raises:

  • (ArgumentError)


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 ArgumentError.new('You need to define an :engine class') unless @engine_klass
  raise ArgumentError.new('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
40
41
42
43
44
45
46
# File 'lib/fewer/app.rb', line 26

def call(env)
  eng = engine(sources_from_path(env['PATH_INFO']))

  if env["HTTP_IF_NONE_MATCH"] && env["HTTP_IF_NONE_MATCH"] == eng.etag
    Fewer.logger.debug "Fewer: returning 304 not modified"
    [304, {}, []]
  else
    headers = {
      'Content-Type' => engine_klass.content_type || 'text/plain',
      'Cache-Control' => "public, max-age=#{cache}",
      'Last-Modified' => eng.mtime.rfc2822,
      'ETag' => eng.etag
    }

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

#engine(names) ⇒ Object



48
49
50
# File 'lib/fewer/app.rb', line 48

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