Skip to content

Commit

Permalink
Release
Browse files Browse the repository at this point in the history
  • Loading branch information
soveran committed Jan 15, 2016
1 parent dfd6263 commit 94607ab
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
3.5.0

* Add `not_found` hook for customizing the 404 error.

* Remove undocumented `header` matcher.

* Depend explicitly on Rack 1.6.x.

* Experimental feature: `param` now accepts a second parameter
with a default value.

3.4.0

* Add `Cuba::Safe` plugin. This plugin contains security related
Expand Down
2 changes: 1 addition & 1 deletion cuba.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "cuba"
s.version = "3.4.0"
s.version = "3.5.0"
s.summary = "Microframework for web applications."
s.description = "Cuba is a microframework for web applications."
s.authors = ["Michel Martens"]
Expand Down
16 changes: 10 additions & 6 deletions lib/cuba.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,24 @@ def extension(ext = "\\w+")
lambda { consume("([^\\/]+?)\.#{ext}\\z") }
end

# Used to ensure that certain request parameters are present. Acts like a
# precondition / assertion for your route.
# Ensures that certain request parameters are present. Acts like a
# precondition / assertion for your route. A default value can be
# provided as a second argument. In that case, it always matches
# and the result is either the parameter or the default value.
#
# @example
# # POST with data like user[fname]=John&user[lname]=Doe
# on "signup", param("user") do |atts|
# User.create(atts)
# end
#
# on "login", param("username", "guest") do |username|
# # If not provided, username == "guest"
# end
def param(key, default = nil)
lambda { captures << req[key] unless req[key].to_s.empty? }
end
value = req[key] || default

def header(key)
lambda { env[key.upcase.tr("-","_")] }
lambda { captures << value unless value.to_s.empty? }
end

# Useful for matching against the request host (i.e. HTTP_HOST).
Expand Down
22 changes: 22 additions & 0 deletions test/param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
res.write email
end

on get, "login", param("username", "guest") do |username|
res.write username
end

on default do
res.write "No email"
end
Expand Down Expand Up @@ -42,3 +46,21 @@

assert_response resp, ["No email"]
end

test "yields a default param" do
env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/login",
"SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
"QUERY_STRING" => "username=john" }

_, _, resp = Cuba.call(env)

assert_response resp, ["john"]

env = { "REQUEST_METHOD" => "GET", "PATH_INFO" => "/login",
"SCRIPT_NAME" => "/", "rack.input" => StringIO.new,
"QUERY_STRING" => "" }

_, _, resp = Cuba.call(env)

assert_response resp, ["guest"]
end

0 comments on commit 94607ab

Please sign in to comment.