grape-raketasks icon indicating copy to clipboard operation
grape-raketasks copied to clipboard

Formatting

Open pboling opened this issue 11 years ago • 4 comments

The current formatting is horrible (say it with a Spanish accent, please!).

This is what I have hacked up.

In the style of rails routes... (nicely formatted)

namespace :grape do
  desc "Condensed API Routes"
  task :routes => :environment do
    mapped_prefix = '/whatevs' # where mounted in routes.rb
    format = "%46s  %3s %7s %50s %12s:  %s"
    API.routes.each do |grape_route|
      info = grape_route.instance_variable_get :@options
      puts format % [
        info[:description] ? info[:description][0..45] : '',
        info[:version],
        info[:method],
        mapped_prefix + info[:path],
        '# params: ' + info[:params].length.to_s,
        info[:params].first.inspect
      ]
      if info[:params].length > 1
        info[:params].each_with_index do |param_info, index|
          next if index == 0
          puts format % ['','','','','',param_info.inspect]
        end
      end
    end
  end
end

pboling avatar Jul 09 '14 21:07 pboling

@pboling, I suspect those are words I'm more likely to hear in a French accent!

Yeah, the printed routes aren't aesthetically pleasing. Someone else asked me about this in Issue #4 and I explained why I didn't do the horizontally-spaced rails routes style.

I'm open to pull requests. I want this gem to be functional and nice and made by more people than just me. Right now there's a bug in it related to Active Support's #subclasses method that I discovered a few weeks ago (not all classes that actually subclass from Grape::API are shown), so that's what I'm working on.

reprah avatar Jul 12 '14 14:07 reprah

@reprah So you want a real pull request? I think I can manage that. Perhaps different formats can be options?

Question... if you just iterate through #routes from grape how can it have anything to do with ActiveSupport?

pboling avatar Jul 14 '14 18:07 pboling

It's just because ActiveSupport defines the subclasses method on classes, and there are two reasons I want to use this:

1.) To dynamically figure out which APIs a person has defined (which I currently do by calling Grape::API.subclasses) and make sure every route from every API is printed 2.) To give people the option of filtering routes by the API

For example, in your script above, it looks like it's printing the routes for one API, but there are applications that have multiple Grape APIs and I needed a way to determine what those are. Does that make sense?

reprah avatar Jul 20 '14 21:07 reprah

Here's the formatting thing I was referencing when someone with limited screen space (laptop, Tmux panes) uses Rails-style horizontal route display (plus the current state of affairs as the first image for comparison): Image. See how they all wrap to the next line and it's harder to make sense of?

Yeah I'm open to a pull request with tests. Thank you for even offering. If this gem has had four users (maybe...) and two of them thought the display of routes was enough of a problem to create an issue for, then it's a problem. Yes an option for how to display the routes would be nice.

reprah avatar Jul 20 '14 22:07 reprah