Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Extract URL mapping to a Router class (changing Handler constructor a…

…long the way)
  • Loading branch information...
commit a48d732de12823d5affe4d8515fe24e9346df6f0 1 parent 84883ae
@ttencate authored
View
1  lib/blimp.rb
@@ -23,6 +23,7 @@
require_relative "blimp/page"
require_relative "blimp/static"
require_relative "blimp/renderer"
+require_relative "blimp/router"
require_relative "blimp/web_server"
View
3  lib/blimp/handler.rb
@@ -14,8 +14,7 @@ def find_by_name(name)
end
end
- def initialize(source, path)
- @source = source
+ def initialize(path)
@path = path
end
end
View
4 lib/blimp/handlers/static_handler.rb
@@ -3,8 +3,8 @@ module Handlers
class StaticHandler < Blimp::Handler
@name = "static"
- def handle(path, params = {})
- resource = Static.from_path(path, @source)
+ def handle(source, path, params = {})
+ resource = Static.from_path(path, source)
headers = {"Content-Type" => resource.mimetype}
body = resource.contents
View
29 lib/blimp/router.rb
@@ -0,0 +1,29 @@
+class Router
+ def initialize(handler_config = [])
+ @handlers = { "/" => [
+ Blimp::Handlers::PageHandler.new("/"),
+ Blimp::Handlers::StaticHandler.new("/"),
+ ] }
+ for handler_config in handler_config || [] do
+ path = handler_config[:path]
+ names = handler_config[:handler]
+ names = [names] if not names.is_a?(Array)
+ handlers[path] = names.map {|name| Blimp::Handler.find_by_name(name).new(path) }
+ end
+ end
+
+ def handlers_for_path(path)
+ raise ArgumentError, "Path #{path} does not start with a slash" if not path.start_with?("/")
+ handler_list = []
+ while not path == "/"
+ handler_list += handlers[path] || []
+ path = File.dirname(path)
+ end
+ handler_list += handlers[path] # now /
+ return handler_list
+ end
+
+ protected
+
+ attr_reader :handlers
+end
View
28 lib/blimp/site.rb
@@ -9,7 +9,6 @@ class NoHandler < StandardError; end
attr_reader :source
attr_reader :theme
attr_reader :domains
- attr_reader :handlers
liquid_methods :title
@@ -21,28 +20,11 @@ def initialize(key, source, options = {})
@theme = Theme.new(source, "/templates")
config = load_config
-
- @handlers = { "/" => [
- Blimp::Handlers::PageHandler.new(source, "/"),
- Blimp::Handlers::StaticHandler.new(source, "/"),
- ] }
- for handler_config in config[:handlers] || [] do
- path = handler_config[:path]
- names = handler_config[:handler]
- names = [names] if not names.is_a?(Array)
- handlers[path] = names.map {|name| Blimp::Handler.find_by_name(name).new(source, path) }
- end
+ @router = Router.new(config["handlers"])
end
- def handlers_for_path(path)
- raise ArgumentError, "Path #{path} does not start with a slash" if not path.start_with?("/")
- handler_list = []
- while not path == "/"
- handler_list += handlers[path] || []
- path = File.dirname(path)
- end
- handler_list += handlers[path] # now /
- return handler_list
+ def handler_for_path(path)
+ router.handler_for_path(path)
end
def find_page(path)
@@ -57,6 +39,10 @@ def has_domain?(domain)
domains.include?(domain)
end
+ protected
+
+ attr_reader :router
+
private
def load_config
View
2  lib/blimp/web_server.rb
@@ -18,7 +18,7 @@ class WebServer < Sinatra::Base
get '*' do
handlers = @site.handlers_for_path(request.path_info)
# TODO try them one by one, raise Sinatra::NotFound if all fail
- response_headers, response_body = handlers[0].handle(request.path_info, params)
+ response_headers, response_body = handlers[0].handle(site.source, request.path_info, params)
headers response_headers
body response_body
View
6 spec/blimp/handlers/static_handler_spec.rb
@@ -2,11 +2,11 @@
describe Blimp::Handlers::StaticHandler do
let(:source) { Blimp::Sources::FakeSource.new({ "image.jpg" => "A kitten" }) }
- let(:handler) { Blimp::Handlers::StaticHandler.new(source, "/") }
+ let(:handler) { Blimp::Handlers::StaticHandler.new("/") }
describe "#handle" do
- let(:headers) { handler.handle("/image.jpg")[0] }
- let(:body) { handler.handle("/image.jpg")[1] }
+ let(:headers) { handler.handle(source, "/image.jpg")[0] }
+ let(:body) { handler.handle(source, "/image.jpg")[1] }
it "returns the contents for the path" do
body.should == "A kitten"
View
38 spec/blimp/router_spec.rb
@@ -0,0 +1,38 @@
+require 'spec_helper'
+
+describe Router do
+ describe "#handlers_for_path" do
+ context "without any handlers configured" do
+ let(:router) { Router.new }
+
+ it "should return the default handlers for the root" do
+ router.handlers_for_path("/").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
+ end
+
+ it "should return the default handlers for a subdir" do
+ router.handlers_for_path("/subdir").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
+ end
+ end
+
+ context "with some handlers configured" do
+ let(:router) { Router.new([{ :path => "/", :handler => "static" }, { :path => "/page", :handler => "page" }]) }
+
+ it "should return handlers for the root" do
+ router.handlers_for_path("/").should have_types([Blimp::Handlers::StaticHandler])
+ end
+
+ it "should return configured handlers for subdirectories" do
+ router.handlers_for_path("/page").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
+ end
+
+ it "should fall back the handler of parent directories" do
+ router.handlers_for_path("/foo").should have_types([Blimp::Handlers::StaticHandler])
+ router.handlers_for_path("/page/foo").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
+ end
+
+ it "should not match prefixes incorrectly" do
+ router.handlers_for_path("/page_with_suffix").should have_types([Blimp::Handlers::StaticHandler])
+ end
+ end
+ end
+end
View
38 spec/blimp/site_spec.rb
@@ -25,44 +25,6 @@
end
end
- describe "#get_handler" do
- context "without any handlers configured" do
- let(:source) { Blimp::Sources::FakeSource.new({ "_blimp.yaml" => "handlers:" }) }
- let(:site) { Site.new("my-site", source) }
-
- it "should return the default handlers for the root" do
- site.handlers_for_path("/").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
- end
-
- it "should return the default handlers for a subdir" do
- site.handlers_for_path("/subdir").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
- end
- end
-
- context "with some handlers configured" do
- let(:source) { Blimp::Sources::FakeSource.new(
- { "_blimp.yaml" => "handlers:\n- path: /\n handler: static\n- path: /page\n handler: page" }) }
- let(:site) { Site.new("my-site", source) }
-
- it "should return handlers for the root" do
- site.handlers_for_path("/").should have_types([Blimp::Handlers::StaticHandler])
- end
-
- it "should return configured handlers for subdirectories" do
- site.handlers_for_path("/page").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
- end
-
- it "should fall back the handler of parent directories" do
- site.handlers_for_path("/foo").should have_types([Blimp::Handlers::StaticHandler])
- site.handlers_for_path("/page/foo").should have_types([Blimp::Handlers::PageHandler, Blimp::Handlers::StaticHandler])
- end
-
- it "should not match prefixes incorrectly" do
- site.handlers_for_path("/page_with_suffix").should have_types([Blimp::Handlers::StaticHandler])
- end
- end
- end
-
describe "#find_page" do
it "should find a page" do
path = stub
Please sign in to comment.
Something went wrong with that request. Please try again.