-
Notifications
You must be signed in to change notification settings - Fork 190
/
request.rb
368 lines (347 loc) · 17.8 KB
/
request.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
module GooglePlaces
# This class performs the queries on the API
class Request
# @return [HTTParty::Response] the retrieved response from the API
attr_accessor :response
attr_reader :options
include ::HTTParty
format :json
NEARBY_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/nearbysearch/json'
DETAILS_URL = 'https://maps.googleapis.com/maps/api/place/details/json'
PHOTO_URL = 'https://maps.googleapis.com/maps/api/place/photo'
TEXT_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/textsearch/json'
PAGETOKEN_URL = 'https://maps.googleapis.com/maps/api/place/search/json'
RADAR_SEARCH_URL = 'https://maps.googleapis.com/maps/api/place/radarsearch/json'
AUTOCOMPLETE_URL = 'https://maps.googleapis.com/maps/api/place/autocomplete/json'
# Search for Spots at the provided location
#
# @return [Array<Spot>]
# @param [Hash] options
# @option options [String] :key
# the provided api key.
# <b>Note that this is a mandatory parameter</b>
# @option options [Integer] :radius
# Defines the distance (in meters) within which to return Place results.
# The maximum allowed radius is 50,000 meters.
# Note that radius must not be included if <b>:rankby</b> is specified
# <b>Note that this is a mandatory parameter</b>
# @option options [(Integer,Integer),String] :location
# The latitude/longitude around which to retrieve Spot information. This must be specified as latitude,longitude
# <b>Note that this is a mandatory parameter</b>
# @option options [String,Array] :types
# Restricts the results to Spots matching at least one of the specified types
# @option options [String] :name
# A term to be matched against the names of Places.
# Results will be restricted to those containing the passed name value.
# @option options [String] :keyword
# A term to be matched against all content that Google has indexed for this Spot,
# including but not limited to name, type, and address,
# as well as customer reviews and other third-party content.
# @option options [String] :language
# The language code, indicating in which language the results should be returned, if possible.
# @option options [String,Array<String>] :exclude ([])
# A String or an Array of <b>types</b> to exclude from results
#
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def self.spots(options = {})
request = new(NEARBY_SEARCH_URL, options)
request.parsed_response
end
# Search for a Spot with a place_id key
#
# @return [Spot]
# @param [Hash] options
# @option options [String] :key
# the provided api key.
# <b>Note that this is a mandatory parameter</b>
# @option options [String] :place_id
# The place_id of the Spot. This parameter should be sent as placeid
# in requests but is snake_cased in responses (place_id)
# @see: https://developers.google.com/places/documentation/details
# <b>Note that this is a mandatory parameter</b>
# @option options [String] :language
# The language code, indicating in which language the results should be returned, if possible.
#
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
def self.spot(options = {})
request = new(DETAILS_URL, options)
request.parsed_response
end
# @param [Hash] options
# @option options [String] :key
# the provided api key.
# <b>Note that this is a mandatory parameter</b>
# @option options [String] :location
# the lat, lng for the search
# @option options [Integer] :radius (1000)
# Defines the distance (in meters) within which to return Place results.
# The maximum allowed radius is 50,000 meters.
# <b>Note that this is a mandatory parameter</b>
# @option options [String,Array] :types
# Restricts the results to Spots matching at least one of the specified types
# @option options [String] :name
# A term to be matched against the names of Places.
# Results will be restricted to those containing the passed name value.
# @option options [String] :keyword
# A term to be matched against all content that Google has indexed for this Spot,
# including but not limited to name, type, and address,
# as well as customer reviews and other third-party content.
# @option options [Integer] :minprice
# Restricts results to only those places within the specified price range. Valid values range between 0 (most affordable) to 4 (most expensive), inclusive.
# @option options [Integer] :maxprice
# Restricts results to only those places within the specified price range. Valid values range between 0 (most affordable) to 4 (most expensive), inclusive.
# @option options [Boolean] :opennow
# Retricts results to those Places that are open for business at the time the query is sent.
# Places that do not specify opening hours in the Google Places database will not be returned if you include this parameter in your query.
# Setting openNow to false has no effect.
# @option options [Boolean] :zagatselected
# Restrict your search to only those locations that are Zagat selected businesses.
# This parameter does not require a true or false value, simply including the parameter in the request is sufficient to restrict your search.
# The zagatselected parameter is experimental, and only available to Places API enterprise customers.
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see https://developers.google.com/places/documentation/search#RadarSearchRequests Radar Search
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def self.spots_by_radar(options = {})
request = new(RADAR_SEARCH_URL, options)
request.parsed_response
end
# Search for Spots within a give SW|NE bounds with query
#
# @return [Array<Spot>]
# @param [Hash] bounds
# @param [String] api_key the provided api key
# @param [Hash] options
# @option bounds [String, Integer] :se
# the southeast lat|lng pair
# @option bounds [:se][String, Integer] :lat
# the SE latitude
# @option bounds [:se][String, Integer] :lng
# the SE longitude
# @option bounds [:se][String, Integer] :lat
# the SE latitude
# @option bounds [:se][String, Integer] :lng
# the SE longitude
# @option options [String,Array] :query
# Restricts the results to Spots matching term(s) in the specified query
# @option options [String] :language
# The language code, indicating in which language the results should be returned, if possible.
# @option options [String,Array<String>] :exclude ([])
# A String or an Array of <b>types</b> to exclude from results
#
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def self.spots_by_bounds(options = {})
request = new(TEXT_SEARCH_URL, options)
request.parsed_response
end
# Search for Spots with a query
#
# @return [Array<Spot>]
# @param [String] query the query to search for
# @param [String] api_key the provided api key
# @param [Hash] options
# @option options [String,Integer] :lat
# the latitude for the search
# @option options [String,Integer] :lng
# the longitude for the search
# @option options [Integer] :radius
# Defines the distance (in meters) within which to return Place results.
# The maximum allowed radius is 50,000 meters.
# Note that radius must not be included if <b>:rankby</b> is specified
# <b>Note that this is a mandatory parameter</b>
# @option options [String,Array] :types
# Restricts the results to Spots matching at least one of the specified types
# @option options [String] :language
# The language code, indicating in which language the results should be returned, if possible.
# @option options [String,Array<String>] :exclude ([])
# A String or an Array of <b>types</b> to exclude from results
#
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def self.spots_by_query(options = {})
request = new(TEXT_SEARCH_URL, options)
request.parsed_response
end
# Search for Spots with a page token
#
# @return [Array<Spot>]
# @param [String] pagetoken the next page token to search for
# @param [String] api_key the provided api key
# @param [Hash] options
# @option options [String,Array<String>] :exclude ([])
# A String or an Array of <b>types</b> to exclude from results
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def self.spots_by_pagetoken(options = {})
request = new(PAGETOKEN_URL, options)
request.parsed_response
end
# Query for Place Predictions
#
# @return [Array<Prediction>]
# @param [String] api_key the provided api key
# @param [Hash] options
# @option options [String,Array<String>] :exclude ([])
# A String or an Array of <b>types</b> to exclude from results
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def self.predictions_by_input(options = {})
request = new(AUTOCOMPLETE_URL, options)
request.parsed_response
end
# Search for a Photo's URL with a reference key
#
# @return [URL]
# @param [Hash] options
# @option options [String] :key
# the provided api key.
# <b>Note that this is a mandatory parameter</b>
# @option options [Integer] :maxwidth
# The maximum width of the photo url to be returned
# <b>Note that this is a mandatory parameter</b>
# @option options [String] :photoreference
# The reference of a already retrieved Photo
# <b>Note that this is a mandatory parameter</b>
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
def self.photo_url(options = {})
request = new(PHOTO_URL, options, false)
request.parsed_response
end
# Create a new Request for a given uri and the provided params
#
# @return [Request]
# @param [String] url The uri to make the query on
# @param [Hash] options A Hash providing options for the request
# @option options [String] :key
# the provided api key.
# <b>Note that this is a mandatory parameter</b>
# @option options [Integer] :radius
# Defines the distance (in meters) within which to return Place results.
# The maximum allowed radius is 50,000 meters.
# Note that radius must not be included if <b>:rankby</b> is specified
# <b>Note that this is a mandatory parameter</b>
# @option options [(Integer,Integer),String] :location
# The latitude/longitude around which to retrieve Spot information. This must be specified as latitude,longitude
# <b>Note that this is a mandatory parameter</b>
# @option options [String,Array] :types
# Restricts the results to Spots matching at least one of the specified types
# @option options [String] :name
# A term to be matched against the names of Places.
# Results will be restricted to those containing the passed name value.
# @option options [String] :keyword
# A term to be matched against all content that Google has indexed for this Spot,
# including but not limited to name, type, and address,
# as well as customer reviews and other third-party content.
# @option options [String] :language
# The language code, indicating in which language the results should be returned, if possible.
# @option options [String,Array<String>] :exclude ([])
# A String or an Array of <b>types</b> to exclude from results
#
# @option options [Hash] :retry_options ({})
# A Hash containing parameters for search retries
# @option options [Object] :retry_options[:status] ([])
# @option options [Integer] :retry_options[:max] (0) the maximum retries
# @option options [Integer] :retry_options[:delay] (5) the delay between each retry in seconds
#
# @see http://spreadsheets.google.com/pub?key=p9pdwsai2hDMsLkXsoM05KQ&gid=1 List of supported languages
# @see https://developers.google.com/maps/documentation/places/supported_types List of supported types
def initialize(url, options, follow_redirects = true)
retry_options = options.delete(:retry_options) || {}
retry_options[:status] ||= []
retry_options[:max] ||= 0
retry_options[:delay] ||= 5
retry_options[:status] = [retry_options[:status]] unless retry_options[:status].is_a?(Array)
@response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)
# puts @response.request.last_uri.to_s
return unless retry_options[:max] > 0 && retry_options[:status].include?(@response.parsed_response['status'])
retry_request = proc do
for i in (1..retry_options[:max])
sleep(retry_options[:delay])
@response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)
break unless retry_options[:status].include?(@response.parsed_response['status'])
end
end
if retry_options[:timeout]
begin
Timeout::timeout(retry_options[:timeout]) do
retry_request.call
end
rescue Timeout::Error
raise RetryTimeoutError.new(@response)
end
else
retry_request.call
raise RetryError.new(@response) if retry_options[:status].include?(@response.parsed_response['status'])
end
end
def execute
@response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)
end
# Parse errors from the server respons, if any
# @raise [OverQueryLimitError] when server response object includes status 'OVER_QUERY_LIMIT'
# @raise [RequestDeniedError] when server response object includes 'REQUEST_DENIED'
# @raise [InvalidRequestError] when server response object includes 'INVALID_REQUEST'
# @raise [UnknownError] when server response object includes 'UNKNOWN_ERROR'
# @raise [NotFoundError] when server response object includes 'NOT_FOUND'
# @return [String] the response from the server as JSON
def parsed_response
return @response.headers["location"] if @response.code >= 300 and @response.code < 400
case @response.parsed_response['status']
when 'OK', 'ZERO_RESULTS'
@response.parsed_response
when 'OVER_QUERY_LIMIT'
raise OverQueryLimitError.new(@response)
when 'REQUEST_DENIED'
raise RequestDeniedError.new(@response)
when 'INVALID_REQUEST'
raise InvalidRequestError.new(@response)
when 'UNKNOWN_ERROR'
raise UnknownError.new(@response)
when 'NOT_FOUND'
raise NotFoundError.new(@response)
end
end
end
end