Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splat params with Espresso

Tags:

ruby

With Sinatra i can pass multiple "unknown" params to a route by using:

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

How to do the same in Espresso?

like image 559
Nathan Muller Avatar asked Feb 04 '26 22:02

Nathan Muller


1 Answers

Routes in Espresso are regular Ruby methods.

So if the method works in Ruby, the route will work in Espresso.

What you are trying to achieve is offered by Ruby for free.

Just define a Ruby method with predefined arguments:

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