From 49b61a52e277836924f56d7b61b5438c2063387a Mon Sep 17 00:00:00 2001 From: ildarkayumov Date: Tue, 5 Mar 2013 15:52:53 +0400 Subject: [PATCH 1/2] Add ability to collect routes from several files. --- lib/api_taster.rb | 3 +- lib/api_taster/route.rb | 5 +- lib/api_taster/route_collector.rb | 18 +++++++ spec/dummy/api_tasters/test1.rb | 4 ++ spec/dummy/api_tasters/test2.rb | 3 ++ .../app/api_tasters/global_params/test.rb | 3 ++ spec/dummy/app/api_tasters/mapper/test1.rb | 10 ++++ .../app/api_tasters/route_collector/test1.rb | 4 ++ .../app/api_tasters/route_collector/test2.rb | 3 ++ spec/mapper_spec.rb | 51 +++++++------------ spec/route_collector_spec.rb | 22 ++++++++ 11 files changed, 89 insertions(+), 37 deletions(-) create mode 100644 lib/api_taster/route_collector.rb create mode 100644 spec/dummy/api_tasters/test1.rb create mode 100644 spec/dummy/api_tasters/test2.rb create mode 100644 spec/dummy/app/api_tasters/global_params/test.rb create mode 100644 spec/dummy/app/api_tasters/mapper/test1.rb create mode 100644 spec/dummy/app/api_tasters/route_collector/test1.rb create mode 100644 spec/dummy/app/api_tasters/route_collector/test2.rb create mode 100644 spec/route_collector_spec.rb diff --git a/lib/api_taster.rb b/lib/api_taster.rb index 7add34f..2132588 100644 --- a/lib/api_taster.rb +++ b/lib/api_taster.rb @@ -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 diff --git a/lib/api_taster/route.rb b/lib/api_taster/route.rb index 04eb871..f273ed4 100644 --- a/lib/api_taster/route.rb +++ b/lib/api_taster/route.rb @@ -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 = [] @@ -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 diff --git a/lib/api_taster/route_collector.rb b/lib/api_taster/route_collector.rb new file mode 100644 index 0000000..ac6d927 --- /dev/null +++ b/lib/api_taster/route_collector.rb @@ -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 diff --git a/spec/dummy/api_tasters/test1.rb b/spec/dummy/api_tasters/test1.rb new file mode 100644 index 0000000..d8adb10 --- /dev/null +++ b/spec/dummy/api_tasters/test1.rb @@ -0,0 +1,4 @@ +ApiTaster.routes do + post '/dummy_users' + post '/dummy_users', { :hello => 'world' }, { :meta => 'data' } +end diff --git a/spec/dummy/api_tasters/test2.rb b/spec/dummy/api_tasters/test2.rb new file mode 100644 index 0000000..be62478 --- /dev/null +++ b/spec/dummy/api_tasters/test2.rb @@ -0,0 +1,3 @@ +ApiTaster.routes do + get '/dummy_users/:id', :id => 1 +end \ No newline at end of file diff --git a/spec/dummy/app/api_tasters/global_params/test.rb b/spec/dummy/app/api_tasters/global_params/test.rb new file mode 100644 index 0000000..be62478 --- /dev/null +++ b/spec/dummy/app/api_tasters/global_params/test.rb @@ -0,0 +1,3 @@ +ApiTaster.routes do + get '/dummy_users/:id', :id => 1 +end \ No newline at end of file diff --git a/spec/dummy/app/api_tasters/mapper/test1.rb b/spec/dummy/app/api_tasters/mapper/test1.rb new file mode 100644 index 0000000..65e1c03 --- /dev/null +++ b/spec/dummy/app/api_tasters/mapper/test1.rb @@ -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 diff --git a/spec/dummy/app/api_tasters/route_collector/test1.rb b/spec/dummy/app/api_tasters/route_collector/test1.rb new file mode 100644 index 0000000..d8adb10 --- /dev/null +++ b/spec/dummy/app/api_tasters/route_collector/test1.rb @@ -0,0 +1,4 @@ +ApiTaster.routes do + post '/dummy_users' + post '/dummy_users', { :hello => 'world' }, { :meta => 'data' } +end diff --git a/spec/dummy/app/api_tasters/route_collector/test2.rb b/spec/dummy/app/api_tasters/route_collector/test2.rb new file mode 100644 index 0000000..b00daf6 --- /dev/null +++ b/spec/dummy/app/api_tasters/route_collector/test2.rb @@ -0,0 +1,3 @@ +ApiTaster.routes do + get '/dummy_users/:id', :id => 1 +end diff --git a/spec/mapper_spec.rb b/spec/mapper_spec.rb index 09d567d..518f91e 100644 --- a/spec/mapper_spec.rb +++ b/spec/mapper_spec.rb @@ -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 @@ -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 diff --git a/spec/route_collector_spec.rb b/spec/route_collector_spec.rb new file mode 100644 index 0000000..c1609e2 --- /dev/null +++ b/spec/route_collector_spec.rb @@ -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 From 9a998e19100eaab195134dc00b6feecbea47057c Mon Sep 17 00:00:00 2001 From: Fred Wu Date: Mon, 8 Apr 2013 10:42:41 +1000 Subject: [PATCH 2/2] Small tweaks --- lib/api_taster/route.rb | 3 +-- lib/api_taster/route_collector.rb | 15 ++++++--------- .../app/api_tasters/route_collector/test2.rb | 2 +- spec/mapper_spec.rb | 2 -- spec/route_collector_spec.rb | 2 +- 5 files changed, 9 insertions(+), 15 deletions(-) diff --git a/lib/api_taster/route.rb b/lib/api_taster/route.rb index f273ed4..2ff8ed1 100644 --- a/lib/api_taster/route.rb +++ b/lib/api_taster/route.rb @@ -9,7 +9,6 @@ class Route cattr_accessor :metadata class << self - def map_routes(path = "#{Rails.root}/app/api_tasters") self.route_set = Rails.application.routes self.supplied_params = {} @@ -20,7 +19,7 @@ def map_routes(path = "#{Rails.root}/app/api_tasters") normalise_routes! begin - ApiTaster::RouteCollector.collect!(path) + ApiTaster::RouteCollector.collect(path) Mapper.instance_eval(&self.mappings) rescue Route.mappings = {} diff --git a/lib/api_taster/route_collector.rb b/lib/api_taster/route_collector.rb index ac6d927..0993d7f 100644 --- a/lib/api_taster/route_collector.rb +++ b/lib/api_taster/route_collector.rb @@ -3,15 +3,12 @@ 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 + 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 diff --git a/spec/dummy/app/api_tasters/route_collector/test2.rb b/spec/dummy/app/api_tasters/route_collector/test2.rb index b00daf6..a1b2d5f 100644 --- a/spec/dummy/app/api_tasters/route_collector/test2.rb +++ b/spec/dummy/app/api_tasters/route_collector/test2.rb @@ -1,3 +1,3 @@ ApiTaster.routes do - get '/dummy_users/:id', :id => 1 + get '/dummy_users/:id', :id => 42 end diff --git a/spec/mapper_spec.rb b/spec/mapper_spec.rb index 518f91e..b028a3c 100644 --- a/spec/mapper_spec.rb +++ b/spec/mapper_spec.rb @@ -39,9 +39,7 @@ module ApiTaster member { map_method :patch, :update } end end - end - before(:all) do Route.map_routes "#{Rails.root}/app/api_tasters/mapper" end diff --git a/spec/route_collector_spec.rb b/spec/route_collector_spec.rb index c1609e2..7302bbc 100644 --- a/spec/route_collector_spec.rb +++ b/spec/route_collector_spec.rb @@ -11,7 +11,7 @@ module ApiTaster it "gets users" do route = Route.find_by_verb_and_path(:get, '/dummy_users/:id') - Route.supplied_params[route[:id]].should == [{ :id => 1 }] + Route.supplied_params[route[:id]].should == [{ :id => 42 }] end it "posts a new user" do