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