From 79bf74add43723bac4977aae5678e295d74c37b2 Mon Sep 17 00:00:00 2001 From: Eugene Burmakin Date: Sun, 24 Nov 2024 14:56:20 +0100 Subject: [PATCH] Move APP_VERSION to a constant --- app/helpers/application_helper.rb | 4 ---- app/jobs/reverse_geocoding_job.rb | 2 ++ app/services/check_app_version.rb | 3 +-- app/views/shared/_navbar.html.erb | 4 ++-- config/initializers/01_constants.rb | 1 + config/initializers/geocoder.rb | 1 + spec/services/check_app_version_spec.rb | 12 +++++------- 7 files changed, 12 insertions(+), 15 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 0b55457b..a5be07f5 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -69,10 +69,6 @@ module ApplicationHelper CheckAppVersion.new.call end - def app_version - File.read('.app_version').strip - end - def app_theme current_user&.theme == 'light' ? 'light' : 'dark' end diff --git a/app/jobs/reverse_geocoding_job.rb b/app/jobs/reverse_geocoding_job.rb index ec57b4e7..27363a24 100644 --- a/app/jobs/reverse_geocoding_job.rb +++ b/app/jobs/reverse_geocoding_job.rb @@ -6,6 +6,8 @@ class ReverseGeocodingJob < ApplicationJob def perform(klass, id) return unless REVERSE_GEOCODING_ENABLED + sleep 1 if PHOTON_API_HOST == 'photon.komoot.io' + data_fetcher(klass, id).call end diff --git a/app/services/check_app_version.rb b/app/services/check_app_version.rb index 6dc59932..3ae5ed76 100644 --- a/app/services/check_app_version.rb +++ b/app/services/check_app_version.rb @@ -5,11 +5,10 @@ class CheckAppVersion def initialize @repo_url = 'https://api.github.com/repos/Freika/dawarich/tags' - @app_version = File.read('.app_version').strip end def call - latest_version != @app_version + latest_version != APP_VERSION rescue StandardError false end diff --git a/app/views/shared/_navbar.html.erb b/app/views/shared/_navbar.html.erb index e24c176a..6ba5ead7 100644 --- a/app/views/shared/_navbar.html.erb +++ b/app/views/shared/_navbar.html.erb @@ -19,10 +19,10 @@ <% if new_version_available? %> - + <% else %> - + <% end %> diff --git a/config/initializers/01_constants.rb b/config/initializers/01_constants.rb index d1ce75e1..5065345f 100644 --- a/config/initializers/01_constants.rb +++ b/config/initializers/01_constants.rb @@ -5,3 +5,4 @@ REVERSE_GEOCODING_ENABLED = ENV.fetch('REVERSE_GEOCODING_ENABLED', 'true') == 't PHOTON_API_HOST = ENV.fetch('PHOTON_API_HOST', nil) PHOTON_API_USE_HTTPS = ENV.fetch('PHOTON_API_USE_HTTPS', 'true') == 'true' DISTANCE_UNIT = ENV.fetch('DISTANCE_UNIT', 'km').to_sym +APP_VERSION = File.read('.app_version').strip diff --git a/config/initializers/geocoder.rb b/config/initializers/geocoder.rb index 724a0531..d873c8ea 100644 --- a/config/initializers/geocoder.rb +++ b/config/initializers/geocoder.rb @@ -6,6 +6,7 @@ settings = { cache: Redis.new, always_raise: :all, use_https: PHOTON_API_USE_HTTPS, + http_headers: { 'User-Agent' => "Dawarich #{APP_VERSION} (https://dawarich.app)" }, cache_options: { expiration: 1.day } diff --git a/spec/services/check_app_version_spec.rb b/spec/services/check_app_version_spec.rb index 58f002f4..b58cc2e5 100644 --- a/spec/services/check_app_version_spec.rb +++ b/spec/services/check_app_version_spec.rb @@ -6,27 +6,25 @@ RSpec.describe CheckAppVersion do describe '#call' do subject(:check_app_version) { described_class.new.call } - let(:app_version) { File.read('.app_version').strip } - before do stub_request(:any, 'https://api.github.com/repos/Freika/dawarich/tags') .to_return(status: 200, body: '[{"name": "1.0.0"}]', headers: {}) + + stub_const('APP_VERSION', '1.0.0') end context 'when latest version is newer' do - before { allow(File).to receive(:read).with('.app_version').and_return('0.9.0') } + before { stub_const('APP_VERSION', '0.9.0') } it { is_expected.to be true } end context 'when latest version is the same' do - before { allow(File).to receive(:read).with('.app_version').and_return('1.0.0') } - it { is_expected.to be false } end context 'when latest version is older' do - before { allow(File).to receive(:read).with('.app_version').and_return('1.1.0') } + before { stub_const('APP_VERSION', '1.1.0') } it { is_expected.to be true } end @@ -34,7 +32,7 @@ RSpec.describe CheckAppVersion do context 'when request fails' do before do allow(Net::HTTP).to receive(:get).and_raise(StandardError) - allow(File).to receive(:read).with('.app_version').and_return(app_version) + allow(File).to receive(:read).with('.app_version').and_return(APP_VERSION) end it { is_expected.to be false }