Move caching of reverse geocodes to describe_location

This commit is contained in:
Tom Hughes 2012-11-28 22:44:03 +00:00
parent 0a3aba7f89
commit 5fe5777a58
6 changed files with 37 additions and 33 deletions

View file

@ -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

View file

@ -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

View file

@ -2,8 +2,6 @@
<a href="<%= url_for :controller => 'site', :action => 'index', :lat => location.latitude, :lon => location.longitude, :zoom => 14 %>">
<abbr class="geo" title="<%= number_with_precision(location.latitude, :precision => 4) %>; <%= number_with_precision(location.longitude, :precision => 4) %>">
<% 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 %>
</abbr>
</a>

View file

@ -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)

View file

@ -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

24
lib/nominatim.rb Normal file
View file

@ -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