forked from alexreisner/geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Google Places Search Lookup (alexreisner#1143)
- Loading branch information
1 parent
fb1ae03
commit 3ec4111
Showing
9 changed files
with
193 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require "geocoder/lookups/google" | ||
require "geocoder/results/google_places_search" | ||
|
||
module Geocoder | ||
module Lookup | ||
class GooglePlacesSearch < Google | ||
def name | ||
"Google Places Search" | ||
end | ||
|
||
def required_api_key_parts | ||
["key"] | ||
end | ||
|
||
def supported_protocols | ||
[:https] | ||
end | ||
|
||
def query_url(query) | ||
"#{protocol}://maps.googleapis.com/maps/api/place/textsearch/json?#{url_query_string(query)}" | ||
end | ||
|
||
private | ||
|
||
def query_url_google_params(query) | ||
{ | ||
query: query.text, | ||
language: query.language || configuration.language | ||
} | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
require "geocoder/results/google" | ||
|
||
module Geocoder | ||
module Result | ||
class GooglePlacesSearch < Google | ||
|
||
def types | ||
@data["types"] || [] | ||
end | ||
|
||
def rating | ||
@data["rating"] | ||
end | ||
|
||
def photos | ||
@data["photos"] | ||
end | ||
|
||
def city | ||
"" | ||
end | ||
|
||
def state | ||
"" | ||
end | ||
|
||
def state_code | ||
"" | ||
end | ||
|
||
def province | ||
"" | ||
end | ||
|
||
def province_code | ||
"" | ||
end | ||
|
||
def postal_code | ||
"" | ||
end | ||
|
||
def country | ||
"" | ||
end | ||
|
||
def country_code | ||
"" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ | ||
"html_attributions" : [], | ||
"results" : [ | ||
{ | ||
"formatted_address" : "4 Pennsylvania Plaza, New York, NY 10001, United States", | ||
"geometry" : { | ||
"location" : { | ||
"lat" : 40.75050450000001, | ||
"lng" : -73.9934387 | ||
}, | ||
"viewport" : { | ||
"northeast" : { | ||
"lat" : 40.7523981, | ||
"lng" : -73.98975269999997 | ||
}, | ||
"southwest" : { | ||
"lat" : 40.7485721, | ||
"lng" : -73.9963951 | ||
} | ||
} | ||
}, | ||
"icon" : "https://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png", | ||
"id" : "55e3174d410b31da010030a7dfc0c9819027445a", | ||
"name" : "Madison Square Garden", | ||
"photos" : [ | ||
{ | ||
"height" : 3904, | ||
"html_attributions" : [ | ||
"\u003ca href=\"https://maps.google.com/maps/contrib/117796018192827964147/photos\"\u003eAntonio Vera\u003c/a\u003e" | ||
], | ||
"photo_reference" : "CoQBdwAAAJSRc-oTDFp3lkRwkyDN85h49Inw9YRC8U2sPUDeV0FSKzQNMxKfswp27o4Eh8gt1U6ZLPYES3RCP-2zJPswcVMtQOE9NxxM9Yg7SJllFYcC1GR_yjCJw6OGTP3_OCaL-gFI1_r54-04veyCc-UNtzjF836se6yQlSGd643zBy3_EhCYKtc3tY024iyO-6xPZUzzGhRcgC0A-itlFq-U2qmYbPde4gJU7Q", | ||
"width" : 3152 | ||
} | ||
], | ||
"place_id" : "ChIJhRwB-yFawokR5Phil-QQ3zM", | ||
"rating" : 4.5, | ||
"reference" : "CmRRAAAAqb-Y-BFyZh54ipS97aLZCfQYVVI_l87_7HQxJAXMx3rI29XzscexUUiwt7kLbr4YeDaggMQ78coK-V_yGztNvhsGbq2OsrdR-BmVpecrNGbiE9fNDPsPGvdxKcB3SPbAEhDTgnyzjLY1p7IPh2M4L9KnGhS7ZZMAtvVKPnjoCnaWP9IzdzRC4w", | ||
"types" : [ "stadium", "point_of_interest", "establishment" ] | ||
} | ||
], | ||
"status" : "OK" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"html_attributions" : [], | ||
"results" : [], | ||
"status" : "ZERO_RESULTS" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# encoding: utf-8 | ||
require 'test_helper' | ||
|
||
class GooglePlacesSearchTest < GeocoderTestCase | ||
|
||
def setup | ||
Geocoder.configure(lookup: :google_places_search) | ||
set_api_key!(:google_places_search) | ||
end | ||
|
||
def test_google_places_search_result_contains_place_id | ||
assert_equal "ChIJhRwB-yFawokR5Phil-QQ3zM", madison_square_garden.place_id | ||
end | ||
|
||
def test_google_places_search_result_contains_latitude | ||
assert_equal madison_square_garden.latitude, 40.75050450000001 | ||
end | ||
|
||
def test_google_places_search_result_contains_longitude | ||
assert_equal madison_square_garden.longitude, -73.9934387 | ||
end | ||
|
||
def test_google_places_search_result_contains_rating | ||
assert_equal 4.5, madison_square_garden.rating | ||
end | ||
|
||
def test_google_places_search_result_contains_types | ||
assert_equal madison_square_garden.types, %w(stadium point_of_interest establishment) | ||
end | ||
|
||
def test_google_places_search_query_url_contains_language | ||
url = lookup.query_url(Geocoder::Query.new("some-address", language: "de")) | ||
assert_match(/language=de/, 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) | ||
end | ||
|
||
private | ||
|
||
def lookup | ||
Geocoder::Lookup::GooglePlacesSearch.new | ||
end | ||
|
||
def madison_square_garden | ||
Geocoder.search("Madison Square Garden").first | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters