Splat params与浓缩咖啡

时间:2012-12-04 20:59:20

标签: ruby

使用Sinatra,我可以使用以下方法将多个“未知”参数传递给路径:

get '/say/*/to/*' do
  # matches /say/hello/to/world
  params[:splat] # => ["hello", "world"]
end

如何在Espresso中做同样的事情?

1 个答案:

答案 0 :(得分:3)

Espresso中的路线是常规的Ruby方法。

因此,如果该方法适用于Ruby,则路由将在Espresso中运行。

您要实现的目标是Ruby免费提供的。

只需使用预定义的参数定义Ruby方法:

require 'e'

class App < E
  map '/'

  def say greeting = :hello, vertor = :to, subject = :world
    "say #{greeting} #{vertor} #{subject}"
  end
end

# some testing
require 'sonar' # same as rack-test but a bit better
include Sonar

app App # letting Sonar know that app to test

puts get('/say').body
# => say hello to world

puts get('/say/Hi').body
# => say Hi to world

puts get('/say/Hi/from').body
# => say Hi from world

puts get('/say/Hello/from/Espresso').body
# => say Hello from Espresso

puts get('/say/Goodbye/to/Sinatra').body
# => say Goodbye to Sinatra

Working Demo