]> git.openstreetmap.org Git - rails.git/blob - lib/nominatim.rb
Merge tooltip fixes/tests
[rails.git] / lib / nominatim.rb
1 module Nominatim
2   require "timeout"
3
4   extend ActionView::Helpers::NumberHelper
5
6   def self.describe_location(lat, lon, zoom = nil, language = nil)
7     zoom ||= 14
8     language ||= http_accept_language.user_preferred_languages.join(",")
9
10     Rails.cache.fetch "/nominatim/location/#{lat}/#{lon}/#{zoom}/#{language}" do
11       url = "#{Settings.nominatim_url}reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}"
12
13       begin
14         response = Timeout.timeout(4) do
15           REXML::Document.new(Net::HTTP.get(URI.parse(url)))
16         end
17       rescue StandardError
18         response = nil
19       end
20
21       if response && result = response.get_text("reversegeocode/result")
22         result.to_s
23       else
24         "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}"
25       end
26     end
27   end
28 end