Remove REVERSE_GEOCODING_ENABLED env var

This commit is contained in:
Eugene Burmakin 2025-01-07 13:41:09 +01:00
parent 688440710f
commit 974f45a4c9
13 changed files with 25 additions and 25 deletions

View file

@ -17,6 +17,7 @@ You may now use Geoapify API for reverse geocoding. To obtain an API key, sign u
- Photon ENV vars from the `.env.development` and docker-compose.yml files. - Photon ENV vars from the `.env.development` and docker-compose.yml files.
- `APPLICATION_HOST` env var. - `APPLICATION_HOST` env var.
- `REVERSE_GEOCODING_ENABLED` env var.
# 0.21.4 - 2025-01-05 # 0.21.4 - 2025-01-05

View file

@ -4,7 +4,7 @@ class ReverseGeocodingJob < ApplicationJob
queue_as :reverse_geocoding queue_as :reverse_geocoding
def perform(klass, id) def perform(klass, id)
return unless REVERSE_GEOCODING_ENABLED return unless DawarichSettings.reverse_geocoding_enabled?
rate_limit_for_photon_api rate_limit_for_photon_api

View file

@ -13,7 +13,7 @@ class Place < ApplicationRecord
enum :source, { manual: 0, photon: 1 } enum :source, { manual: 0, photon: 1 }
def async_reverse_geocode def async_reverse_geocode
return unless REVERSE_GEOCODING_ENABLED return unless DawarichSettings.reverse_geocoding_enabled?
ReverseGeocodingJob.perform_later(self.class.to_s, id) ReverseGeocodingJob.perform_later(self.class.to_s, id)
end end

View file

@ -34,7 +34,7 @@ class Point < ApplicationRecord
end end
def async_reverse_geocode def async_reverse_geocode
return unless REVERSE_GEOCODING_ENABLED return unless DawarichSettings.reverse_geocoding_enabled?
ReverseGeocodingJob.perform_later(self.class.to_s, id) ReverseGeocodingJob.perform_later(self.class.to_s, id)
end end

View file

@ -40,7 +40,7 @@ class Visit < ApplicationRecord
end end
def async_reverse_geocode def async_reverse_geocode
return unless REVERSE_GEOCODING_ENABLED return unless DawarichSettings.reverse_geocoding_enabled?
return if place.blank? return if place.blank?
ReverseGeocodingJob.perform_later('place', place_id) ReverseGeocodingJob.perform_later('place', place_id)

View file

@ -20,7 +20,7 @@ class Visits::Suggest
create_visits_notification(user) create_visits_notification(user)
return nil unless reverse_geocoding_enabled? return nil unless DawarichSettings.reverse_geocoding_enabled?
reverse_geocode(visits) reverse_geocode(visits)
end end
@ -68,10 +68,6 @@ class Visits::Suggest
visits.each(&:async_reverse_geocode) visits.each(&:async_reverse_geocode)
end end
def reverse_geocoding_enabled?
::REVERSE_GEOCODING_ENABLED && ::PHOTON_API_HOST.present?
end
def create_visits_notification(user) def create_visits_notification(user)
content = <<~CONTENT content = <<~CONTENT
New visits have been suggested based on your location data from #{Time.zone.at(start_at)} to #{Time.zone.at(end_at)}. You can review them on the <a href="#{visits_path}" class="link">Visits</a> page. New visits have been suggested based on your location data from #{Time.zone.at(start_at)} to #{Time.zone.at(end_at)}. You can review them on the <a href="#{visits_path}" class="link">Visits</a> page.

View file

@ -12,7 +12,7 @@
</div> </div>
</div> </div>
<p><%= stat.distance %><%= DISTANCE_UNIT %></p> <p><%= stat.distance %><%= DISTANCE_UNIT %></p>
<% if REVERSE_GEOCODING_ENABLED %> <% if DawarichSettings.reverse_geocoding_enabled? %>
<div class="card-actions justify-end"> <div class="card-actions justify-end">
<%= countries_and_cities_stat_for_month(stat) %> <%= countries_and_cities_stat_for_month(stat) %>
</div> </div>

View file

@ -16,7 +16,7 @@
<div class="stat-title">Geopoints tracked</div> <div class="stat-title">Geopoints tracked</div>
</div> </div>
<% if REVERSE_GEOCODING_ENABLED %> <% if DawarichSettings.reverse_geocoding_enabled? %>
<%= render 'stats/reverse_geocoding_stats' %> <%= render 'stats/reverse_geocoding_stats' %>
<% end %> <% end %>
</div> </div>
@ -39,7 +39,7 @@
<%= number_with_delimiter year_distance_stat(year, current_user) %><%= DISTANCE_UNIT %> <%= number_with_delimiter year_distance_stat(year, current_user) %><%= DISTANCE_UNIT %>
<% end %> <% end %>
</p> </p>
<% if REVERSE_GEOCODING_ENABLED %> <% if DawarichSettings.reverse_geocoding_enabled? %>
<div class="card-actions justify-end"> <div class="card-actions justify-end">
<%= countries_and_cities_stat_for_year(year, stats) %> <%= countries_and_cities_stat_for_year(year, stats) %>
</div> </div>

