Class: GooglePlaces::Request

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, options, follow_redirects = true) ⇒ Request

Create a new Request for a given uri and the provided params

Parameters:

  • url (String)

    The uri to make the query on

  • options (Hash)

    A Hash providing options for the request

Options Hash (options):

  • :key (String)

    the provided api key. Note that this is a mandatory parameter

  • :radius (Integer)

    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 :rankby is specified Note that this is a mandatory parameter

  • :location ((Integer, Integer), String)

    The latitude/longitude around which to retrieve Spot information. This must be specified as latitude,longitude Note that this is a mandatory parameter

  • :types (String, Array)

    Restricts the results to Spots matching at least one of the specified types

  • :name (String)

    A term to be matched against the names of Places. Results will be restricted to those containing the passed name value.

  • :keyword (String)

    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.

  • :language (String)

    The language code, indicating in which language the results should be returned, if possible.

  • :exclude (String, Array<String>) — default: []

    A String or an Array of types to exclude from results

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

See Also:



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, 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)

  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

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/google_places/request.rb', line 6

def options
  @options
end

#responseHTTParty::Response

Returns the retrieved response from the API.

Returns:

  • (HTTParty::Response)

    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

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :key (String)

    the provided api key. Note that this is a mandatory parameter

  • :maxwidth (Integer)

    The maximum width of the photo url to be returned Note that this is a mandatory parameter

  • :photoreference (String)

    The reference of a already retrieved Photo Note that this is a mandatory parameter

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:

  • (URL)


269
270
271
272
# File 'lib/google_places/request.rb', line 269

def self.photo_url(options = {})
  request = new(PHOTO_URL, options, false)
  request.parsed_response
end

.predictions_by_input(options = {}) ⇒ Array<Prediction>

Query for Place Predictions

Parameters:

  • api_key (String)

    the provided api key

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :exclude (String, Array<String>) — default: []

    A String or an Array of types to exclude from results

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:

See Also:



246
247
248
249
# File 'lib/google_places/request.rb', line 246

def self.predictions_by_input(options = {})
  request = new(AUTOCOMPLETE_URL, options)
  request.parsed_response
end

.spot(options = {}) ⇒ Spot

Search for a Spot with a place_id key

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :key (String)

    the provided api key. Note that this is a mandatory parameter

  • :place_id (String)

    The place_id of the Spot. This parameter should be sent as placeid in requests but is snake_cased in responses (place_id) @see: developers.google.com/places/documentation/details Note that this is a mandatory parameter

  • :language (String)

    The language code, indicating in which language the results should be returned, if possible.

  • :region (String)

    The region code, specified as a ccTLD (country code top-level domain) two-character value. Most ccTLD codes are identical to ISO 3166-1 codes, with some exceptions. This parameter will only influence, not fully restrict, search results. If more relevant results exist outside of the specified region, they may be included. When this parameter is used, the country name is omitted from the resulting formatted_address for results in the specified region.

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:



87
88
89
90
# File 'lib/google_places/request.rb', line 87

def self.spot(options = {})
  request = new(DETAILS_URL, options)
  request.parsed_response
end

.spots(options = {}) ⇒ Array<Spot>

Search for Spots at the provided location

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :key (String)

    the provided api key. Note that this is a mandatory parameter

  • :radius (Integer)

    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 :rankby is specified Note that this is a mandatory parameter

  • :location ((Integer, Integer), String)

    The latitude/longitude around which to retrieve Spot information. This must be specified as latitude,longitude Note that this is a mandatory parameter

  • :types (String, Array)

    Restricts the results to Spots matching at least one of the specified types

  • :name (String)

    A term to be matched against the names of Places. Results will be restricted to those containing the passed name value.

  • :keyword (String)

    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.

  • :language (String)

    The language code, indicating in which language the results should be returned, if possible.

  • :exclude (String, Array<String>) — default: []

    A String or an Array of types to exclude from results

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:

See Also:



56
57
58
59
# File 'lib/google_places/request.rb', line 56

def self.spots(options = {})
  request = new(NEARBY_SEARCH_URL, options)
  request.parsed_response
end

.spots_by_bounds(options = {}) ⇒ Array<Spot>

Search for Spots within a give SW|NE bounds with query

