diff options
| -rw-r--r-- | app/models/chouette/route.rb | 2 | ||||
| -rw-r--r-- | lib/tom_tom.rb | 8 | ||||
| -rw-r--r-- | spec/lib/tom_tom_spec.rb | 15 | ||||
| -rw-r--r-- | spec/models/chouette/route/route_base_spec.rb | 18 |
4 files changed, 40 insertions, 3 deletions
diff --git a/app/models/chouette/route.rb b/app/models/chouette/route.rb index 4b1b88543..f814d5160 100644 --- a/app/models/chouette/route.rb +++ b/app/models/chouette/route.rb @@ -75,7 +75,7 @@ module Chouette validates :wayback, inclusion: { in: self.wayback.values } - after_save :calculate_costs! + after_save :calculate_costs!, if: ->() { TomTom.enabled? } def duplicate overrides = { diff --git a/lib/tom_tom.rb b/lib/tom_tom.rb index 97f914f28..a1a2bda43 100644 --- a/lib/tom_tom.rb +++ b/lib/tom_tom.rb @@ -1,17 +1,21 @@ module TomTom BASE_URL = 'https://api.tomtom.com' - API_KEY = Rails.application.secrets.tomtom_api_key + @api_key = Rails.application.secrets.tomtom_api_key @connection = Faraday.new( url: BASE_URL, params: { - key: API_KEY + key: @api_key } ) do |faraday| faraday.use FaradayMiddleware::FollowRedirects, limit: 1 faraday.adapter Faraday.default_adapter end + def self.enabled? + @api_key.present? + end + def self.batch(way_costs) TomTom::Batch.new(@connection).batch(way_costs) end diff --git a/spec/lib/tom_tom_spec.rb b/spec/lib/tom_tom_spec.rb new file mode 100644 index 000000000..71584e242 --- /dev/null +++ b/spec/lib/tom_tom_spec.rb @@ -0,0 +1,15 @@ +RSpec.describe TomTom do + describe ".enabled?" do + it "returns true when API key is set" do + TomTom.instance_variable_set(:@api_key, 'fake key') + + expect(TomTom.enabled?).to be true + end + + it "returns false without an API key" do + TomTom.instance_variable_set(:@api_key, '') + + expect(TomTom.enabled?).to be false + end + end +end diff --git a/spec/models/chouette/route/route_base_spec.rb b/spec/models/chouette/route/route_base_spec.rb index 98cb3e358..d24ad6348 100644 --- a/spec/models/chouette/route/route_base_spec.rb +++ b/spec/models/chouette/route/route_base_spec.rb @@ -61,4 +61,22 @@ RSpec.describe Chouette::Route, :type => :model do end end end + + context "callbacks" do + it "calls #calculate_costs! after_save when TomTom is enabled" do + allow(TomTom).to receive(:enabled?).and_return(true) + route = create(:route) + + expect(route).to receive(:calculate_costs!) + route.save + end + + it "doesn't call #calculate_costs! after_save if TomTom is disabled" do + allow(TomTom).to receive(:enabled?).and_return(false) + route = create(:route) + + expect(route).not_to receive(:calculate_costs!) + route.save + end + end end |
