Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README_API_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,20 @@ The [Google Places Details API](https://developers.google.com/maps/documentation
* **Region**: world
* **SSL support**: yes
* **Languages**: ar, eu, bg, bn, ca, cs, da, de, el, en, en-AU, en-GB, es, eu, fa, fi, fil, fr, gl, gu, hi, hr, hu, id, it, iw, ja, kn, ko, lt, lv, ml, mr, nl, no, pl, pt, pt-BR, pt-PT, ro, ru, sk, sl, sr, sv, tl, ta, te, th, tr, uk, vi, zh-CN, zh-TW (see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1)
* **Extra params**:
* `:fields` - Requested API response fields (affects pricing, see the [Google Places Details developer guide](https://developers.google.com/maps/documentation/places/web-service/details#fields) for available fields)
* **Documentation**: https://developers.google.com/maps/documentation/places/web-service/details
* **Terms of Service**: https://developers.google.com/maps/documentation/places/web-service/policies
* **Limitations**: "If your application displays Places API data on a page or view that does not also display a Google Map, you must show a "Powered by Google" logo with that data."
* **Notes**:
* You can set the default fields for all queries in the Geocoder configuration, for example:
```rb
Geocoder.configure(
google_places_details: {
fields: %w[business_status formatted_address geometry name photos place_id plus_code types]
}
)
```

### Google Places Search (`:google_places_search`)

Expand All @@ -173,6 +184,16 @@ The [Google Places Search API](https://developers.google.com/maps/documentation/
* **Documentation**: https://developers.google.com/maps/documentation/places/web-service/search
* **Terms of Service**: https://developers.google.com/maps/documentation/places/web-service/policies
* **Limitations**: "If your application displays Places API data on a page or view that does not also display a Google Map, you must show a "Powered by Google" logo with that data."
* **Notes**:
* You can set the default fields for all queries in the Geocoder configuration, for example:
```rb
Geocoder.configure(
google_places_search: {
fields: %w[address_components adr_address business_status formatted_address geometry name
photos place_id plus_code types url utc_offset vicinity]
}
)
```

### Here/Nokia (`:here`)

Expand Down
20 changes: 20 additions & 0 deletions lib/geocoder/lookups/google_places_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,29 @@ def results(query)
result
end

def fields(query)
if query.options.has_key?(:fields)
return format_fields(query.options[:fields])
end

if configuration.has_key?(:fields)
return format_fields(configuration[:fields])
end

nil # use Google Places defaults
end

def format_fields(*fields)
flattened = fields.flatten.compact
return if flattened.empty?

flattened.join(',')
end

Comment on lines +36 to +54
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This repeats code in GooglePlacesSearch; let me know if you think these methods should be abstracted out to a base class GooglePlaces < Google. I felt it was arguable whether this was enough to justify it.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. I agree that it's not enough to merit the complexity of a new base class.

def query_url_google_params(query)
{
placeid: query.text,
fields: fields(query),
language: query.language || configuration.language
}
end
Expand Down
14 changes: 11 additions & 3 deletions lib/geocoder/lookups/google_places_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ def query_url_google_params(query)
end

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

if configuration.has_key?(:fields)
return format_fields(configuration[:fields])
end

default_fields
end
Expand All @@ -52,7 +57,10 @@ def default_fields
end

def format_fields(*fields)
fields.flatten.join(',')
flattened = fields.flatten.compact
return if flattened.empty?

flattened.join(',')
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions test/unit/lookups/google_places_details_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,39 @@ def test_google_places_details_query_url_always_uses_https
assert_match(%r(^https://), url)
end

def test_google_places_details_query_url_omits_fields_by_default
url = lookup.query_url(Geocoder::Query.new("some-place-id"))
assert_no_match(/fields=/, url)
end

def test_google_places_details_query_url_contains_specific_fields_when_given
fields = %w[formatted_address place_id]
url = lookup.query_url(Geocoder::Query.new("some-place-id", fields: fields))
assert_match(/fields=#{fields.join('%2C')}/, url)
end

def test_google_places_details_query_url_contains_specific_fields_when_configured
fields = %w[business_status geometry photos]
Geocoder.configure(google_places_details: {fields: fields})
url = lookup.query_url(Geocoder::Query.new("some-place-id"))
assert_match(/fields=#{fields.join('%2C')}/, url)
Geocoder.configure(google_places_details: {})
end

def test_google_places_details_query_url_omits_fields_when_nil_given
Geocoder.configure(google_places_details: {fields: %w[business_status geometry photos]})
url = lookup.query_url(Geocoder::Query.new("some-place-id", fields: nil))
assert_no_match(/fields=/, url)
Geocoder.configure(google_places_details: {})
end

def test_google_places_details_query_url_omits_fields_when_nil_configured
Geocoder.configure(google_places_details: {fields: nil})
url = lookup.query_url(Geocoder::Query.new("some-place-id"))
assert_no_match(/fields=/, url)
Geocoder.configure(google_places_details: {})
end

def test_google_places_details_result_with_no_reviews_shows_empty_reviews
assert_equal no_reviews_result.reviews, []
end
Expand Down
20 changes: 20 additions & 0 deletions test/unit/lookups/google_places_search_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ def test_google_places_search_query_url_contains_specific_fields_when_given
assert_match(/fields=#{fields.join('%2C')}/, url)
end

def test_google_places_search_query_url_contains_specific_fields_when_configured
fields = %w[business_status geometry photos]
Geocoder.configure(google_places_search: {fields: fields})
url = lookup.query_url(Geocoder::Query.new("some-address"))
assert_match(/fields=#{fields.join('%2C')}/, url)
Geocoder.configure(google_places_search: {})
end

def test_google_places_search_query_url_omits_fields_when_nil_given
url = lookup.query_url(Geocoder::Query.new("some-address", fields: nil))
assert_no_match(/fields=/, url)
end

def test_google_places_search_query_url_omits_fields_when_nil_configured
Geocoder.configure(google_places_search: {fields: nil})
url = lookup.query_url(Geocoder::Query.new("some-address"))
assert_no_match(/fields=/, url)
Geocoder.configure(google_places_search: {})
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)
Expand Down