lib/server.rb in rubrowser-0.1.6 vs lib/server.rb in rubrowser-0.2.0
- old
+ new
@@ -1,37 +1,49 @@
require 'webrick'
require 'data'
-require 'json'
+require 'formatter/json'
require 'erb'
module Rubrowser
class Server < WEBrick::HTTPServer
include ERB::Util
+ ROUTES = {
+ '/' => :root,
+ '/data.json' => :data
+ }
+
def self.start(options = {})
new(options).start
end
def initialize(options)
super Port: options[:port]
+ @files = options[:files]
- @data = Rubrowser::Data.new(options[:files])
- @data.parse
-
mount_proc '/' do |req, res|
- res.body = root(req.path)
+ res.body = router(req.path)
end
-
- trap('INT') { shutdown }
end
private
- attr_reader :data
+ attr_reader :files
- def root(path)
+ def router(path)
return file(path) if file?(path)
+ return send(ROUTES[path]) if ROUTES.key?(path)
+ 'Route not found.'
+ end
+
+ def root
erb :index
+ end
+
+ def data
+ data = Data.new(files)
+ formatter = Formatter::JSON.new(data)
+ formatter.call
end
def file?(path)
path = resolve_file_path("/public#{path}")
File.file?(path)