Grape: Match all request on resources

Created on 11 Feb 2017  路  2Comments  路  Source: ruby-grape/grape

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 :

  • blablabla
  • blablablabla/blablablabla/blablablbla
  • blablablabla/blablablabla?query=toto

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,

Most helpful comment

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"}

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Legogris picture Legogris  路  6Comments

davidmccoy picture davidmccoy  路  3Comments

nab0310 picture nab0310  路  4Comments

dblock picture dblock  路  6Comments

stabenfeldt picture stabenfeldt  路  6Comments