Hi,
I was wondering if there was a way to match all incoming request for a specific route ?
desc 'blablablabla'
params do
requires :service, type: String, desc: 'Service you are looking for'
requires :path, type: String, desc: 'Path you are looking for', regexp: /.+/
end
get ':service/:path' do
puts params['service']
puts params['path']
end
I want to be able to get the first variable called service then path should be able to match everything else like :
So I could hit this route using the URL * http://localhost:40117/api/v1/search/unicorn/10 * where service will be "search" and path "unicorn/10".
Thanks in advance,
To make :path optional, you need to include it in parenthesis.
To include slashes, you need to change requirements, see https://github.com/ruby-grape/grape#routes.
Here's a quick test:
require 'spec_helper'
require 'shared/versioning_examples'
require 'grape-entity'
describe Grape::API do
subject { Class.new(Grape::API) }
def app
subject
end
it 'matches everything' do
subject.format :json
subject.get ':service(/:path)', requirements: { path: /.*/ } do
{
service: params[:service],
path: params[:path],
query: params[:query]
}
end
get 'blablabla'
puts JSON.parse(last_response.body)
get 'blablablabla/blablablabla/blablablbla'
puts JSON.parse(last_response.body)
get 'blablablabla/blablablabla?query=toto'
puts JSON.parse(last_response.body)
end
end
{"service"=>"blablabla", "path"=>nil, "query"=>nil}
{"service"=>"blablablabla", "path"=>"blablablabla/blablablbla", "query"=>nil}
{"service"=>"blablablabla", "path"=>"blablablabla", "query"=>"toto"}
I'll close this. Please use the Grape mailing list for questions.
Most helpful comment
To make
:pathoptional, you need to include it in parenthesis.To include slashes, you need to change
requirements, see https://github.com/ruby-grape/grape#routes.Here's a quick test: