Skip to content

Commit

Permalink
Google place search (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
maximilientyc authored Jul 16, 2020
1 parent b520aee commit 31dfaa6
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
7 changes: 4 additions & 3 deletions lib/geocoder/lookups/google.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ def valid_response?(response)
super(response) and ['OK', 'ZERO_RESULTS'].include?(status)
end

def results(query)
def results(query, root_attr = 'results')
return [] unless doc = fetch_data(query)
case doc['status']; when "OK" # OK status implies >0 results
return doc['results']
case doc['status']
when "OK" # OK status implies >0 results
return doc[root_attr]
when "OVER_QUERY_LIMIT"
raise_error(Geocoder::OverQueryLimitError) ||
Geocoder.log(:warn, "#{name} API error: over query limit.")
Expand Down
30 changes: 28 additions & 2 deletions lib/geocoder/lookups/google_places_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,42 @@ def supported_protocols

private

def results(query)
super(query, 'candidates')
end

def base_query_url(query)
"#{protocol}://maps.googleapis.com/maps/api/place/textsearch/json?"
"#{protocol}://maps.googleapis.com/maps/api/place/findplacefromtext/json?"
end

def query_url_google_params(query)
{
query: query.text,
input: query.text,
inputtype: 'textquery',
fields: fields(query),
language: query.language || configuration.language
}
end

def fields(query)
query_fields = query.options[:fields]
return format_fields(query_fields) if query_fields

default_fields
end

def default_fields
legacy = %w[id reference]
basic = %w[business_status formatted_address geometry icon name
photos place_id plus_code types]
contact = %w[opening_hours]
atmosphere = %W[price_level rating user_ratings_total]
format_fields(legacy, basic, contact, atmosphere)
end

def format_fields(*fields)
fields.flatten.join(',')
end
end
end
end
3 changes: 1 addition & 2 deletions test/fixtures/google_places_search_madison_square_garden
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"html_attributions" : [],
"results" : [
"candidates" : [
{
"formatted_address" : "4 Pennsylvania Plaza, New York, NY 10001, United States",
"geometry" : {
Expand Down
32 changes: 31 additions & 1 deletion test/unit/lookups/google_places_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,39 @@ def test_google_places_search_query_url_contains_language
assert_match(/language=de/, url)
end

def test_google_places_search_query_url_contains_input
url = lookup.query_url(Geocoder::Query.new("some-address"))
assert_match(/input=some-address/, url)
end

def test_google_places_search_query_url_contains_input_typer
url = lookup.query_url(Geocoder::Query.new("some-address"))
assert_match(/inputtype=textquery/, url)
end

def test_google_places_search_query_url_always_uses_https
url = lookup.query_url(Geocoder::Query.new("some-address"))
assert_match(%r(^https://), url)
assert_match(%r{^https://}, url)
end

def test_google_places_search_query_url_contains_every_field_available_by_default
url = lookup.query_url(Geocoder::Query.new("some-address"))
fields = %w[id reference business_status formatted_address geometry icon name
photos place_id plus_code types opening_hours price_level rating
user_ratings_total]
assert_match(/fields=#{fields.join('%2C')}/, url)
end

def test_google_places_search_query_url_contains_specific_fields_when_given
fields = %w[formatted_address place_id]
url = lookup.query_url(Geocoder::Query.new("some-address", fields: fields))

assert_match(/fields=#{fields.join('%2C')}/, url)
end

def test_google_places_search_query_url_uses_find_place_service
url = lookup.query_url(Geocoder::Query.new("some-address"))
assert_match(%r{//maps.googleapis.com/maps/api/place/findplacefromtext/}, url)
end

private
Expand Down

0 comments on commit 31dfaa6

Please sign in to comment.