Class: GooglePlaces::Request
- Inherits:
-
Object
- Object
- GooglePlaces::Request
- Includes:
- HTTParty
- Defined in:
- lib/google_places/request.rb
Overview
This class performs the queries on the API
Constant Summary collapse
- 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'
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#response ⇒ HTTParty::Response
The retrieved response from the API.
Class Method Summary collapse
-
.photo_url(options = {}) ⇒ URL
Search for a Photo’s URL with a reference key.
-
.predictions_by_input(options = {}) ⇒ Array<Prediction>
Query for Place Predictions.
-
.spot(options = {}) ⇒ Spot
Search for a Spot with a place_id key.
-
.spots(options = {}) ⇒ Array<Spot>
Search for Spots at the provided location.
-
.spots_by_bounds(options = {}) ⇒ Array<Spot>
Search for Spots within a give SW|NE bounds with query.
-
.spots_by_pagetoken(options = {}) ⇒ Array<Spot>
Search for Spots with a page token.
-
.spots_by_query(options = {}) ⇒ Array<Spot>
Search for Spots with a query.
- .spots_by_radar(options = {}) ⇒ Object
Instance Method Summary collapse
- #execute ⇒ Object
-
#initialize(url, options, follow_redirects = true) ⇒ Request
constructor
Create a new Request for a given uri and the provided params.
-
#parsed_response ⇒ String
Parse errors from the server respons, if any.
Constructor Details
#initialize(url, options, follow_redirects = true) ⇒ Request
Create a new Request for a given uri and the provided params
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 |
# File 'lib/google_places/request.rb', line 312 def initialize(url, , follow_redirects = true) = .delete(:retry_options) || {} [:status] ||= [] [:max] ||= 0 [:delay] ||= 5 [:status] = [[:status]] unless [:status].is_a?(Array) @response = self.class.get(url, :query => , :follow_redirects => follow_redirects) return unless [:max] > 0 && [:status].include?(@response.parsed_response['status']) retry_request = proc do for i in (1..[:max]) sleep([:delay]) @response = self.class.get(url, :query => , :follow_redirects => follow_redirects) break unless [:status].include?(@response.parsed_response['status']) end end if [:timeout] begin Timeout::timeout([:timeout]) do retry_request.call end rescue Timeout::Error raise RetryTimeoutError.new(@response) end else retry_request.call raise RetryError.new(@response) if [:status].include?(@response.parsed_response['status']) end end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/google_places/request.rb', line 6 def @options end |
#response ⇒ HTTParty::Response
Returns the retrieved response from the API.
5 6 7 |
# File 'lib/google_places/request.rb', line 5 def response @response end |
Class Method Details
.photo_url(options = {}) ⇒ URL
Search for a Photo’s URL with a reference key
269 270 271 272 |
# File 'lib/google_places/request.rb', line 269 def self.photo_url( = {}) request = new(PHOTO_URL, , false) request.parsed_response end |
.predictions_by_input(options = {}) ⇒ Array<Prediction>
Query for Place Predictions
246 247 248 249 |
# File 'lib/google_places/request.rb', line 246 def self.predictions_by_input( = {}) request = new(AUTOCOMPLETE_URL, ) request.parsed_response end |
.spot(options = {}) ⇒ Spot
Search for a Spot with a place_id key
87 88 89 90 |
# File 'lib/google_places/request.rb', line 87 def self.spot( = {}) request = new(DETAILS_URL, ) request.parsed_response end |
.spots(options = {}) ⇒ Array<Spot>
Search for Spots at the provided location
56 57 58 59 |
# File 'lib/google_places/request.rb', line 56 def self.spots( = {}) request = new(NEARBY_SEARCH_URL, ) request.parsed_response end |
.spots_by_bounds(options = {}) ⇒ Array<Spot>
Search for Spots within a give SW|NE bounds with query
167 168 169 170 |
# File 'lib/google_places/request.rb', line 167 def self.spots_by_bounds( = {}) request = new(TEXT_SEARCH_URL, ) request.parsed_response end |
.spots_by_pagetoken(options = {}) ⇒ Array<Spot>
Search for Spots with a page token
227 228 229 230 |
# File 'lib/google_places/request.rb', line 227 def self.spots_by_pagetoken( = {}) request = new(PAGETOKEN_URL, ) request.parsed_response end |
.spots_by_query(options = {}) ⇒ Array<Spot>
Search for Spots with a query
207 208 209 210 |
# File 'lib/google_places/request.rb', line 207 def self.spots_by_query( = {}) request = new(TEXT_SEARCH_URL, ) request.parsed_response end |
.spots_by_radar(options = {}) ⇒ Object
131 132 133 134 |
# File 'lib/google_places/request.rb', line 131 def self.spots_by_radar( = {}) request = new(RADAR_SEARCH_URL, ) request.parsed_response end |
Instance Method Details
#execute ⇒ Object
348 349 350 |
# File 'lib/google_places/request.rb', line 348 def execute @response = self.class.get(url, :query => , :follow_redirects => follow_redirects) end |
#parsed_response ⇒ String
Parse errors from the server respons, if any
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/google_places/request.rb', line 359 def parsed_response return @response.headers["location"] if @response.code >= 300 && @response.code < 400 raise APIConnectionError.new(@response) if @response.code >= 500 && @response.code < 600 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 |