Code Monkey home page Code Monkey logo

grape-rails-routes's Introduction

grape-rails-routes

After you mount Grape API routes into Rails, Grape API routes would normally not be printed in rake routes or /rails/info/routes.

This gem adds routes printing functionalities for Grape into Rails.

Usage

Add this line to your Gemfile

    gem 'grape-rails-routes'

Then review your routes via

    rake routes_with_grape

Or (recommended) access

http://localhost:3000/rails/info/routes_with_grape

to get a HTML table view of all your routes.

Enjoy :P

grape-rails-routes's People

Contributors

pmq20 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

grape-rails-routes's Issues

Gem clobbers helpers_path

As per: rails/rails#2739

It appears grape-rails-routes is calling some initialization on Rails that clobbers helpers_path which has the effect of making any helper that is not directly related to a controller no longer available.

I believe in rails v4+ they changed to including all helpers all the time.

I experienced this on rails v4.1.16

To reproduce:

  • Create a new rails app on v4.1.16
  • Add grape-rails-routes gem
  • Create a controller/view
  • Add a helper with method that isn't paired to controller
  • Call helper method from view

Undefined method `ast` String

NoMethodError: undefined method `ast' for "/api/:version/ping(.json)":String

I use 1.0 version in my Gemfile.lock

Doesn't work with ActionPack 4.2.0

Set this up on a new project using Rails 4.2.0, and when running rake routes_with_grape it fails with

undefined method `ast' for "/api/game/:slug":String

It looks like ActionDispatch::Journey::Path::Pattern.new is looking for an expression of some kind. I will see if I can dig into this to get it working.

Creating unnecessary routes?

I have question regarding the implementation. I have quite deep folder structure in my rails project:

    ├── app
    │   ├── controllers
    │   │   ├── api
    │   │   │   ├── base.rb
    │   │   │   └── v2
    │   │   │       ├── api.rb
    │   │   │       ├── defaults.rb
    │   │   │       └── users.rb

I basically followed this tutorial: http://codetunes.com/2014/introduction-to-building-apis-with-grape/
and running into some issues/weirdness with routes, etc. When I install your gem, I got following results of rake routes_with_grape in my application:

                Prefix Verb    URI Pattern                           Controller#Action
              api_base         /api                                  API::Base
Return list of hussars GET     /:version/hussars(.:format)           API::V1::Hussars
                       OPTIONS /:version/hussars(.:format)(.:format) API::V1::Hussars
                       PUT     /:version/hussars(.:format)(.:format) API::V1::Hussars
                       POST    /:version/hussars(.:format)(.:format) API::V1::Hussars
                       DELETE  /:version/hussars(.:format)(.:format) API::V1::Hussars
                       PATCH   /:version/hussars(.:format)(.:format) API::V1::Hussars
Return list of hussars GET     /:version/hussars(.:format)           API::V1::Base
                       OPTIONS /:version/hussars(.:format)(.:format) API::V1::Base
                       PUT     /:version/hussars(.:format)(.:format) API::V1::Base
                       POST    /:version/hussars(.:format)(.:format) API::V1::Base
                       DELETE  /:version/hussars(.:format)(.:format) API::V1::Base
                       PATCH   /:version/hussars(.:format)(.:format) API::V1::Base
Return list of hussars GET     /:version/hussars(.:format)           API::Base

Q1: How do I avoid double listing the API? I know that this line:
grape_klasses = ObjectSpace.each_object(Class).select { |klass| klass < Grape::API }
is returning following array:

[API::V1::Hussars, API::V1::Base, API::Base, API::V1::Hussars, API::V1::Base, API::Base] 

and when I compile these paths by klass.compile I got several routes:

version=v1, method=GET, path=/:version/hussars(.:format)
version=, method=OPTIONS, path=/:version/hussars(.:format)(.:format)
version=, method=PUT, path=/:version/hussars(.:format)(.:format)
version=, method=POST, path=/:version/hussars(.:format)(.:format)
version=, method=DELETE, path=/:version/hussars(.:format)(.:format)
version=, method=PATCH, path=/:version/hussars(.:format)(.:format)
version=v1, method=GET, path=/:version/hussars(.:format)
version=, method=OPTIONS, path=/:version/hussars(.:format)(.:format)
version=, method=PUT, path=/:version/hussars(.:format)(.:format)
version=, method=POST, path=/:version/hussars(.:format)(.:format)
version=, method=DELETE, path=/:version/hussars(.:format)(.:format)
version=, method=PATCH, path=/:version/hussars(.:format)(.:format)
version=v1, method=GET, path=/:version/hussars(.:format)
version=v1, method=GET, path=/:version/hussars(.:format)
version=, method=OPTIONS, path=/:version/hussars(.:format)(.:format)
version=, method=PUT, path=/:version/hussars(.:format)(.:format)
version=, method=POST, path=/:version/hussars(.:format)(.:format)
version=, method=DELETE, path=/:version/hussars(.:format)(.:format)
version=, method=PATCH, path=/:version/hussars(.:format)(.:format)
version=v1, method=GET, path=/:version/hussars(.:format)
version=, method=OPTIONS, path=/:version/hussars(.:format)(.:format)
version=, method=PUT, path=/:version/hussars(.:format)(.:format)
version=, method=POST, path=/:version/hussars(.:format)(.:format)
version=, method=DELETE, path=/:version/hussars(.:format)(.:format)
version=, method=PATCH, path=/:version/hussars(.:format)(.:format)
version=v1, method=GET, path=/:version/hussars(.:format)

As the code follows with lines:

path = ActionDispatch::Journey::Path::Pattern.new route.route_path
      all_routes.add_route(klass, path, {
        request_method: %r{^#{route.route_method}$}
      }, {}, route.route_description)

I'm not exactly sure what does ActionDispatch::Journey::Path::Pattern.new do, but I guess that it is creating new paths in the application. Am I right? So it's adding unnecessary path to the app, because I have files such as:

module API
  class Base < Grape::API
    mount API::V1::Base
  end
end

Would you help me just troubleshoot this?

I'm also interested why in routes listing I got following lines(example):

version=, method=POST, path=/:version/hussars(.:format)(.:format)

Why is there twice (.:format)?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.