Skip to content

Commit

Permalink
Add support languages to query
Browse files Browse the repository at this point in the history
  • Loading branch information
AhlOct committed Nov 11, 2024
1 parent 77d3dd9 commit fe7a131
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
28 changes: 19 additions & 9 deletions lib/geocoder/lookups/azure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,25 @@ def supported_protocols
private

def base_query_url(query)
host = 'atlas.microsoft.com/search/address'
url = if query.reverse_geocode?
"#{protocol}://#{host}/reverse/json"
else
"#{protocol}://#{host}/json"
end
params = "?subscription-key=#{configuration.api_key}&api-version=1.0&query=#{query.sanitized_text}&limit=#{configuration.limit}"

url + params
host = 'atlas.microsoft.com/search/address'

if query.reverse_geocode?
"#{protocol}://#{host}/reverse/json?"
else
"#{protocol}://#{host}/json?"
end
end

def query_url_params(query)
params = {
'api-version' => 1.0,
'language' => query.options[:language] || 'en',
'limit' => configuration.limit || 10,
'query' => query.sanitized_text,
'subscription-key' => configuration.api_key
}

params.merge!(super)
end

def results(query)
Expand Down
20 changes: 16 additions & 4 deletions test/unit/lookups/azure_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ def test_azure_results_no_street_number
def test_query_url
query = Geocoder::Query.new('Jakarta')

assert_equal 'https://atlas.microsoft.com/search/address/json?subscription-key=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&api-version=1.0&query=Jakarta&limit=1', query.url
assert_equal 'https://atlas.microsoft.com/search/address/json?api-version=1.0&language=en&limit=1&query=Jakarta&subscription-key=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', query.url
end

def test_reverse_query_url
query = Geocoder::Query.new([-6.198967624433219, 106.82358133258361])

assert_equal "https://atlas.microsoft.com/search/address/reverse/json?subscription-key=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa&api-version=1.0&query=-6.198967624433219,106.82358133258361&limit=1", query.url
assert_equal "https://atlas.microsoft.com/search/address/reverse/json?api-version=1.0&language=en&limit=1&query=-6.198967624433219%2C106.82358133258361&subscription-key=aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", query.url
end

def test_azure_query_url_contains_api_key
Expand All @@ -83,6 +83,18 @@ def test_azure_query_url_contains_api_key
assert_match(/subscription-key=a+/, url)
end

def test_azure_query_url_contains_language
lookup = Geocoder::Lookup::Azure.new
url = lookup.query_url(
Geocoder::Query.new(
'Test Query',
language: 'en'
)
)

assert_match(/language=en/, url)
end

def test_azure_query_url_contains_text
lookup = Geocoder::Lookup::Azure.new
url = lookup.query_url(
Expand All @@ -91,7 +103,7 @@ def test_azure_query_url_contains_text
)
)

assert_match(/PT Kulkul Teknologi Internasional/i, url)
assert_match(/PT\+Kulkul\+Teknologi\+Internasional/i, url)
end

def test_azure_reverse_query_url_contains_lat_lon
Expand All @@ -102,7 +114,7 @@ def test_azure_reverse_query_url_contains_lat_lon
)
)

assert_match(/query=-6\.198967624433219,106\.82358133258361/, url)
assert_match(/query=-6\.198967624433219%2C106\.82358133258361/, url)
end

def test_azure_invalid_key
Expand Down

0 comments on commit fe7a131

Please sign in to comment.