Skip to content

Commit ec2f034

Browse files
authored
Merge pull request #1672 from iBlackShadow/fix-url-encoding
Fix spaces in a url being encoded as '+' instead of '%20' in lookups that use URL path instead of query string (Mapbox and Bing).
2 parents c1a19f2 + 5c2c6d0 commit ec2f034

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

lib/geocoder/lookups/bing.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def required_api_key_parts
1919
private # ---------------------------------------------------------------
2020

2121
def base_query_url(query)
22-
text = CGI.escape(query.sanitized_text.strip)
22+
text = ERB::Util.url_encode(query.sanitized_text.strip)
2323
url = "#{protocol}://dev.virtualearth.net/REST/v1/Locations/"
2424
if query.reverse_geocode?
2525
url + "#{text}?"

lib/geocoder/lookups/mapbox.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ def query_url_params(query)
3030
end
3131

3232
def mapbox_search_term(query)
33-
require 'cgi' unless defined?(CGI) && defined?(CGI.escape)
33+
require 'erb' unless defined?(ERB) && defined?(ERB::Util.url_encode)
3434
if query.reverse_geocode?
3535
lat,lon = query.coordinates
36-
"#{CGI.escape lon},#{CGI.escape lat}"
36+
"#{ERB::Util.url_encode lon},#{ERB::Util.url_encode lat}"
3737
else
3838
# truncate at first semicolon so Mapbox doesn't go into batch mode
3939
# (see Github issue #1299)
40-
CGI.escape query.text.to_s.split(';').first.to_s
40+
ERB::Util.url_encode query.text.to_s.split(';').first.to_s
4141
end
4242
end
4343

test/unit/lookups/bing_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def test_query_url_escapes_spaces_in_address
6060
"manchester, lancashire",
6161
:region => "uk"
6262
))
63-
assert_match(%r!Locations/uk/\?q=manchester%2C\+lancashire!, url)
63+
assert_match(%r!Locations/uk/\?q=manchester%2C%20lancashire!, url)
6464
end
6565

6666
def test_query_url_strips_trailing_and_leading_spaces
@@ -69,7 +69,7 @@ def test_query_url_strips_trailing_and_leading_spaces
6969
" manchester, lancashire ",
7070
:region => "uk"
7171
))
72-
assert_match(%r!Locations/uk/\?q=manchester%2C\+lancashire!, url)
72+
assert_match(%r!Locations/uk/\?q=manchester%2C%20lancashire!, url)
7373
end
7474

7575
def test_raises_exception_when_service_unavailable

test/unit/lookups/mapbox_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ def setup
1212
def test_url_contains_api_key
1313
Geocoder.configure(mapbox: {api_key: "abc123"})
1414
query = Geocoder::Query.new("Leadville, CO")
15-
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C+CO.json?access_token=abc123", query.url
15+
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C%20CO.json?access_token=abc123", query.url
1616
end
1717

1818
def test_url_contains_params
1919
Geocoder.configure(mapbox: {api_key: "abc123"})
2020
query = Geocoder::Query.new("Leadville, CO", {params: {country: 'CN'}})
21-
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C+CO.json?access_token=abc123&country=CN", query.url
21+
assert_equal "https://api.mapbox.com/geocoding/v5/mapbox.places/Leadville%2C%20CO.json?access_token=abc123&country=CN", query.url
2222
end
2323

2424
def test_result_components

0 commit comments

Comments
 (0)