Skip to content

Conversation

dblock
Copy link
Member

@dblock dblock commented Aug 12, 2011

Grape APIs can now be inspected. It now exposes an array of API versions and an array of compiled routes. Each route contains a prefix, version, namespace, method and path.

This allows to build dynamic documentation and stuff like that. The possibilities are endless, the world is ours!

    class TwitterAPI < Grape::API      

      version 'v1'
      get "version" do 
        api.version
      end

      version 'v2'
      namespace "ns" do
        get "version" do
          api.version
        end
      end      

    end

    TwitterAPI::versions # yields [ 'v1', 'v2' ]
    TwitterAPI::routes # yields an array of Grape::Route objects

You can store additional information with routes and inspect it.

@mbleigh
Copy link
Contributor

mbleigh commented Aug 31, 2011

OK, feel free to go ahead and make this happen (it can't be auto-merged).

dblock added a commit that referenced this pull request Aug 31, 2011
Exposing the set of rack routes built by Grape
@dblock dblock merged commit f263d54 into ruby-grape:master Aug 31, 2011
dblock added a commit that referenced this pull request Dec 5, 2011
Exposing the set of rack routes built by Grape
@pboling
Copy link

pboling commented Jul 8, 2014

What happened to the routes method? Is there a current way to accomplish this? Current grape gives me:

undefined methodroutes' for #Rack::Builder:0x0000010c471d70"` when I try to get the routes.

@dblock
Copy link
Member Author

dblock commented Jul 9, 2014

It should be working just fine, but it's a class method on the API.

~/source/grape-on-rails/dblock (master)$ rails c
Acme::Ping.routes
Loading development environment (Rails 4.1.4)
2.0.0-p353 :001 > Acme::Ping.routes
 => [#<Grape::Route:0x007fef55fb72e0 @options={:description=>"Returns pong.", :prefix=>"api", :version=>nil, :namespace=>"/", :method=>"GET", :path=>"/api/ping(.:format)", :params=>{}, :compiled=>/\A\/api\/ping(?:\.(?<format>[^\/.?]+))?\Z/}>] 

@pboling
Copy link

pboling commented Jul 9, 2014

Hmmm... perhaps it only works when grape is mounted in Rails 4? We are on Rails 3.2.18:

$ rails c
> TrumakerAPI::Root.routes
NoMethodError: undefined method `routes' for #<Rack::Builder:0x000001063b06a8>
> TrumakerAPI::Root.class
=> Rack::Builder

@pboling
Copy link

pboling commented Jul 9, 2014

I think the fact that my Grape API class is not a Grape related thing might be the problem.

> TrumakerAPI::Root.class.ancestors
=> [Rack::Builder,
 Object,
 Looksee::ObjectMixin,
 ORI::Extensions::Object,
 Debugging,
 MethodLocator,
 EveryDayIrb,
 InteractiveEditor::Editors,
 FileUtils::Verbose,
 FileUtils,
 FileUtils::StreamUtils_,
 ActionDispatch::TestProcess,
 Metaclass::ObjectMethods,
 Mocha::ObjectMethods,
 PP::ObjectMixin,
 Nori::CoreExt::Object,
 ERB::Util,
 AttrEncryptor::InstanceMethods,
 JSON::Ext::Generator::GeneratorMethods::Object,
 ActiveSupport::Dependencies::Loadable,
 Kernel,
 BasicObject]

@pboling
Copy link

pboling commented Jul 9, 2014

Nevermind, thanks for rubber ducking.

For future googlers, I have this setup:

definition of the class is like this:

module TrumakerAPI
  class Dispatch < Grape::API
    # blah blah
  end

  # Insert the logging app into the middleware stack
  Root = Rack::Builder.new do
    use TrumakerAPI::Logger
    run TrumakerAPI::Dispatch
  end
end

So Root, is what I mount in my app... but Grape is buried inside it.

Trumaker::Application.routes.draw do # |map|
  mount TrumakerAPI::Root => '/kthxbye'

And this does work:

> TrumakerAPI::Dispatch.routes
=> All my goodies.

@dblock
Copy link
Member Author

dblock commented Jul 9, 2014

👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants