Permalink
Browse files

Paths can be (ruby) Regex objects.

  • Loading branch information...
1 parent 561bd20 commit 4369ec2b39148c2aa183d2f8fb4eefdc07df793b @timothyandrew committed Nov 5, 2011
Showing with 40 additions and 6 deletions.
  1. +4 −0 lib/instadroid-server/app.rb
  2. +8 −5 lib/instadroid-server/framework.rb
  3. +28 −1 spec/framework_spec.rb
@@ -23,6 +23,10 @@ def create_routes
[200, {"Content-Type" => "text/html"}, @engine.render(Object.new, haml_vars)]
end
+ get /\/public\/?.*\/abc/ do |env|
+ [200, {"Content-Type" => "text/html"}, "Public"]
+ end
+
get :default do |env|
[200, {"Content-Type" => "text/html"}, env.to_s]
end
@@ -21,14 +21,17 @@ def call(env)
@path_hashes[method][path].call env
rescue NoMethodError
begin
- [200, {"Content-Type"=>"text/html"}, File.new(Dir.pwd+path, "r").read]
+ @path_hashes[method].select { |k,v| k.class == Regexp and k.match(path) }.values.first.call env
rescue
begin
- @path_hashes[method][:default].call env
- rescue NoMethodError
- [404, {"Content-Type"=>"text/html"}, @path_hashes.to_s]
+ [200, {"Content-Type"=>"text/html"}, File.new(Dir.pwd+path, "r").read]
+ rescue
+ begin
+ @path_hashes[method][:default].call env
+ rescue
+ [404, {"Content-Type"=>"text/html"}, @path_hashes.to_s]
+ end
end
-
end
end
end
View
@@ -1,12 +1,39 @@
require './lib/instadroid-server/framework'
describe Framework do
- it "must store paths mapped to code-blocks" do
+ it "maps paths to code-blocks directly" do
f = Framework.new
f.path_hashes.should be_kind_of(Hash)
f.path_hashes.values.all? {|hash| hash.should be_kind_of(Hash) }
end
+ it "maps paths to code-blocks using regexes" do
+ f = Framework.new
+
+ block = Proc.new {
+ "foo"
+ }
+
+ f.get /\/public\/.*/, &block
+ opts = {"SERVER_SOFTWARE"=>"Apache/2.2.20 (Unix) DAV/2 Phusion_Passenger/3.0.9",
+ "SERVER_PROTOCOL"=>"HTTP/1.1",
+ "REQUEST_METHOD"=>"GET",
+ "PATH_INFO" => "/public/",
+ "QUERY_STRING" => "a=foo&b=bar"
+ }
+
+ resp = f.call(opts)
+ resp.should == "foo"
+
+ opts["PATH_INFO"] = "/public/bar"
+ resp = f.call(opts)
+ resp.should == "foo"
+
+ opts["PATH_INFO"] = "/public/bar/baz"
+ resp = f.call(opts)
+ resp.should == "foo"
+ end
+
it "must store GET & POST paths separately" do
f = Framework.new
f.path_hashes.should have_exactly(2).items

0 comments on commit 4369ec2

Please sign in to comment.