]> git.openstreetmap.org Git - rails.git/commitdiff
Added telephone links into tag detail browser
authorŠtefan Baebler <stefan.baebler@gmail.com>
Sun, 15 Jun 2014 08:30:17 +0000 (09:30 +0100)
committerTom Hughes <tom@compton.nu>
Sun, 15 Jun 2014 08:31:04 +0000 (09:31 +0100)
International phone numbers become links using the official URI scheme
as per http://tools.ietf.org/html/rfc3966.

It is not limited to key=phone, so it can be used in other keys as well.

app/helpers/browse_helper.rb
config/locales/en.yml
test/helpers/browse_helper_test.rb

index bfc7db80bf6199641de4a064951d696fa0ce3e9d..e70ec204e6446c5046a7276fcd5c39648206cd38 100644 (file)
@@ -63,6 +63,8 @@ module BrowseHelper
       link_to h(wp[:title]), wp[:url], :title => t('browse.tag_details.wikipedia_link', :page => wp[:title])
     elsif url = wiki_link("tag", "#{key}=#{value}")
       link_to h(value), url, :title => t('browse.tag_details.wiki_link.tag', :key => key, :value => value)
+    elsif url = telephone_link(key, value)
+      link_to h(value), url, :title => t('browse.tag_details.telephone_link', :phone_number => value)
     else
       linkify h(value)
     end
@@ -146,4 +148,15 @@ private
       :title => value + section
     }
   end
+
+  def telephone_link(key, value)
+    # does it look like a phone number? eg "+1 (234) 567-8901 " ?
+    return nil unless value =~ /^\s*\+[\d\s\(\)\/\.-]{6,25}\s*$/
+
+    # remove all whitespace instead of encoding it http://tools.ietf.org/html/rfc3966#section-5.1.1
+    # "+1 (234) 567-8901 " -> "+1(234)567-8901"
+    valueNoWhitespace = value.gsub(/\s+/, '')
+
+    return "tel:#{valueNoWhitespace}"
+  end
 end
index db644920e54c047bd0117134357574268b912e37..fe5f58d512cd55d4c4996a68d7421c503c6cbedb 100644 (file)
@@ -181,6 +181,7 @@ en:
         key: "The wiki description page for the %{key} tag"
         tag: "The wiki description page for the %{key}=%{value} tag"
       wikipedia_link: "The %{page} article on Wikipedia"
+      telephone_link: "Call %{phone_number}"
     note:
       title: "Note: %{id}"
       new_note: "New Note"
index 8306749409cfcf642a19257b0c4ea025480c5420..d5f7a91e6f5fe80f053bd15566624521d100970c 100644 (file)
@@ -83,6 +83,9 @@ class BrowseHelperTest < ActionView::TestCase
 
     html = format_value("unknown", "unknown")
     assert_equal "unknown", html
+
+    html = format_value("phone", "+1234567890")
+    assert_equal "<a href=\"tel:+1234567890\" title=\"Call +1234567890\">+1234567890</a>", html
   end
 
   def test_icon_tags
@@ -157,4 +160,48 @@ class BrowseHelperTest < ActionView::TestCase
     link = wikipedia_link("foo", "Test")
     assert_nil link
   end
+
+  def test_telephone_link
+    link = telephone_link("foo", "Test")
+    assert_nil link
+
+    link = telephone_link("phone", "+123")
+    assert_nil link
+
+    link = telephone_link("phone", "123")
+    assert_nil link
+
+    link = telephone_link("phone", "123 abcdefg")
+    assert_nil link
+
+    link = telephone_link("phone", "+1234567890 abc")
+    assert_nil link
+
+    link = telephone_link("phone", "+1234567890; +22334455667788")
+    assert_nil link
+
+    link = telephone_link("phone", "1234567890")
+    assert_nil link
+
+    link = telephone_link("phone", "+1234567890")
+    assert_equal "tel:+1234567890", link
+
+    link = telephone_link("phone", "+1234-567-890")
+    assert_equal "tel:+1234-567-890", link
+
+    link = telephone_link("phone", "+1234/567/890")
+    assert_equal "tel:+1234/567/890", link
+
+    link = telephone_link("phone", "+1234.567.890")
+    assert_equal "tel:+1234.567.890", link
+
+    link = telephone_link("phone", "   +1234 567-890   ")
+    assert_equal "tel:+1234567-890", link
+
+    link = telephone_link("phone", "+1 234-567-890")
+    assert_equal "tel:+1234-567-890", link
+
+    link = telephone_link("phone", "+1 (234) 567-890")
+    assert_equal "tel:+1(234)567-890", link
+  end
 end