Skip to content

Commit

Permalink
a little extra magic on matching
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Hull committed Apr 26, 2011
1 parent 4b76a9a commit 1c5d6a4
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
12 changes: 5 additions & 7 deletions lib/http_router.rb
Expand Up @@ -71,7 +71,7 @@ def pass_on_response(response)
end

def set_pass_on_response(&blk)
extend(Module.new{define_method(:pass_on_response, &blk)})
extend(Module.new { define_method(:pass_on_response, &blk) })
end

# Adds a path that only responds to the request method +GET+.
Expand Down Expand Up @@ -120,10 +120,10 @@ def call(env, perform_call = true)
else
request = Request.new(rack_request.path_info, rack_request, perform_call)
response = catch(:success) { @root[request] }
if response.nil?
no_response(env, perform_call)
elsif response
if response
response
elsif response.nil?
no_response(env, perform_call)
elsif perform_call
@default_app.call(env)
else
Expand All @@ -134,10 +134,8 @@ def call(env, perform_call = true)

# Resets the router to a clean state.
def reset!
@root = Node.new(self)
@routes, @named_routes, @root = [], {}, Node.new(self)
@default_app = Proc.new{ |env| ::Rack::Response.new("Your request couldn't be found", 404).finish }
@routes = []
@named_routes = {}
end

# Assigns the default application.
Expand Down
1 change: 1 addition & 0 deletions lib/http_router/route.rb
Expand Up @@ -82,6 +82,7 @@ def user_agent(user_agent)
end

def matching(matchers)
matchers = Hash[*matchers] if matchers.is_a?(Array)
@opts.merge!(matchers)
self
end
Expand Down
5 changes: 5 additions & 0 deletions test/test_variable.rb
Expand Up @@ -76,6 +76,11 @@ def test_var_with_optional_format
assert_route r, '/foo', {:test => 'foo'}
end

def test_var_with_optional_format_and_regex
r = router { add('/:test(.:format)', :format => /[^\.]+/) }
assert_route r, '/asd@asd.com.json', {:test => 'asd@asd.com', :format => 'json'}
end

def test_glob
assert_route '/test/*variable', 'test/one/two/three', {:variable => ['one', 'two', 'three']}
end
Expand Down

0 comments on commit 1c5d6a4

Please sign in to comment.