From 5fe5777a58751e3626f645d57dafb0dd854586ec Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 28 Nov 2012 22:44:03 +0000 Subject: [PATCH] Move caching of reverse geocodes to describe_location --- app/helpers/geocoder_helper.rb | 18 +----------------- app/models/notifier.rb | 4 ++-- app/views/diary_entry/_location.html.erb | 2 -- app/views/notes/_note.rss.builder | 11 +++++------ app/views/notes/feed.rss.builder | 11 +++++------ lib/nominatim.rb | 24 ++++++++++++++++++++++++ 6 files changed, 37 insertions(+), 33 deletions(-) create mode 100644 lib/nominatim.rb diff --git a/app/helpers/geocoder_helper.rb b/app/helpers/geocoder_helper.rb index f5519ddc3..8f662bde7 100644 --- a/app/helpers/geocoder_helper.rb +++ b/app/helpers/geocoder_helper.rb @@ -22,22 +22,6 @@ module GeocoderHelper end def describe_location(lat, lon, zoom = nil, language = nil) - zoom = zoom || 14 - language = language || request.user_preferred_languages.join(',') - url = "http://nominatim.openstreetmap.org/reverse?lat=#{lat}&lon=#{lon}&zoom=#{zoom}&accept-language=#{language}" - - begin - response = OSM::Timer.timeout(4) do - REXML::Document.new(Net::HTTP.get(URI.parse(url))) - end - rescue Exception - response = nil - end - - if response and result = response.get_text("reversegeocode/result") - result.to_s - else - "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}" - end + Nominatim.describe_location(lat, lon, zoom, language) end end diff --git a/app/models/notifier.rb b/app/models/notifier.rb index a4eb0d4c3..ae50fa9b1 100644 --- a/app/models/notifier.rb +++ b/app/models/notifier.rb @@ -126,8 +126,8 @@ class Notifier < ActionMailer::Base def note_comment_notification(comment, recipient) @locale = recipient.preferred_language_from(I18n.available_locales) @noteurl = browse_note_url(comment.note, :host => SERVER_URL) - @place = comment.note.nearby_place - @comment =comment.body + @place = Nominatim.describe_location(comment.note.lat, comment.note.lon, 14, @locale) + @comment = comment.body @owner = recipient == comment.note.author @commenter = comment.author_name diff --git a/app/views/diary_entry/_location.html.erb b/app/views/diary_entry/_location.html.erb index 4779471de..a3ba473a8 100644 --- a/app/views/diary_entry/_location.html.erb +++ b/app/views/diary_entry/_location.html.erb @@ -2,8 +2,6 @@ -<% cache(:controller => 'diary_entry', :action => 'view', :display_name => location.user.display_name, :id => location.id, :part => "location") do %> <%= describe_location location.latitude, location.longitude, 14, location.language_code %> -<% end %> diff --git a/app/views/notes/_note.rss.builder b/app/views/notes/_note.rss.builder index efdae52bb..cffc3f271 100644 --- a/app/views/notes/_note.rss.builder +++ b/app/views/notes/_note.rss.builder @@ -1,13 +1,12 @@ xml.item do - location_string = Rails.cache.fetch("location_description_#{note.lat}_#{note.lon}_#{locale}") do - describe_location note.lat, note.lon, 14, locale - end + location = describe_location(note.lat, note.lon, 14, locale) + if note.status == "closed" - xml.title t('note.rss.closed', :place => location_string) + xml.title t('note.rss.closed', :place => location) elsif note.comments.length > 1 - xml.title t('note.rss.comment', :place => location_string) + xml.title t('note.rss.comment', :place => location) else - xml.title t('note.rss.new', :place => location_string) + xml.title t('note.rss.new', :place => location) end xml.link browse_note_url(note) diff --git a/app/views/notes/feed.rss.builder b/app/views/notes/feed.rss.builder index ba0e3fbfe..7113364f0 100644 --- a/app/views/notes/feed.rss.builder +++ b/app/views/notes/feed.rss.builder @@ -9,16 +9,15 @@ xml.rss("version" => "2.0", xml.link url_for(:controller => "site", :action => "index", :only_path => false) @comments.each do |comment| - location_string = Rails.cache.fetch("location_description_#{comment.note.lat}_#{comment.note.lon}_#{locale}") do - describe_location comment.note.lat, comment.note.lon, 14, locale - end + location = describe_location(comment.note.lat, comment.note.lon, 14, locale) + xml.item do if comment.event == "closed" - xml.title t('note.rss.closed', :place => location_string) + xml.title t('note.rss.closed', :place => location) elsif comment.event == "commented" - xml.title t('note.rss.comment', :place => location_string) + xml.title t('note.rss.comment', :place => location) elsif comment.event == "opened" - xml.title t('note.rss.new', :place => location_string) + xml.title t('note.rss.new', :place => location) else xml.title "unknown event" end diff --git a/lib/nominatim.rb b/lib/nominatim.rb new file mode 100644 index 000000000..5b1c7ac32 --- /dev/null +++ b/lib/nominatim.rb @@ -0,0 +1,24 @@ +module Nominatim + def self.describe_location(lat, lon, zoom = nil, language = nil) + zoom = zoom || 14 + language = language || request.user_preferred_languages.join(',') + + 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}" + + begin + response = OSM::Timer.timeout(4) do + REXML::Document.new(Net::HTTP.get(URI.parse(url))) + end + rescue Exception + response = nil + end + + if response and result = response.get_text("reversegeocode/result") + result.to_s + else + "#{number_with_precision(lat, :precision => 3)}, #{number_with_precision(lon, :precision => 3)}" + end + end + end +end -- 2.39.5