Skip to content

Commit

Permalink
Merge pull request alexreisner#498 from arnoldclark/master
Browse files Browse the repository at this point in the history
Allow bing to search restricted by region
  • Loading branch information
alexreisner committed Jan 30, 2014
2 parents a25e3d7 + 3a390fb commit 2273951
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 4 deletions.
16 changes: 12 additions & 4 deletions lib/geocoder/lookups/bing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ def required_api_key_parts
end

def query_url(query)
"#{protocol}://dev.virtualearth.net/REST/v1/Locations" +
(query.reverse_geocode? ? "/#{query.sanitized_text}?" : "?") +
url_query_string(query)
base_url(query) + url_query_string(query)
end

private # ---------------------------------------------------------------

def base_url(query)
["#{protocol}://dev.virtualearth.net/REST/v1/Locations",
query.options[:region],
sanitized_text(query)].compact.join("/") + "?"
end

def sanitized_text(query)
URI.escape(query.sanitized_text.strip) if !query.reverse_geocode?
end

def results(query)
return [] unless doc = fetch_data(query)

Expand All @@ -40,7 +48,7 @@ def results(query)
def query_url_params(query)
{
:key => configuration.api_key,
:query => query.reverse_geocode? ? nil : query.sanitized_text
:query => query.reverse_geocode? ? query.sanitized_text : nil
}.merge(super)
end
end
Expand Down
39 changes: 39 additions & 0 deletions test/services_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,45 @@ def test_bing_no_results
assert_equal 0, results.length
end

def test_bing_query_url_contains_region
lookup = Geocoder::Lookup::Bing.new
url = lookup.query_url(Geocoder::Query.new(
"manchester",
:region => "uk"
))
assert_match /Locations\/uk\/manchester/, url
assert_no_match /query/, url
end

def test_bing_query_url_without_region
lookup = Geocoder::Lookup::Bing.new
url = lookup.query_url(Geocoder::Query.new(
"manchester"
))
assert_match /Locations\/manchester/, url
assert_no_match /query/, url
end

def test_bing_query_url_contains_address_with_spaces
lookup = Geocoder::Lookup::Bing.new
url = lookup.query_url(Geocoder::Query.new(
"manchester, lancashire",
:region => "uk"
))
assert_match /Locations\/uk\/manchester,%20lancashire/, url
assert_no_match /query/, url
end

def test_bing_query_url_contains_address_with_trailing_and_leading_spaces
lookup = Geocoder::Lookup::Bing.new
url = lookup.query_url(Geocoder::Query.new(
" manchester, lancashire ",
:region => "uk"
))
assert_match /Locations\/uk\/manchester,%20lancashire/, url
assert_no_match /query/, url
end

# --- Nominatim ---

def test_nominatim_result_components
Expand Down

0 comments on commit 2273951

Please sign in to comment.