Class: Snack::Server

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server.



44
45
46
# File 'lib/snack.rb', line 44

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/snack.rb', line 48

def call(env)
  body = render File.join(@app.settings[:root], env['PATH_INFO'])
  if body
    [200, { 'Content-Type' => Rack::Mime.mime_type(File.extname(env['PATH_INFO']), 'text/html') }, [body]]
  else
    [404, { 'Content-Type' => 'text/plain' }, 'Not Found']
  end
end

#render(path) ⇒ Object



57
58
59
60
61
62
63
64
65
66
# File 'lib/snack.rb', line 57

def render(path)
  return File.read path if File.file? path

  # default to index if path to directory
  path = File.join(path, 'index') if Dir.exists? path

  # return the first filename that matches file
  template = Dir[File.join("#{path}*")].first
  return View.new(template).render if template
end