googlemaps-services 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,56 @@
1
+ require "googlemaps/services/util"
2
+
3
+
4
+ module GoogleMaps
5
+ module Services
6
+
7
+ # Performs requests to the Google Maps Elevation API.
8
+ #
9
+ # @example
10
+ # elevation = GoogleMaps::Services::Elevation(client)
11
+ # result = elevation.query(locations: [{:lat => 52.520645, :lng => 13.409779}, "Brussels"])
12
+ class Elevation
13
+ attr_accessor :client
14
+
15
+ def initialize(client)
16
+ self.client = client
17
+ end
18
+
19
+ # Provides elevation data for locations provided on the surface of the earth, including depth locations
20
+ # on the ocean floor (which return negative values). Provides elevation data sampled along a path on
21
+ # the surface of the earth.
22
+ #
23
+ # @param [Array] locations Array of lat/lng values from which to calculate elevation data.
24
+ # @param [String, Array] path An encoded polyline string, or an Array of lat/lng values from which to calculate elevation data.
25
+ # @param [Integer] samples The number of sample points along a path for which to return elevation data.
26
+ #
27
+ # @return [String] Valid JSON or XML response.
28
+ def query(locations: [], path: nil, samples: 0)
29
+ params = {}
30
+
31
+ if path && locations
32
+ raise StandardError, "Should not specify both path and locations."
33
+ end
34
+
35
+ if locations
36
+ params["locations"] = Convert.shortest_path(locations)
37
+ end
38
+
39
+ if path
40
+ if path.is_a? String
41
+ path = "enc:#{path}"
42
+ elsif path.is_a? Array
43
+ path = Convert.shortest_path(path)
44
+ else
45
+ raise TypeError, "Path should be either a String or an Array."
46
+ end
47
+
48
+ params = { "path" => path, "samples" => samples }
49
+ end
50
+
51
+ self.client.get(url: "/maps/api/elevation/json", params: params)["results"]
52
+ end
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,55 @@
1
+ module GoogleMaps
2
+ module Services
3
+ # Defines errors that are raised by the Google Maps client.
4
+ #
5
+ # @since 1.0.0
6
+ module Exceptions
7
+
8
+ # Represents an error raised by the remote API.
9
+ class APIError < StandardError
10
+ # @return [Symbol] The response status code.
11
+ attr_accessor :status
12
+
13
+ def initialize(status)
14
+ self.status = status
15
+ end
16
+ end
17
+
18
+ # Something went wrong while trying to execute the request.
19
+ class TransportError < StandardError
20
+ end
21
+
22
+ # Represents an unexpected HTTP error.
23
+ class HTTPError < TransportError
24
+ # @return [Symbol] status_code The response status code.
25
+ attr_accessor :status_code
26
+
27
+ def initialize(status_code)
28
+ self.status_code = status_code
29
+ end
30
+
31
+ # Returns the string representation of this error.
32
+ #
33
+ # @return [String] Human-readable error string.
34
+ def to_s
35
+ "HTTP Error: #{self.status_code}"
36
+ end
37
+ end
38
+
39
+ # Represents a timeout error.
40
+ class Timeout < Exception
41
+ # Return the string representation of this error.
42
+ #
43
+ # @return [String] Human-readable error string.
44
+ def to_s
45
+ "The request timed out."
46
+ end
47
+ end
48
+
49
+ # Signifies that the request can be retried.
50
+ class RetriableRequest < Exception
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,106 @@
1
+ require "googlemaps/services/util"
2
+
3
+
4
+ module GoogleMaps
5
+ module Services
6
+
7
+ # Performs requests to the Google Maps Geocoding API.
8
+ #
9
+ # @example
10
+ # geocode = GoogleMaps::Services::Geocode.new(client)
11
+ # result = geocode.query(address: "1600 Amphitheatre Parkway, Mountain View, CA")
12
+ class Geocode
13
+ # @return [Symbol] The HTTP client.
14
+ attr_accessor :client
15
+
16
+ def initialize(client)
17
+ self.client = client
18
+ end
19
+
20
+ # Geocoding is the process of converting addresses (like "1600 Amphitheatre Parkway, Mountain View, CA")
21
+ # into geographic coordinates (like latitude 37.423021 and longitude -122.083739), which you can use
22
+ # to place markers or position the map.
23
+ #
24
+ # @param [String] address The address to geocode.
25
+ # @param [Hash] components A component filter for which you wish to obtain a geocode.
26
+ # E.g. {'administrative_area': 'TX','country': 'US'}
27
+ # @param [Hash] bounds The bounding box of the viewport within which to bias geocode results more prominently.
28
+ # The hash must have :northeast and :southwest keys.
29
+ # @param [String] region The region code, specified as a ccTLD ("top-level domain") two-character value.
30
+ # @param [String] language The language in which to return results.
31
+ #
32
+ # @return [String] Valid JSON or XML response.
33
+ def query(address: nil, components: nil, bounds: nil, region: nil, language: nil)
34
+ params = {}
35
+
36
+ if address
37
+ params["address"] = address
38
+ end
39
+
40
+ if components
41
+ params["components"] = Convert.components(components)
42
+ end
43
+
44
+ if bounds
45
+ params["bounds"] = Convert.bounds(bounds)
46
+ end
47
+
48
+ if region
49
+ params["region"] = region
50
+ end
51
+
52
+ if language
53
+ params["language"] = language
54
+ end
55
+
56
+ self.client.get(url: "/maps/api/geocode/json", params: params)["results"]
57
+ end
58
+ end
59
+
60
+ # Performs requests to the Google Maps Geocoding API.
61
+ #
62
+ # @example
63
+ # reverse_geocode = GoogleMaps::Services::ReverseGeocode(client)
64
+ # result = reverse_geocode.query(latlng: {:lat => 52.520645, :lng => 13.409779}, language: "fr")
65
+ class ReverseGeocode
66
+ attr_accessor :client
67
+
68
+ def initialize(client)
69
+ self.client = client
70
+ end
71
+
72
+ # Reverse geocoding is the process of converting geographic coordinates into a human-readable address.
73
+ #
74
+ # @param [String, Hash] latlng The lat/lng value or place_id for which you wish to obtain the closest, human-readable address.
75
+ # @param [Array] result_type One or more address types to restrict results to.
76
+ # @param [Array] location_type One or more location types to restrict results to.
77
+ # @param [String] language The language in which to return results.
78
+ #
79
+ # @return [String] Valid JSON or XML response.
80
+ def query(latlng:, result_type: nil, location_type: nil, language: nil)
81
+ # Check if latlng param is a place_id string.
82
+ # 'place_id' strings do not contain commas; latlng strings do.
83
+ if latlng.is_a?(String) && !latlng.include?("'")
84
+ params = {"place_id" => latlng}
85
+ else
86
+ params = {"latlng" => Convert.to_latlng(latlng)}
87
+ end
88
+
89
+ if result_type
90
+ params["result_type"] = Convert.join_array("|", result_type)
91
+ end
92
+
93
+ if location_type
94
+ params["location_type"] = Convert.join_array("|", location_type)
95
+ end
96
+
97
+ if language
98
+ params["language"] = language
99
+ end
100
+
101
+ self.client.get(url: "/maps/api/geocode/json", params: params)["results"]
102
+ end
103
+ end
104
+
105
+ end
106
+ end
@@ -0,0 +1,245 @@
1
+ require "googlemaps/services/util"
2
+
3
+ module GoogleMaps
4
+ module Services
5
+
6
+ # Performs requests to the Google Places API.
7
+ class Places
8
+ # @return [Symbol] The HTTP client.
9
+ attr_accessor :client
10
+
11
+ def initialize(client)
12
+ self.client = client
13
+ end
14
+
15
+ # Performs places search.
16
+ #
17
+ # @param [String] query The text string on which to search. E.g. "restaurant".
18
+ # @param [String, Hash] location The lat/lng value for which you wish to obtain the closest, human-readable address.
19
+ # @param [Integer] radius Distance in meters within which to bias results.
20
+ # @param [String] language The language in which to return results.
21
+ # @param [Integer] min_price Restricts results to only those places with no less than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).
22
+ # @param [Integer] max_price Restricts results to only those places with no greater than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).
23
+ # @param [TrueClass, FalseClass] open_now Return only those places that are open for business at the time the query is sent.
24
+ # @param [String] type Restricts the results to places matching the specified type. The full list of supported types is available here: https://developers.google.com/places/supported_types
25
+ # @param [String] page_token Token from a previous search that when provided will returns the next page of results for the same search.
26
+ #
27
+ # @return [Hash] Valid JSON or XML response.
28
+ def search(query:, location: nil, radius: nil, language: nil, min_price: nil,
29
+ max_price: nil, open_now: false, type: nil, page_token: nil)
30
+ _places(url_part: "text", query: query, location: location, radius: radius,
31
+ language: language, min_price: min_price, max_price: max_price,
32
+ open_now: open_now, type: type, page_token: page_token)
33
+ end
34
+
35
+ # Performs nearby search for places.
36
+ #
37
+ # @param [String, Hash] location The lat/lng value for which you wish to obtain the closest, human-readable address.
38
+ # @param [Integer] radius Distance in meters within which to bias results.
39
+ # @param [String] keyword A term to be matched against all content that Google has indexed for this place.
40
+ # @param [String] language The language in which to return results.
41
+ # @param [Integer] min_price Restricts results to only those places with no less than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).
42
+ # @param [Integer] max_price Restricts results to only those places with no greater than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).
43
+ # @param [Array] name One or more terms to be matched against the names of places.
44
+ # @param [TrueClass, FalseClass] open_now Return only those places that are open for business at the time the query is sent.
45
+ # @param [String] rank_by Specifies the order in which results are listed. Possible values are: prominence (default), distance
46
+ # @param [String] type Restricts the results to places matching the specified type. The full list of supported types is available here: https://developers.google.com/places/supported_types
47
+ # @param [String] page_token Token from a previous search that when provided will returns the next page of results for the same search.
48
+ #
49
+ # @return [Hash] Valid JSON or XML response.
50
+ def nearby(location:, radius: nil, keyword: nil, language: nil, min_price: nil,
51
+ max_price: nil, name: nil, open_now: false, rank_by: nil, type: nil, page_token: nil)
52
+ if rank_by == "distance"
53
+ if !(keyword || name || type)
54
+ raise StandardError, "either a keyword, name or type arg is required when rank_by is set to distance."
55
+ elsif radius
56
+ raise StandardError, "radius cannot be specified when rank_by is set to distance."
57
+ end
58
+ end
59
+
60
+ _places(url_part: "nearby", location: location, radius: radius, keyword: keyword, language: language,
61
+ min_price: min_price, max_price: max_price, name: name, open_now: open_now, rank_by: rank_by,
62
+ type: type, page_token: page_token)
63
+ end
64
+
65
+ # Performs radar search for places
66
+ #
67
+ # @param [String, Hash] location The latitude/longitude value for which you wish to obtain the closest, human-readable address.
68
+ # @param [Integer] radius Distance in meters within which to bias results.
69
+ # @param [String] keyword A term to be matched against all content that Google has indexed for this place.
70
+ # @param [Integer] min_price Restricts results to only those places with no less than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).
71
+ # @param [Integer] max_price Restricts results to only those places with no greater than this price level. Valid values are in the range from 0 (most affordable) to 4 (most expensive).
72
+ # @param [Array] name One or more terms to be matched against the names of places.
73
+ # @param [TrueClass, FalseClass] open_now Return only those places that are open for business at the time the query is sent.
74
+ # @param [String] type: Restricts the results to places matching the specified type. The full list of supported types is available here: https://developers.google.com/places/supported_types
75
+ #
76
+ # @return [Hash] Valid JSON or XML response.
77
+ def radar(location:, radius:, keyword: nil, min_price: nil,
78
+ max_price: nil, name: nil, open_now: false, type: nil)
79
+ if !(keyword || name || type)
80
+ raise StandardError, "either a keyword, name, or type arg is required."
81
+ end
82
+
83
+ _places(url_part: "radar", location: location, radius: radius,
84
+ keyword: keyword, min_price: min_price, max_price: max_price,
85
+ name: name, open_now: open_now, type: type)
86
+ end
87
+
88
+ # Handler for "places", "places_nearby" and "places_radar" queries.
89
+ # @private
90
+ def _places(url_part:, query: nil, location: nil, radius: nil, keyword: nil, language: nil,
91
+ min_price: 0, max_price: 4, name: nil, open_now: false, rank_by: nil, type: nil,
92
+ page_token: nil)
93
+ params = { "minprice" => min_price, "maxprice" => max_price }
94
+
95
+ if query
96
+ params["query"] = query
97
+ end
98
+
99
+ if location
100
+ params["location"] = Convert.to_latlng(location)
101
+ end
102
+
103
+ if radius
104
+ params["radius"] = radius
105
+ end
106
+
107
+ if keyword
108
+ params["keyword"] = keyword
109
+ end
110
+
111
+ if language
112
+ params["language"] = language
113
+ end
114
+
115
+ if name
116
+ params["name"] = Convert.join_array(" ", name)
117
+ end
118
+
119
+ if open_now
120
+ params["opennow"] = "true"
121
+ end
122
+
123
+ if rank_by
124
+ params["rankby"] = rank_by
125
+ end
126
+
127
+ if type
128
+ params["type"] = type
129
+ end
130
+
131
+ if page_token
132
+ params["pagetoken"] = page_token
133
+ end
134
+
135
+ self.client.get(url: "/maps/api/place/#{url_part}search/json", params: params)
136
+ end
137
+
138
+
139
+ # Comprehensive details for an individual place.
140
+ #
141
+ # @param [String] place_id A textual identifier that uniquely identifies a place, returned from a Places search.
142
+ # @param [String] language The language in which to return results.
143
+ #
144
+ # @return [Hash] Valid JSON or XML response.
145
+ def place_details(place_id:, language: nil)
146
+ params = { "placeid" => place_id }
147
+ if language
148
+ params["language"] = language
149
+ end
150
+
151
+ self.client.get(url: "/maps/api/place/details/json", params: params)
152
+ end
153
+
154
+ # Downloads a photo from the Places API.
155
+ #
156
+ # @param [String] photo_reference: A string identifier that uniquely identifies a photo, as provided by either a Places search or Places detail request.
157
+ # @param [Integer] max_width: Specifies the maximum desired width, in pixels.
158
+ # @param [Integer] max_height: Specifies the maximum desired height, in pixels.
159
+ #
160
+ # @return [String] URL of the photo.
161
+ def place_photo(photo_reference:, max_width: nil, max_height: nil)
162
+ if !(max_width || max_height)
163
+ raise StandardError, "a max_width or max_height arg is required"
164
+ end
165
+
166
+ params = {"photoreference" => photo_reference}
167
+
168
+ if max_width
169
+ params["maxwidth"] = max_width
170
+ end
171
+
172
+ if max_height
173
+ params["maxheight"] = max_height
174
+ end
175
+
176
+ self.client.get(url: "/maps/api/place/photo", params: params)
177
+ end
178
+
179
+ # Returns Place predictions given a textual search string and optional geographic bounds.
180
+ #
181
+ # @param [String] input_text The text string on which to search.
182
+ # @param [Integer] offset The position, in the input term, of the last character that the service uses to match predictions. For example, if the input is 'Google' and the offset is 3, the service will match on 'Goo'.
183
+ # @param [String, Hash] location The latitude/longitude value for which you wish to obtain the closest, human-readable address.
184
+ # @param [Integer] radius Distance in meters within which to bias results.
185
+ # @param [String] language The language in which to return results.
186
+ # @param [String] type Restricts the results to places matching the specified type. The full list of supported types is available here: https://developers.google.com/places/web-service/autocomplete#place_types
187
+ # @param [Hash] components A component filter for which you wish to obtain a geocode, e.g. "{'administrative_area': 'TX','country': 'US'}"
188
+ #
189
+ # @return [Array] Array of predictions.
190
+ def autocomplete(input_text:, offset: nil, location: nil, radius: nil, language: nil, type: nil, components: nil)
191
+ _autocomplete(url_part: "", input_text: input_text, offset: offset, location: location,
192
+ radius: radius, language: language, type: type, components: components)
193
+ end
194
+
195
+ # Returns Place predictions given a textual search query, such as "pizza near Brussels", and optional geographic bounds.
196
+ #
197
+ # @param [String] input_text The text query on which to search.
198
+ # @param [Integer] offset The position, in the input term, of the last character that the service uses to match predictions. For example, if the input is 'Google' and the offset is 3, the service will match on 'Goo'.
199
+ # @param [String, Hash] location The latitude/longitude value for which you wish to obtain the closest, human-readable address.
200
+ # @param [Integer] radius Distance in meters within which to bias results.
201
+ # @param [String] language The language in which to return results.
202
+ #
203
+ # @return [Array] Array of predictions.
204
+ def autocomplete_query(input_text:, offset: nil, location: nil, radius: nil, language: nil)
205
+ _autocomplete(url_part: "query", input_text: input_text, offset: offset,
206
+ location: location, radius: radius, language: language)
207
+ end
208
+
209
+ # Handler for "autocomplete" and "autocomplete_query" queries.
210
+ # @private
211
+ def _autocomplete(url_part:, input_text:, offset: nil, location: nil,
212
+ radius: nil, language: nil, type: nil, components: nil)
213
+ params = { "input" => input_text }
214
+
215
+ if offset
216
+ params["offset"] = offset
217
+ end
218
+
219
+ if location
220
+ params["location"] = Convert.to_latlng(location)
221
+ end
222
+
223
+ if radius
224
+ params["radius"] = radius
225
+ end
226
+
227
+ if language
228
+ params["language"] = language
229
+ end
230
+
231
+ if type
232
+ params["type"] = type
233
+ end
234
+
235
+ if components
236
+ params["components"] = Convert.components(components)
237
+ end
238
+
239
+ self.client.get(url: "/maps/api/place/#{url_part}autocomplete/json", params: params)["predictions"]
240
+ end
241
+
242
+ private :_places, :_autocomplete
243
+ end
244
+ end
245
+ end