Skip to content

Commit

Permalink
Merge pull request #42 from fredwu/pr/39
Browse files Browse the repository at this point in the history
Pr/39
  • Loading branch information
fredwu committed Apr 8, 2013
2 parents 62b5e1c + 9a998e1 commit 39b288c
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 40 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
6 changes: 3 additions & 3 deletions lib/api_taster/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ class Route
cattr_accessor :metadata

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 +19,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
15 changes: 15 additions & 0 deletions lib/api_taster/route_collector.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module ApiTaster
class RouteCollector
cattr_accessor :routes
self.routes = []

def self.collect(path)
self.routes = []
Dir["#{path}/**/*.rb"].each { |file| load(file) }

Route.mappings = Proc.new do
RouteCollector.routes.each { |route| instance_eval(&route) }
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 => 42
end
53 changes: 17 additions & 36 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 @@ -28,38 +39,8 @@ module ApiTaster
member { map_method :patch, :update }
end
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 => 42 }]
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 39b288c

Please sign in to comment.