From 271384e683bfbee1ee3377f36df07e48bff5e39f Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 25 Jun 2024 17:57:38 +0100 Subject: [PATCH] Simplify handling of geocoder URLs This avoids having to build them in multiple places and also ensures we link to what was actually searched rather than some random string from the locale file. --- app/controllers/geocoder_controller.rb | 20 +++++++++++--------- app/views/geocoder/search.html.erb | 3 +-- config/locales/en.yml | 3 --- test/http/nominatim.yml | 6 +++--- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/controllers/geocoder_controller.rb b/app/controllers/geocoder_controller.rb index 0419fb3b8..4a593f86a 100644 --- a/app/controllers/geocoder_controller.rb +++ b/app/controllers/geocoder_controller.rb @@ -13,10 +13,10 @@ class GeocoderController < ApplicationController @sources = [] if @params[:lat] && @params[:lon] - @sources.push({ :name => "latlon", :parameters => "" }) - @sources.push({ :name => "osm_nominatim_reverse", :parameters => "reverse?format=html&#{nominatim_reverse_url_parameters}" }) + @sources.push(:name => "latlon", :url => root_path) + @sources.push(:name => "osm_nominatim_reverse", :url => nominatim_reverse_url(:format => "html")) elsif @params[:query] - @sources.push({ :name => "osm_nominatim", :parameters => "search?format=html&#{nominatim_url_parameters}" }) + @sources.push(:name => "osm_nominatim", :url => nominatim_url(:format => "html")) end if @sources.empty? @@ -72,7 +72,7 @@ class GeocoderController < ApplicationController def search_osm_nominatim # ask nominatim - response = fetch_xml("#{Settings.nominatim_url}search?format=xml&" + nominatim_url_parameters) + response = fetch_xml(nominatim_url(:format => "xml")) # extract the results from the response results = response.elements["searchresults"] @@ -131,7 +131,7 @@ class GeocoderController < ApplicationController @results = [] # ask nominatim - response = fetch_xml("#{Settings.nominatim_url}reverse?" + nominatim_reverse_url_parameters) + response = fetch_xml(nominatim_reverse_url(:format => "xml")) # parse the response response.elements.each("reversegeocode/result") do |result| @@ -156,7 +156,7 @@ class GeocoderController < ApplicationController private - def nominatim_url_parameters + def nominatim_url(format: nil) # get query parameters query = params[:query] minlon = params[:minlon] @@ -170,16 +170,18 @@ class GeocoderController < ApplicationController # get objects to excude exclude = "&exclude_place_ids=#{params[:exclude]}" if params[:exclude] - "extratags=1&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}" + # build url + "#{Settings.nominatim_url}search?format=#{format}&extratags=1&q=#{escape_query(query)}#{viewbox}#{exclude}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}" end - def nominatim_reverse_url_parameters + def nominatim_reverse_url(format: nil) # get query parameters lat = params[:lat] lon = params[:lon] zoom = params[:zoom] - "lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}" + # build url + "#{Settings.nominatim_url}reverse?format=#{format}&lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{http_accept_language.user_preferred_languages.join(',')}" end def fetch_text(url) diff --git a/app/views/geocoder/search.html.erb b/app/views/geocoder/search.html.erb index 53e87b3fd..f87a4909d 100644 --- a/app/views/geocoder/search.html.erb +++ b/app/views/geocoder/search.html.erb @@ -4,8 +4,7 @@ <% @sources.each do |source| %>

- <%= t(".title.results_from_html", :results_link => link_to(t(".title.#{source[:name]}"), - t(".title.#{source[:name]}_url").to_s + source[:parameters].to_s)) %> + <%= t(".title.results_from_html", :results_link => link_to(t(".title.#{source[:name]}"), source[:url].to_s)) %>

">
diff --git a/config/locales/en.yml b/config/locales/en.yml index 8de8a8721..4ad1687a6 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -671,11 +671,8 @@ en: title: results_from_html: 'Results from %{results_link}' latlon: Internal - latlon_url: https://openstreetmap.org/ osm_nominatim: OpenStreetMap Nominatim - osm_nominatim_url: https://nominatim.openstreetmap.org/ osm_nominatim_reverse: OpenStreetMap Nominatim - osm_nominatim_reverse_url: https://nominatim.openstreetmap.org/ search_osm_nominatim: prefix_format: "%{name}" prefix: diff --git a/test/http/nominatim.yml b/test/http/nominatim.yml index 35070fa73..6e7f3c03b 100644 --- a/test/http/nominatim.yml +++ b/test/http/nominatim.yml @@ -16,7 +16,7 @@ -/reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=15: +/reverse?accept-language=&format=xml&lat=51.7632&lon=-0.0076&zoom=15: code: 200 body: | @@ -33,7 +33,7 @@ -/reverse?accept-language=&lat=51.7632&lon=-0.0076&zoom=17: +/reverse?accept-language=&format=xml&lat=51.7632&lon=-0.0076&zoom=17: code: 200 body: | @@ -52,7 +52,7 @@ -/reverse?accept-language=&lat=13.7709&lon=100.50507&zoom=19: +/reverse?accept-language=&format=xml&lat=13.7709&lon=100.50507&zoom=19: code: 200 body: | -- 2.39.5