Parameters:

  • bounds (Hash)
  • api_key (String)

    the provided api key

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :query (String, Array)

    Restricts the results to Spots matching term(s) in the specified query

  • :language (String)

    The language code, indicating in which language the results should be returned, if possible.

  • :exclude (String, Array<String>) — default: []

    A String or an Array of types to exclude from results

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:

See Also:



167
168
169
170
# File 'lib/google_places/request.rb', line 167

def self.spots_by_bounds(options = {})
  request = new(TEXT_SEARCH_URL, options)
  request.parsed_response
end

.spots_by_pagetoken(options = {}) ⇒ Array<Spot>

Search for Spots with a page token

Parameters:

  • pagetoken (String)

    the next page token to search for

  • api_key (String)

    the provided api key

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :exclude (String, Array<String>) — default: []

    A String or an Array of types to exclude from results

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:

See Also:



227
228
229
230
# File 'lib/google_places/request.rb', line 227

def self.spots_by_pagetoken(options = {})
  request = new(PAGETOKEN_URL, options)
  request.parsed_response
end

.spots_by_query(options = {}) ⇒ Array<Spot>

Search for Spots with a query

Parameters:

  • query (String)

    the query to search for

  • api_key (String)

    the provided api key

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :lat (String, Integer)

    the latitude for the search

  • :lng (String, Integer)

    the longitude for the search

  • :radius (Integer)

    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 :rankby is specified Note that this is a mandatory parameter

  • :types (String, Array)

    Restricts the results to Spots matching at least one of the specified types

  • :language (String)

    The language code, indicating in which language the results should be returned, if possible.

  • :region (String)

    The region code, specified as a ccTLD (country code top-level domain) two-character value. Most ccTLD codes are identical to ISO 3166-1 codes, with some exceptions. This parameter will only influence, not fully restrict, search results. If more relevant results exist outside of the specified region, they may be included. When this parameter is used, the country name is omitted from the resulting formatted_address for results in the specified region.

  • :exclude (String, Array<String>) — default: []

    A String or an Array of types to exclude from results

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

Returns:

See Also:



207
208
209
210
# File 'lib/google_places/request.rb', line 207

def self.spots_by_query(options = {})
  request = new(TEXT_SEARCH_URL, options)
  request.parsed_response
end

.spots_by_radar(options = {}) ⇒ Object

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :key (String)

    the provided api key. Note that this is a mandatory parameter

  • :location (String)

    the lat, lng for the search

  • :radius (Integer) — default: 1000

    Defines the distance (in meters) within which to return Place results. The maximum allowed radius is 50,000 meters. Note that this is a mandatory parameter

  • :types (String, Array)

    Restricts the results to Spots matching at least one of the specified types

  • :name (String)

    A term to be matched against the names of Places. Results will be restricted to those containing the passed name value.

  • :keyword (String)

    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.

  • :minprice (Integer)

    Restricts results to only those places within the specified price range. Valid values range between 0 (most affordable) to 4 (most expensive), inclusive.

  • :maxprice (Integer)

    Restricts results to only those places within the specified price range. Valid values range between 0 (most affordable) to 4 (most expensive), inclusive.

  • :opennow (Boolean)

    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.

  • :zagatselected (Boolean)

    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.

  • :retry_options (Hash) — default: {}

    A Hash containing parameters for search retries

  • :retry_options[:status] (Object) — default: []
  • :retry_options[:max] (Integer) — default: 0

    the maximum retries

  • :retry_options[:delay] (Integer) — default: 5

    the delay between each retry in seconds

See Also:



131
132
133
134
# File 'lib/google_places/request.rb', line 131

def self.spots_by_radar(options = {})
  request = new(RADAR_SEARCH_URL, options)
  request.parsed_response
end

Instance Method Details

#executeObject



348
349
350
# File 'lib/google_places/request.rb', line 348

def execute
  @response = self.class.get(url, :query => options, :follow_redirects => follow_redirects)
end

#parsed_responseString

Parse errors from the server respons, if any

Returns:

  • (String)

    the response from the server as JSON

Raises:

  • (OverQueryLimitError)

    when server response object includes status ‘OVER_QUERY_LIMIT’

  • (RequestDeniedError)

    when server response object includes ‘REQUEST_DENIED’

  • (InvalidRequestError)

    when server response object includes ‘INVALID_REQUEST’

  • (UnknownError)

    when server response object includes ‘UNKNOWN_ERROR’

  • (NotFoundError)

    when server response object includes ‘NOT_FOUND’



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