Mike Bevz
Mike Bevz

Reputation: 1266

Rails Grape-Swagger: get current domain for base_path

I've got the following Grape root class definition

module Api
  class Root < Grape::API
    mount Api::Version1



    add_swagger_documentation :mount_path => 'docs', 
                              :base_path => "/api", 
                              :markdown => true, 
                              :hide_documentation_path => true
  end
end

The problem is that ":base_path" should include a full URL, and I don't know where to get it. I've checked ENV variable, and there is nothing close to the current domain. There is also no "request" object of any kind. Is there any way in Ruby I can just get the current domain, ie HTTP_HOST variable?

Upvotes: 4

Views: 1365

Answers (2)

Yaro Holodiuk
Yaro Holodiuk

Reputation: 588

OK, for anyone wandering here after all these years (like I did), - this is the solution that worked for me:

add_swagger_documentation base_path: lambda { |request| "http://#{request.host}:#{request.port}" }
                          ... #other options

Basically you just have to pass a proc that yields Grape::Request instance, from which you can pull the host and port data.

Upvotes: 6

Eric Himmelreich
Eric Himmelreich

Reputation: 437

I've had decent luck using http://localhost:9292/api. I think another alternative would be to keep these settings in a config yml file, and you could possibly set that based on RACK_ENV's value.

Upvotes: 0

Related Questions