aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorMarc Florisson2012-12-13 19:14:45 +0100
committerMarc Florisson2012-12-13 19:14:45 +0100
commit988800310d2e6ae3dfc0935506eeae12f21c9ed6 (patch)
treed1bae0fe02f052006309755347a48f9f48537235 /app
parent3a4ee94cd91de749521ac190656c65266861270f (diff)
downloadchouette-core-988800310d2e6ae3dfc0935506eeae12f21c9ed6.tar.bz2
refactor api/v1
Diffstat (limited to 'app')
-rw-r--r--app/controllers/api/v1/chouette_controller.rb11
-rw-r--r--app/controllers/api/v1/companies_controller.rb4
-rw-r--r--app/controllers/api/v1/connection_links_controller.rb3
-rw-r--r--app/controllers/api/v1/journey_patterns_controller.rb1
-rw-r--r--app/controllers/api/v1/lines_controller.rb2
-rw-r--r--app/controllers/api/v1/networks_controller.rb7
-rw-r--r--app/controllers/api/v1/routes_controller.rb1
-rw-r--r--app/controllers/api/v1/stop_areas_controller.rb5
-rw-r--r--app/controllers/api/v1/time_tables_controller.rb3
-rw-r--r--app/controllers/api/v1/vehicle_journeys_controller.rb1
-rw-r--r--app/models/api/v1/api_key.rb14
11 files changed, 22 insertions, 30 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