Class: Artifact::Static

Inherits:
Object
  • Object
show all
Defined in:
lib/artifact/static.rb

Constant Summary collapse

SUFFIXES =
['', '.html', '/index.html']
NOT_FOUND =
'Not found mister.'
DEFAULTS =

for static

{ # for static
  :root      => 'build',
  :urls      => ['/'],
}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Static

Returns a new instance of Static.



15
16
17
18
19
20
21
22
23
# File 'lib/artifact/static.rb', line 15

def initialize(options = {})
  not_found  = options.delete(:not_found) || NOT_FOUND
  text       = IO.read(not_found) if File.exist?(not_found)

  @suffixes  = options.delete(:suffixes)  || SUFFIXES

  @not_found = [404, {"Content-Type" => 'text/html'}, [text]]
  @static    = ::Rack::Static.new( lambda { [404, {}, []] }, DEFAULTS.merge(options))
end

Instance Method Details

#call(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/artifact/static.rb', line 25

def call(env)
  # resp = Rack::Head.call(env)
  # return resp if resp[0] != 404

  found = nil
  @suffixes.each do |suffix|
   url = env['PATH_INFO'] + suffix
   resp = @static.call(env.merge({'PATH_INFO' => url}))
   break if resp[0] != 404 && found = resp
  end

  found or @not_found
end