]> git.openstreetmap.org Git - rails.git/blobdiff - lib/nominatim.rb
Merge remote-tracking branch 'upstream/pull/5017'
[rails.git] / lib / nominatim.rb
index 1d2037d8d171c30badf2a07feb5b7d5ceb92eafa..5e4a18b519733d39e5fc7b770e0d3cfb61729c8d 100644 (file)
@@ -1,23 +1,27 @@
 module Nominatim
 module Nominatim
+  require "timeout"
+
   extend ActionView::Helpers::NumberHelper
 
   def self.describe_location(lat, lon, zoom = nil, language = nil)
   extend ActionView::Helpers::NumberHelper
 
   def self.describe_location(lat, lon, zoom = nil, language = nil)
-    zoom = zoom || 14
-    language = language || request.user_preferred_languages.join(',')
+    zoom ||= 14
+    language ||= http_accept_language.user_preferred_languages.join(",")
 
     Rails.cache.fetch "/nominatim/location/#{lat}/#{lon}/#{zoom}/#{language}" do
 
     Rails.cache.fetch "/nominatim/location/#{lat}/#{lon}/#{zoom}/#{language}" do
-      url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
+      url = "#{Settings.nominatim_url}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
 
       begin
 
       begin
-        response = OSM::Timer.timeout(4) do
-          REXML::Document.new(Net::HTTP.get(URI.parse(url)))
+        response = OSM.http_client.get(URI.parse(url)) do |request|
+          request.options.timeout = 4
         end
         end
-      rescue Exception
-        response = nil
+
+        results = REXML::Document.new(response.body) if response.success?
+      rescue StandardError
+        results = nil
       end
 
       end
 
-      if response and result = response.get_text("reversegeocode/result")
-        result.to_s
+      if results && result = results.get_text("reversegeocode/result")
+        result.value
       else
         "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
       end
       else
         "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
       end