View file

@ -9,8 +9,6 @@ TELEMETRY_STRING = Base64.encode64('IjVFvb8j3P9-ArqhSGav9j8YcJaQiuNIzkfOPKQDk2lv
TELEMETRY_URL = 'https://influxdb2.frey.today/api/v2/write' TELEMETRY_URL = 'https://influxdb2.frey.today/api/v2/write'
# Reverse geocoding settings # Reverse geocoding settings
REVERSE_GEOCODING_ENABLED = ENV.fetch('REVERSE_GEOCODING_ENABLED', 'true') == 'true'
PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil) PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil)
PHOTON_API_KEY = ENV.fetch('PHOTON_API_KEY', nil) PHOTON_API_KEY = ENV.fetch('PHOTON_API_KEY', nil)
PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true' PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true'

View file

@ -3,19 +3,19 @@
class DawarichSettings class DawarichSettings
class << self class << self
def reverse_geocoding_enabled? def reverse_geocoding_enabled?
photon_enabled? || geoapify_enabled? @reverse_geocoding_enabled ||= photon_enabled? || geoapify_enabled?
end end
def photon_enabled? def photon_enabled?
PHOTON_API_HOST.present? @photon_enabled ||= PHOTON_API_HOST.present?
end end
def photon_uses_komoot_io? def photon_uses_komoot_io?
PHOTON_API_HOST == 'photon.komoot.io' @photon_uses_komoot_io ||= PHOTON_API_HOST == 'photon.komoot.io'
end end
def geoapify_enabled? def geoapify_enabled?
GEOAPIFY_API_KEY.present? @geoapify_enabled ||= GEOAPIFY_API_KEY.present?
end end
end end
end end

View file

@ -12,8 +12,8 @@ RSpec.describe ReverseGeocodingJob, type: :job do
allow(Geocoder).to receive(:search).and_return([double(city: 'City', country: 'Country')]) allow(Geocoder).to receive(:search).and_return([double(city: 'City', country: 'Country')])
end end
context 'when REVERSE_GEOCODING_ENABLED is false' do context 'when reverse geocoding is disabled' do
before { stub_const('REVERSE_GEOCODING_ENABLED', false) } before { allow(DawarichSettings).to receive(:reverse_geocoding_enabled?).and_return(false) }
it 'does not update point' do it 'does not update point' do
expect { perform }.not_to(change { point.reload.city }) expect { perform }.not_to(change { point.reload.city })
@ -28,8 +28,8 @@ RSpec.describe ReverseGeocodingJob, type: :job do
end end
end end
context 'when REVERSE_GEOCODING_ENABLED is true' do context 'when reverse geocoding is enabled' do
before { stub_const('REVERSE_GEOCODING_ENABLED', true) } before { allow(DawarichSettings).to receive(:reverse_geocoding_enabled?).and_return(true) }
let(:stubbed_geocoder) { OpenStruct.new(data: { city: 'City', country: 'Country' }) } let(:stubbed_geocoder) { OpenStruct.new(data: { city: 'City', country: 'Country' }) }

View file

@ -3,6 +3,12 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe DawarichSettings do RSpec.describe DawarichSettings do
before do
described_class.instance_variables.each do |ivar|
described_class.remove_instance_variable(ivar)
end
end
describe '.reverse_geocoding_enabled?' do describe '.reverse_geocoding_enabled?' do
context 'when photon is enabled' do context 'when photon is enabled' do
before do before do

View file

@ -44,8 +44,7 @@ RSpec.describe Visits::Suggest do
context 'when reverse geocoding is enabled' do context 'when reverse geocoding is enabled' do
before do before do
stub_const('REVERSE_GEOCODING_ENABLED', true) allow(DawarichSettings).to receive(:reverse_geocoding_enabled?).and_return(true)
stub_const('PHOTON_API_HOST', 'http://localhost:2323')
end end
it 'reverse geocodes visits' do it 'reverse geocodes visits' do
@ -57,7 +56,7 @@ RSpec.describe Visits::Suggest do
context 'when reverse geocoding is disabled' do context 'when reverse geocoding is disabled' do
before do before do
stub_const('REVERSE_GEOCODING_ENABLED', false) allow(DawarichSettings).to receive(:reverse_geocoding_enabled?).and_return(false)
end end
it 'does not reverse geocode visits' do it 'does not reverse geocode visits' do