Skip to content

Commit

Permalink
Add ability to collect routes from several files.
Browse files Browse the repository at this point in the history
  • Loading branch information
ildarkayumov committed Apr 6, 2013
1 parent 2c0909e commit 49b61a5
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 37 deletions.
3 changes: 2 additions & 1 deletion lib/api_taster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
require 'api_taster/route'
require 'api_taster/mapper'
require 'api_taster/form_builder'
require 'api_taster/route_collector'

module ApiTaster
mattr_accessor :global_params
self.global_params = {}

def self.routes(&block)
Route.mappings = Proc.new { block }
ApiTaster::RouteCollector.routes << block
end

class Exception < ::Exception; end
Expand Down
5 changes: 3 additions & 2 deletions lib/api_taster/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Route

class << self

def map_routes
def map_routes(path = "#{Rails.root}/app/api_tasters")
self.route_set = Rails.application.routes
self.supplied_params = {}
self.obsolete_definitions = []
Expand All @@ -20,7 +20,8 @@ def map_routes
normalise_routes!

begin
Mapper.instance_eval(&self.mappings.call)
ApiTaster::RouteCollector.collect!(path)
Mapper.instance_eval(&self.mappings)
rescue
Route.mappings = {}
end
Expand Down
18 changes: 18 additions & 0 deletions lib/api_taster/route_collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module ApiTaster
class RouteCollector
cattr_accessor :routes
self.routes = []

class << self
def collect!(path)
self.routes = []
Dir["#{path}/**/*.rb"].each { |file| load(file) }
Route.mappings = Proc.new do
for route in RouteCollector.routes
instance_eval(&route)
end
end
end
end
end
end
4 changes: 4 additions & 0 deletions spec/dummy/api_tasters/test1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ApiTaster.routes do
post '/dummy_users'
post '/dummy_users', { :hello => 'world' }, { :meta => 'data' }
end
3 changes: 3 additions & 0 deletions spec/dummy/api_tasters/test2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ApiTaster.routes do
get '/dummy_users/:id', :id => 1
end
3 changes: 3 additions & 0 deletions spec/dummy/app/api_tasters/global_params/test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ApiTaster.routes do
get '/dummy_users/:id', :id => 1
end
10 changes: 10 additions & 0 deletions spec/dummy/app/api_tasters/mapper/test1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
ApiTaster.routes do
get '/dummy_route'
desc "Dummy user ID"
get '/dummy_users/:id', :id => 1
post '/dummy_users'
post '/dummy_users', { :hello => 'world' }, { :meta => 'data' }
put '/dummy_users/:id', :id => 2
delete '/dummy_users/:id', :id => 3
patch '/dummy_users/:id', :id => 4
end
4 changes: 4 additions & 0 deletions spec/dummy/app/api_tasters/route_collector/test1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ApiTaster.routes do
post '/dummy_users'
post '/dummy_users', { :hello => 'world' }, { :meta => 'data' }
end
3 changes: 3 additions & 0 deletions spec/dummy/app/api_tasters/route_collector/test2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ApiTaster.routes do
get '/dummy_users/:id', :id => 1
end
51 changes: 17 additions & 34 deletions spec/mapper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,30 @@

module ApiTaster
describe Mapper do
context "#global_params" do
before(:all) do
ApiTaster.global_params = { :foo => 'bar' }

Route.map_routes "#{Rails.root}/app/api_tasters/global_params"
end

it "merges params" do
route = Route.find_by_verb_and_path(:get, '/dummy_users/:id')

Route.supplied_params[route[:id]].should == [{ :foo => 'bar', :id => 1 }]
end
end

context "non-existing routes" do
before(:all) do
ApiTaster.global_params = {}
routes = ActionDispatch::Routing::RouteSet.new
routes.draw do
get '/awesome_route' => 'awesome#route'
end

ApiTaster.routes do
get '/dummy_route'
end

Route.route_set = routes
Route.map_routes
Route.map_routes "#{Rails.root}/app/api_tasters/mapper"
end

it "records obsolete definitions" do
Expand All @@ -30,36 +41,8 @@ module ApiTaster
end
end

context "#global_params" do
before(:all) do
ApiTaster.global_params = { :foo => 'bar' }

ApiTaster.routes do
get '/dummy_users/:id', :id => 1
end

Route.map_routes
end

it "merges params" do
route = Route.find_by_verb_and_path(:get, '/dummy_users/:id')

Route.supplied_params[route[:id]].should == [{ :foo => 'bar', :id => 1 }]
end
end

before(:all) do
ApiTaster.routes do
desc "Dummy user ID"
get '/dummy_users/:id', :id => 1
post '/dummy_users'
post '/dummy_users', { :hello => 'world' }, { :meta => 'data' }
put '/dummy_users/:id', :id => 2
delete '/dummy_users/:id', :id => 3
patch '/dummy_users/:id', :id => 4
end

Route.map_routes
Route.map_routes "#{Rails.root}/app/api_tasters/mapper"
end

it "gets users" do
Expand Down
22 changes: 22 additions & 0 deletions spec/route_collector_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'spec_helper'

module ApiTaster
describe RouteCollector do
before(:all) do
Rails.application.routes.draw do
resources :dummy_users
end
Route.map_routes "#{Rails.root}/app/api_tasters/route_collector"
end

it "gets users" do
route = Route.find_by_verb_and_path(:get, '/dummy_users/:id')
Route.supplied_params[route[:id]].should == [{ :id => 1 }]
end

it "posts a new user" do
route = Route.find_by_verb_and_path(:post, '/dummy_users')
Route.supplied_params[route[:id]].should == [{}, { :hello => 'world' }]
end
end
end

0 comments on commit 49b61a5

Please sign in to comment.