Skip to content

Commit

Permalink
Merge pull request geokit#234 from rafaelpetry/master
Browse files Browse the repository at this point in the history
Escape special characters in Google API components filter
  • Loading branch information
dsantosmerino committed Oct 15, 2019
2 parents 4773681 + 8ea2646 commit 3ee032d
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 2 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/geokit/geocoders/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ def self.construct_bias_string_from_options(bias)

def self.construct_components_string_from_options(components={})
unless components.empty?
"&components=#{components.to_a.map { |pair| pair.join(':').downcase }.join(CGI.escape('|'))}"
escaped_components = Geokit::Inflector.url_escape(components.to_a.map { |pair| pair.join(":").downcase }.join("|"))
"&components=#{escaped_components}"
end
end

Expand Down
11 changes: 10 additions & 1 deletion test/test_google_geocoder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ def test_component_filtering
assert_equal 'TX', filtered_result.state
assert_equal 'Austin, TX, USA', filtered_result.full_address

url = 'https://maps.google.com/maps/api/geocode/json?sensor=false&address=austin&components=administrative_area:il%7Ccountry:us'
url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=austin&components=administrative_area%3Ail%7Ccountry%3Aus"
TestHelper.expects(:last_url).with(url)
filtered_result = geocode('austin',
:test_component_filtering_on,
Expand All @@ -368,5 +368,14 @@ def test_component_filtering

assert_equal 'TX', filtered_result.state
assert_equal 'Austin, TX, USA', filtered_result.full_address

url = "https://maps.google.com/maps/api/geocode/json?sensor=false&address=S%C3%A3o+Paulo&components=administrative_area%3As%C3%A3o+paulo%7Ccountry%3Abr"
TestHelper.expects(:last_url).with(url)
filtered_result = geocode("São Paulo",
:test_component_filtering_with_special_characters,
components: { administrative_area: "São Paulo", country: "BR" })

assert_equal "SP", filtered_result.state
assert_equal "São Paulo, State of São Paulo, Brazil", filtered_result.full_address
end
end

0 comments on commit 3ee032d

Please sign in to comment.