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.



46
47
48
# File 'lib/snack.rb', line 46

def initialize(app)
  @app = app
end

Instance Method Details

#call(env) ⇒ Object



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

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



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

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