Adding 'shortlink' functions which will allow URLs like http://osm.org/go/XXXX suitable for use in twitter, etc...
This commit is contained in:
parent
2f6aab7124
commit
1d8e66016c
9 changed files with 213 additions and 2 deletions
35
test/integration/short_link_test.rb
Normal file
35
test/integration/short_link_test.rb
Normal file
|
@ -0,0 +1,35 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class ShortLinkTest < ActionController::IntegrationTest
|
||||
##
|
||||
# test the short link with various parameters and ensure they're
|
||||
# kept in the redirect.
|
||||
def test_short_link_params
|
||||
assert_short_link_redirect('1N8H@P_5W')
|
||||
assert_short_link_redirect('euu4oTas==')
|
||||
end
|
||||
|
||||
##
|
||||
# utility method to test short links
|
||||
def assert_short_link_redirect(short_link)
|
||||
lon, lat, zoom = ShortLink::decode(short_link)
|
||||
|
||||
# test without marker
|
||||
get '/go/' + short_link
|
||||
assert_redirected_to :controller => 'site', :action => 'index', :lat => lat, :lon => lon, :zoom => zoom
|
||||
|
||||
# test with marker
|
||||
get '/go/' + short_link + "?m"
|
||||
assert_redirected_to :controller => 'site', :action => 'index', :mlat => lat, :mlon => lon, :zoom => zoom
|
||||
|
||||
# test with layers and a marker
|
||||
get '/go/' + short_link + "?m&layers=B000FTF"
|
||||
assert_redirected_to :controller => 'site', :action => 'index', :mlat => lat, :mlon => lon, :zoom => zoom, :layers => "B000FTF"
|
||||
get '/go/' + short_link + "?layers=B000FTF&m"
|
||||
assert_redirected_to :controller => 'site', :action => 'index', :mlat => lat, :mlon => lon, :zoom => zoom, :layers => "B000FTF"
|
||||
|
||||
# test with some random query parameters we haven't even implemented yet
|
||||
get '/go/' + short_link + "?foobar=yes"
|
||||
assert_redirected_to :controller => 'site', :action => 'index', :lat => lat, :lon => lon, :zoom => zoom, :foobar => "yes"
|
||||
end
|
||||
end
|
26
test/unit/short_link_test.rb
Normal file
26
test/unit/short_link_test.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class ShortLinkTest < ActiveSupport::TestCase
|
||||
##
|
||||
# tests that encoding and decoding are working to within
|
||||
# the acceptable quantisation range.
|
||||
def test_encode_decode
|
||||
cases = Array.new
|
||||
1000.times do
|
||||
cases << [ 180.0 * rand - 90.0, 360.0 * rand - 180.0, (18 * rand).to_i ]
|
||||
end
|
||||
|
||||
cases.each do |lat, lon, zoom|
|
||||
lon2, lat2, zoom2 = ShortLink.decode(ShortLink.encode(lon, lat, zoom))
|
||||
# zooms should be identical
|
||||
assert_equal zoom, zoom2, "Decoding a encoded short link gives different zoom for (#{lat}, #{lon}, #{zoom})."
|
||||
# but the location has a quantisation error introduced at roughly
|
||||
# one pixel (i.e: zoom + 8). the sqrt(5) is because each position
|
||||
# has an extra bit of accuracy in the lat coordinate, due to the
|
||||
# smaller range.
|
||||
distance = Math.sqrt((lat - lat2) ** 2 + (lon - lon2) ** 2)
|
||||
max_distance = 360.0 / (1 << (zoom + 8)) * 0.5 * Math.sqrt(5)
|
||||
assert max_distance > distance, "Maximum expected error exceeded: #{max_distance} <= #{distance} for (#{lat}, #{lon}, #{zoom})."
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue