Apipie / apipie-rails

Ruby on Rails API documentation tool
Apache License 2.0
2.47k stars 459 forks source link

URL example generation (Feature request) #542

Open cshupp1 opened 7 years ago

cshupp1 commented 7 years ago

If you already support this then sorry for this...

I am using apipie version 0.5.1.

I want to be able to support example URL generation in my docs, something like this:

  api :GET, "#{$API_PIE_ROUTES[:roles_get_ssoi_roles_path]}.json", 'Request all roles for a VA Single Sign on User user as JSON.'
  api :GET, "#{$API_PIE_ROUTES[:roles_get_ssoi_roles_path]}", 'Request all roles for a VA Single Sign on User user as HTML.'
  description %Q{
Returns all the roles for a user

sample invocation JSON:  #{$API_PIE_ROUTES[:roles_get_ssoi_roles_url]}.json?#{CGI.escape({id: :cboden}.to_query)}

sample invocation HTML:  #{$API_PIE_ROUTES[:roles_get_ssoi_roles_url]}?#{CGI.escape({id: :cboden}.to_query)}
}
  def get_ssoi_roles
#impl
end

In order to implement this for our project to the controllers directory I added your apipie/apipis_controller.rb to our project and made the following changes:

module Apipie
  class ApipiesController < ApplicationController #inherit from our app to have the routes
    include ActionView::Context
    include ApipieHelper

    layout Apipie.configuration.layout
    skip_after_action :verify_authorized #trun off our pundit security

    prepend_before_action :set_urls #feed us the routes
    around_action :set_script_name
    before_action :authenticate

    def set_urls
      if $API_PIE_ROUTES.nil? #We will assume no dynamic routes
        $API_PIE_ROUTES = {}
        Rails.application.routes.named_routes.helpers.to_a.each do |url_or_path|
          $API_PIE_ROUTES[url_or_path] = self.send(url_or_path).to_s rescue nil
        end
      end
    end

But you guys can probably do it better.

Thanks,

Cris

cshupp1 commented 7 years ago

Sigh

My approach doesn't work in production mode. It seems the apis are only built once at a time when my hash isn't populated...