diff options
| -rw-r--r-- | app/controllers/api/v1/chouette_controller.rb | 11 | ||||
| -rw-r--r-- | app/controllers/api/v1/companies_controller.rb | 4 | ||||
| -rw-r--r-- | app/controllers/api/v1/connection_links_controller.rb | 3 | ||||
| -rw-r--r-- | app/controllers/api/v1/journey_patterns_controller.rb | 1 | ||||
| -rw-r--r-- | app/controllers/api/v1/lines_controller.rb | 2 | ||||
| -rw-r--r-- | app/controllers/api/v1/networks_controller.rb | 7 | ||||
| -rw-r--r-- | app/controllers/api/v1/routes_controller.rb | 1 | ||||
| -rw-r--r-- | app/controllers/api/v1/stop_areas_controller.rb | 5 | ||||
| -rw-r--r-- | app/controllers/api/v1/time_tables_controller.rb | 3 | ||||
| -rw-r--r-- | app/controllers/api/v1/vehicle_journeys_controller.rb | 1 | ||||
| -rw-r--r-- | app/models/api/v1/api_key.rb | 14 | ||||
| -rw-r--r-- | config/routes.rb | 30 | ||||
| -rw-r--r-- | spec/controllers/api/v1/lines_controller_spec.rb | 1 | ||||
| -rw-r--r-- | spec/controllers/api/v1/stop_area_controller_spec.rb | 21 | ||||
| -rw-r--r-- | spec/support/api_key_protected.rb | 7 |
15 files changed, 60 insertions, 51 deletions
diff --git a/app/controllers/api/v1/chouette_controller.rb b/app/controllers/api/v1/chouette_controller.rb index 98f15414b..81851013c 100644 --- a/app/controllers/api/v1/chouette_controller.rb +++ b/app/controllers/api/v1/chouette_controller.rb @@ -6,19 +6,20 @@ module Api layout false before_filter :authenticate - belongs_to :referential - private - alias_method :referential, :parent def authenticate authenticate_or_request_with_http_token do |token, options| - @api_key = referential.api_keys.find_by_token(token) + puts "token=#{token}" + puts "Api::V1::ApiKey.all.map(&:token)=#{Api::V1::ApiKey.all.map(&:token).join(',')}" + @referential = Api::V1::ApiKey.referential_from_token(token) + + @api_key = @referential.api_keys.find_by_token(token) if @referential switch_referential if @api_key end end def switch_referential - Apartment::Database.switch(referential.slug) + Apartment::Database.switch(@api_key.referential.slug) end end diff --git a/app/controllers/api/v1/companies_controller.rb b/app/controllers/api/v1/companies_controller.rb index 5e71d2eb5..32ffafd03 100644 --- a/app/controllers/api/v1/companies_controller.rb +++ b/app/controllers/api/v1/companies_controller.rb @@ -1,12 +1,12 @@ class Api::V1::CompaniesController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::Company, :finder => :find_by_objectid! protected def collection - @companies ||= referential.companies.search(params[:q]).result(:distinct => true) + @companies ||= ( @referential ? @referential.companies.search(params[:q]).result(:distinct => true) : []) + end end diff --git a/app/controllers/api/v1/connection_links_controller.rb b/app/controllers/api/v1/connection_links_controller.rb index b1e23d413..c5cf39812 100644 --- a/app/controllers/api/v1/connection_links_controller.rb +++ b/app/controllers/api/v1/connection_links_controller.rb @@ -1,12 +1,11 @@ class Api::V1::ConnectionLinksController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::ConnectionLink, :finder => :find_by_objectid! protected def collection - @connection_links ||= referential.connection_links.search(params[:q]).result(:distinct => true) + @connection_links ||= ( @referential ? @referential.connection_links.search(params[:q]).result(:distinct => true) : []) end end diff --git a/app/controllers/api/v1/journey_patterns_controller.rb b/app/controllers/api/v1/journey_patterns_controller.rb index deab88af8..9e7bc3da2 100644 --- a/app/controllers/api/v1/journey_patterns_controller.rb +++ b/app/controllers/api/v1/journey_patterns_controller.rb @@ -1,5 +1,4 @@ class Api::V1::JourneyPatternsController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::JourneyPattern, :finder => :find_by_objectid! diff --git a/app/controllers/api/v1/lines_controller.rb b/app/controllers/api/v1/lines_controller.rb index 9e58ea46b..2375a5f7a 100644 --- a/app/controllers/api/v1/lines_controller.rb +++ b/app/controllers/api/v1/lines_controller.rb @@ -7,7 +7,7 @@ module Api protected def collection - @lines ||= referential.lines.search(params[:q]).result(:distinct => true) + @lines ||= ( @referential ? @referential.lines.search(params[:q]).result(:distinct => true) : []) end end end diff --git a/app/controllers/api/v1/networks_controller.rb b/app/controllers/api/v1/networks_controller.rb index c69c46433..22f58addf 100644 --- a/app/controllers/api/v1/networks_controller.rb +++ b/app/controllers/api/v1/networks_controller.rb @@ -1,18 +1,13 @@ module Api module V1 class NetworksController < ChouetteController - inherit_resources defaults :resource_class => Chouette::Network, :finder => :find_by_objectid! - belongs_to :referential, :parent_class => ::Referential protected - def parent - @referential ||= Referential.find(params[:referential_id]) - end def collection - @networks ||= referential.networks.search(params[:q]).result(:distinct => true) + @networks ||= ( @referential ? @referential.networks.search(params[:q]).result(:distinct => true) : []) end end end diff --git a/app/controllers/api/v1/routes_controller.rb b/app/controllers/api/v1/routes_controller.rb index e7a6ae0cf..e3694725f 100644 --- a/app/controllers/api/v1/routes_controller.rb +++ b/app/controllers/api/v1/routes_controller.rb @@ -1,5 +1,4 @@ class Api::V1::RoutesController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::Route, :finder => :find_by_objectid! diff --git a/app/controllers/api/v1/stop_areas_controller.rb b/app/controllers/api/v1/stop_areas_controller.rb index 2d5a176ae..31259ec8f 100644 --- a/app/controllers/api/v1/stop_areas_controller.rb +++ b/app/controllers/api/v1/stop_areas_controller.rb @@ -1,5 +1,4 @@ class Api::V1::StopAreasController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::StopArea, :finder => :find_by_objectid! @@ -11,9 +10,9 @@ protected def collection if parent - @stop_areas ||= parent.stop_areas.search(params[:q]).result(:distinct => true) + @stop_areas ||= parent.stop_areas.search(params[:q]).result else - @stop_areas ||= referential.stop_areas.search(params[:q]).result(:distinct => true) + @stop_areas ||= @referential.stop_areas.search(params[:q]).result(:distinct => true) end end diff --git a/app/controllers/api/v1/time_tables_controller.rb b/app/controllers/api/v1/time_tables_controller.rb index eb54b5444..669318bad 100644 --- a/app/controllers/api/v1/time_tables_controller.rb +++ b/app/controllers/api/v1/time_tables_controller.rb @@ -1,12 +1,11 @@ class Api::V1::TimeTablesController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::TimeTable, :finder => :find_by_objectid! protected def collection - @time_tables ||= referential.time_tables.search(params[:q]).result(:distinct => true) + @time_tables ||= @referential.time_tables.search(params[:q]).result(:distinct => true) end end diff --git a/app/controllers/api/v1/vehicle_journeys_controller.rb b/app/controllers/api/v1/vehicle_journeys_controller.rb index 2dd78cc19..b5c077635 100644 --- a/app/controllers/api/v1/vehicle_journeys_controller.rb +++ b/app/controllers/api/v1/vehicle_journeys_controller.rb @@ -1,5 +1,4 @@ class Api::V1::VehicleJourneysController < Api::V1::ChouetteController - inherit_resources defaults :resource_class => Chouette::VehicleJourney, :finder => :find_by_objectid! diff --git a/app/models/api/v1/api_key.rb b/app/models/api/v1/api_key.rb index 0b045e58f..40719127a 100644 --- a/app/models/api/v1/api_key.rb +++ b/app/models/api/v1/api_key.rb @@ -5,19 +5,21 @@ module Api belongs_to :referential, :class_name => '::Referential' def eql?(other) - other.token == self.token && other.referential_id == self.referential_id + other.token == self.token + end + + def self.referential_from_token(token) + array = token.split('-') + return nil unless array.size==2 + ::Referential.find( array.first) end private def generate_access_token begin - self.token = SecureRandom.hex - puts "self.token=#{self.token}" + self.token = "#{referential.id}-#{SecureRandom.hex}" end while self.class.exists?(:token => self.token) end - def organisation - @organisation ||= Organisation.find_by_id @organisation_id - end end end end diff --git a/config/routes.rb b/config/routes.rb index 4a78e20a1..a512a68dc 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -7,24 +7,22 @@ ChouetteIhm::Application.routes.draw do namespace :api do namespace :v1 do - resources :referentials do - resources :time_tables, :only => [:index, :show] - resources :connection_links, :only => [:index, :show] - resources :companies, :only => [:index, :show] - resources :networks, :only => [:index, :show] - resources :stop_areas, :only => [:index, :show] - resources :lines, :only => [:index, :show] do + resources :time_tables, :only => [:index, :show] + resources :connection_links, :only => [:index, :show] + resources :companies, :only => [:index, :show] + resources :networks, :only => [:index, :show] + resources :stop_areas, :only => [:index, :show] + resources :lines, :only => [:index, :show] do + resources :journey_patterns, :only => [:index, :show] + resources :routes, :only => [:index, :show] do + resources :vehicle_journeys, :only => [:index, :show] resources :journey_patterns, :only => [:index, :show] - resources :routes, :only => [:index, :show] do - resources :vehicle_journeys, :only => [:index, :show] - resources :journey_patterns, :only => [:index, :show] - resources :stop_areas, :only => [:index, :show] - end + resources :stop_areas, :only => [:index, :show] end - resources :routes, :only => :show - resources :journey_patterns, :only => :show - resources :vehicle_journeys, :only => :show end + resources :routes, :only => :show + resources :journey_patterns, :only => :show + resources :vehicle_journeys, :only => :show end end @@ -38,6 +36,8 @@ ChouetteIhm::Application.routes.draw do resources :file_validations resources :referentials do + resources :keys + resources :api_keys resources :stop_point_areas match 'lines' => 'lines#destroy_all', :via => :delete resources :group_of_lines do diff --git a/spec/controllers/api/v1/lines_controller_spec.rb b/spec/controllers/api/v1/lines_controller_spec.rb index 2bf7515b9..5bd8cac57 100644 --- a/spec/controllers/api/v1/lines_controller_spec.rb +++ b/spec/controllers/api/v1/lines_controller_spec.rb @@ -5,7 +5,6 @@ describe Api::V1::LinesController do it_behaves_like "api key protected controller" do let(:data){line} - let(:provided_referential){referential} end describe "GET #index" do it "test" do diff --git a/spec/controllers/api/v1/stop_area_controller_spec.rb b/spec/controllers/api/v1/stop_area_controller_spec.rb new file mode 100644 index 000000000..cd1c2020b --- /dev/null +++ b/spec/controllers/api/v1/stop_area_controller_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe Api::V1::StopAreasController do + let!(:stop_area) { referential.stop_areas.first || create(:stop_area) } + + it_behaves_like "api key protected controller" do + let(:data){stop_area} + end + describe "GET #index, route_id, line_id" do + let!(:line) { referential.lines.first || create(:line) } + let!(:route) { line.routes.first || create(:route, :line => line) } + before :each do + config_formatted_request_with_authorization( "application/json") + get :index, :route_id => route.objectid, :line_id => route.line.objectid + end + it "test" do + assigns[:stop_areas].should == route.stop_areas + end + end +end + diff --git a/spec/support/api_key_protected.rb b/spec/support/api_key_protected.rb index 9f3544afb..43256716d 100644 --- a/spec/support/api_key_protected.rb +++ b/spec/support/api_key_protected.rb @@ -1,14 +1,13 @@ shared_examples "api key protected controller" do - let(:h) { { :index => (Proc.new { get :index, :referential_id => provided_referential.id }), - :show => (Proc.new { get :show, :referential_id => provided_referential.id, :id => data.objectid })}} + let(:h) { { :index => (Proc.new { get :index }), + :show => (Proc.new { get :show, :id => data.objectid })}} [:index, :show].each do |http_verb| describe "GET ##{http_verb}" do ["application/json","application/xml","application/html"].each do |format| context "when an invalid authorization is provided" do before :each do - puts "when an invalid authorization is provided" config_formatted_request_with_dummy_authorization( format) h[http_verb].call end @@ -18,7 +17,6 @@ shared_examples "api key protected controller" do end context "when no authorization is provided" do before :each do - puts "when no authorization is provided" config_formatted_request_without_authorization( format) h[http_verb].call end @@ -28,7 +26,6 @@ shared_examples "api key protected controller" do end context "when authorization provided and request.accept is #{format}," do before :each do - puts "when authorization provided and request.accept is #{format}," config_formatted_request_with_authorization( format) h[http_verb].call end |
