Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added support for missing definitions

  • Loading branch information...
commit 36ff3755be4f6e60be000b1441c7cf86c5208b2f 1 parent e4211e6
@fredwu authored
View
16 README.md
@@ -2,7 +2,7 @@
A quick and easy way to visually test out your Rails application's API.
-![](http://i.imgur.com/ryjOH.png)
+![](http://i.imgur.com/8Dnto.png)
## Why?
@@ -67,19 +67,23 @@ if Rails.env.development?
end
```
-That's it! Enjoy! :)
-
-## Share Params with Test Factories
+### Share Params with Test Factories
If you use a test factory such as [FactoryGirl](https://github.com/thoughtbot/factory_girl), you can require your test factories and share the params. For example in FactoryGirl you can use the `attributes_for(:name_of_factory)` method.
-## Obsolete / Mismatched Route Definitions Detection
+### Missing Route Definitions Detection
+
+Instead of manually finding out which route definitions you need, API Taster provides a warning page that shows you all the missing definitions.
+
+![](http://i.imgur.com/vZb93.png)
+
+### Obsolete / Mismatched Route Definitions Detection
APIs evolve - especially during the development stage. To keep `ApiTaster.routes` in sync with your route definitions, API Taster provides a warning page that shows you the definitions that are obsolete/mismatched therefore you could correct or remove them.
![](http://i.imgur.com/Fo7kQ.png)
-## Use with an Engine
+### Use with an Engine
Rails Engines are largely self contained and separated from your main app. Therefore, to use API Taster with an Engine, you would need some extra efforts:
View
5 app/controllers/api_taster/routes_controller.rb
@@ -2,6 +2,7 @@ module ApiTaster
class RoutesController < ApplicationController
def index
@routes = Route.grouped_routes
+ @has_missing_definitions = Route.missing_definitions.present?
@has_obsolete_definitions = Route.obsolete_definitions.present?
end
@@ -10,6 +11,10 @@ def show
@inputs = Route.inputs_for(@route)
end
+ def missing_definitions
+ @missing_definitions = Route.missing_definitions
+ end
+
def obsolete_definitions
@obsolete_definitions = Route.obsolete_definitions
end
View
8 app/views/api_taster/routes/_information_warning_li.html.erb
@@ -0,0 +1,8 @@
+<li>
+ <a href="<%= path %>">
+ <div class="label-api">
+ <span class="label label-warning">Warning</span>
+ </div>
+ <%= text %>
+ </a>
+</li>
View
7 app/views/api_taster/routes/_route_definitions.html.erb
@@ -0,0 +1,7 @@
+<pre class="prettyprint lang-rb">ApiTaster.routes do
+
+ <% routes.each do |route| %>
+ <%= route[:verb].downcase %> '<%= route[:path] %>'<%= ", #{route[:params]}" if route[:params].present? %>
+
+ <% end %>
+end</pre>
View
22 app/views/api_taster/routes/index.html.erb
@@ -2,16 +2,20 @@
<div id="list-api-div" class="span5">
<div class="div-container">
<ul class="well nav nav-list">
- <% if @has_obsolete_definitions %>
+ <% if @has_missing_definitions || @has_obsolete_definitions %>
<li class="nav-header">Information</li>
- <li>
- <a href="<%= obsolete_definitions_routes_path %>">
- <div class="label-api">
- <span class="label label-warning">Warning</span>
- </div>
- Obsolete Definitions Detected
- </a>
- </li>
+ <% if @has_missing_definitions %>
+ <%= render 'information_warning_li',
+ :text => 'Missing Definitions Detected',
+ :path => missing_definitions_routes_path
+ %>
+ <% end %>
+ <% if @has_obsolete_definitions %>
+ <%= render 'information_warning_li',
+ :text => 'Obsolete Definitions Detected',
+ :path => obsolete_definitions_routes_path
+ %>
+ <% end %>
<% end %>
<% @routes.each do |controller, routes| %>
<li class="nav-header"><%= controller %></li>
View
12 app/views/api_taster/routes/missing_definitions.html.erb
@@ -0,0 +1,12 @@
+<%= render 'breadcrumb',
+ :parent => 'Information',
+ :current => 'Missing Definitions Detected',
+ :label => 'Warning',
+ :label_type => 'warning'
+%>
+
+<div class="alert alert-warning">
+ <p>The following route definitions are missing from <code>ApiTaster.routes</code>.</p>
+
+ <%= render 'route_definitions', :routes => @missing_definitions %>
+</div>
View
8 app/views/api_taster/routes/obsolete_definitions.html.erb
@@ -8,11 +8,5 @@
<div class="alert alert-warning">
<p>The following route definitions are found within <code>ApiTaster.routes</code> but they are not defined in your routes. Please feel free to correct or remove them.</p>
- <pre class="prettyprint lang-rb">ApiTaster.routes do
-
- <% @obsolete_definitions.each do |route| %>
- <%= route[:verb].downcase %> '<%= route[:path] %>'<%= ", #{route[:params]}" if route[:params].present? %>
-
- <% end %>
-end</pre>
+ <%= render 'route_definitions', :routes => @obsolete_definitions %>
</div>
View
5 config/routes.rb
@@ -1,6 +1,9 @@
ApiTaster::Engine.routes.draw do
resources :routes, :only => [:index, :show] do
- get :obsolete_definitions, :on => :collection
+ collection do
+ get :missing_definitions
+ get :obsolete_definitions
+ end
end
root :to => 'routes#index'
View
4 lib/api_taster.rb
@@ -8,7 +8,11 @@ module ApiTaster
def self.routes(&block)
Route.route_set = Rails.application.routes
Route.inputs = {}
+ Route.missing_definitions = []
Route.obsolete_definitions = []
+
Mapper.instance_eval(&block)
+
+ Route.calculate_missing_definitions
end
end
View
14 lib/api_taster/route.rb
@@ -2,6 +2,7 @@ module ApiTaster
class Route
cattr_accessor :route_set
cattr_accessor :inputs
+ cattr_accessor :missing_definitions
cattr_accessor :obsolete_definitions
class << self
@@ -50,8 +51,21 @@ def inputs_for(route)
inputs[route[:id]].collect { |input| split_input(input, route) }
end
+ def calculate_missing_definitions
+ routes.each do |route|
+ if undefined_route?(route)
+ self.missing_definitions << route
+ end
+ end
+ end
+
private
+ def undefined_route?(route)
+ r = inputs_for(route)
+ r.is_a?(Hash) && r.has_key?(:undefined)
+ end
+
def discover_rack_app(app)
class_name = app.class.name.to_s
if class_name == "ActionDispatch::Routing::Mapper::Constraints"
View
6 spec/controllers/api_taster/routes_controller_spec.rb
@@ -18,6 +18,12 @@ module ApiTaster
assigns(:inputs).should be_kind_of(Array)
end
+ it "#missing_definitions" do
+ get :missing_definitions, :use_route => :api_taster
+
+ assigns(:missing_definitions).should be_kind_of(Array)
+ end
+
it "#obsolete_definitions" do
get :obsolete_definitions, :use_route => :api_taster
View
2  spec/mapper_spec.rb
@@ -6,7 +6,7 @@ module ApiTaster
before(:all) do
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
- # nothing
+ get '/awesome_route' => 'awesome#route'
end
ApiTaster.routes do
View
13 spec/route_spec.rb
@@ -72,6 +72,19 @@ module ApiTaster
end
end
+ it "#missing_definitions" do
+ routes = ActionDispatch::Routing::RouteSet.new
+ routes.draw do
+ get 'awesome_route' => 'awesome#route'
+ end
+ Rails.application.stub(:routes).and_return(routes)
+ ApiTaster.routes do
+ # nothing
+ end
+
+ Route.missing_definitions.first[:path].should == '/awesome_route'
+ end
+
context "private methods" do
it "#discover_rack_app" do
klass = Class.new
Please sign in to comment.
Something went wrong with that request. Please try again.