Class: GooglePlaces::Spot

Inherits:
Object
  • Object
show all
Defined in:
lib/google_places/spot.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(json_result_object, api_key, sensor) ⇒ Spot

Returns a newly created spot.

Parameters:

  • json_result_object (JSON)

    a JSON object to create a Spot from



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
# File 'lib/google_places/spot.rb', line 364

def initialize(json_result_object, api_key, sensor)
  @reference                  = json_result_object['reference']
  @place_id                   = json_result_object['place_id']
  @vicinity                   = json_result_object['vicinity']
  @lat                        = json_result_object['geometry']['location']['lat']
  @lng                        = json_result_object['geometry']['location']['lng']
  @viewport                   = json_result_object['geometry']['viewport']
  @name                       = json_result_object['name']
  @icon                       = json_result_object['icon']
  @types                      = json_result_object['types']
  @id                         = json_result_object['id']
  @formatted_phone_number     = json_result_object['formatted_phone_number']
  @international_phone_number = json_result_object['international_phone_number']
  @formatted_address          = json_result_object['formatted_address']
  @address_components         = json_result_object['address_components']
  @street_number              = address_component(:street_number, 'short_name')
  @street                     = address_component(:route, 'long_name')
  @city                       = address_component(:locality, 'long_name')
  @region                     = address_component(:administrative_area_level_1, 'long_name')
  @postal_code                = address_component(:postal_code, 'long_name')
  @country                    = address_component(:country, 'long_name')
  @rating                     = json_result_object['rating']
  @price_level                = json_result_object['price_level']
  @opening_hours              = json_result_object['opening_hours']
  @url                        = json_result_object['url']
  @cid                        = json_result_object['url'].to_i
  @website                    = json_result_object['website']
  @zagat_reviewed             = json_result_object['zagat_reviewed']
  @zagat_selected             = json_result_object['zagat_selected']
  @aspects                    = aspects_component(json_result_object['aspects'])
  @review_summary             = json_result_object['review_summary']
  @photos                     = photos_component(json_result_object['photos'], api_key, sensor)
  @reviews                    = reviews_component(json_result_object['reviews'])
  @nextpagetoken              = json_result_object['nextpagetoken']
  @events                     = events_component(json_result_object['events'])
  @utc_offset                 = json_result_object['utc_offset']
end

Instance Attribute Details

#address_componentsObject

Returns the value of attribute address_components.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def address_components
  @address_components
end

#aspectsObject

Returns the value of attribute aspects.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def aspects
  @aspects
end

#cidObject

Returns the value of attribute cid.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def cid
  @cid
end

#cityObject

Returns the value of attribute city.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def city
  @city
end

#countryObject

Returns the value of attribute country.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def country
  @country
end

#eventsObject

Returns the value of attribute events.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def events
  @events
end

#formatted_addressObject

Returns the value of attribute formatted_address.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def formatted_address
  @formatted_address
end

#formatted_phone_numberObject

Returns the value of attribute formatted_phone_number.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def formatted_phone_number
  @formatted_phone_number
end

#iconObject

Returns the value of attribute icon.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def icon
  @icon
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def id
  @id
end

#international_phone_numberObject

Returns the value of attribute international_phone_number.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def international_phone_number
  @international_phone_number
end

#latObject

Returns the value of attribute lat.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def lat
  @lat
end

#lngObject

Returns the value of attribute lng.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def lng
  @lng
end

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def name
  @name
end

#nextpagetokenObject

Returns the value of attribute nextpagetoken.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def nextpagetoken
  @nextpagetoken
end

#opening_hoursObject

Returns the value of attribute opening_hours.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def opening_hours
  @opening_hours
end

#photosObject

Returns the value of attribute photos.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def photos
  @photos
end

#place_idObject

Returns the value of attribute place_id.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def place_id
  @place_id
end

#postal_codeObject

Returns the value of attribute postal_code.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def postal_code
  @postal_code
end

#price_levelObject

Returns the value of attribute price_level.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def price_level
  @price_level
end

#ratingObject

Returns the value of attribute rating.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def rating
  @rating
end

#referenceObject

Returns the value of attribute reference.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def reference
  @reference
end

#regionObject

Returns the value of attribute region.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def region
  @region
end

#review_summaryObject

Returns the value of attribute review_summary.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def review_summary
  @review_summary
end

#reviewsObject

Returns the value of attribute reviews.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def reviews
  @reviews
end

#streetObject

Returns the value of attribute street.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def street
  @street
end

#street_numberObject

Returns the value of attribute street_number.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def street_number
  @street_number
end

#typesObject

Returns the value of attribute types.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def types
  @types
end

#urlObject

Returns the value of attribute url.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def url
  @url
end

#utc_offsetObject

Returns the value of attribute utc_offset.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def utc_offset
  @utc_offset
end

#vicinityObject

Returns the value of attribute vicinity.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def vicinity
  @vicinity
end

#viewportObject

Returns the value of attribute viewport.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def viewport
  @viewport
end

#websiteObject

Returns the value of attribute website.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def website
  @website
end

#zagat_reviewedObject

Returns the value of attribute zagat_reviewed.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def zagat_reviewed
  @zagat_reviewed
end

#zagat_selectedObject

Returns the value of attribute zagat_selected.



5
6
7
# File 'lib/google_places/spot.rb', line 5

def zagat_selected
  @zagat_selected
end

Class Method Details

.find(place_id, api_key, sensor, options = {}) ⇒ Spot

Search for a Spot with a reference key

Parameters:

  • place_id (String)

    the place_id of the spot

  • api_key (String)

    the provided api key

  • sensor (Boolean)

    Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. Note that this is a mandatory parameter

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

Options Hash (options):

  • :language (String)

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

  • :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:



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/google_places/spot.rb', line 191

def self.find(place_id, api_key, sensor, options = {})
  language  = options.delete(:language)
  retry_options = options.delete(:retry_options) || {}
  extensions = options.delete(:review_summary) ? 'review_summary' : nil

  response = Request.spot(
    :placeid => place_id,
    :sensor => sensor,
    :key => api_key,
    :language => language,
    :extensions => extensions,
    :retry_options => retry_options
  )

  self.new(response['result'], api_key, sensor)
end

.list(lat, lng, api_key, sensor, options = {}) ⇒ Array<Spot>

Search for Spots at the provided location

Parameters:

  • lat (String, Integer)

    the latitude for the search

  • lng (String, Integer)

    the longitude for the search

  • api_key (String)

    the provided api key

  • sensor (Boolean)

    Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. Note that this is a mandatory parameter

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

Options Hash (options):

  • :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 radius must not be included if :rankby => ‘distance’ (described below) is specified. Note that this is a mandatory parameter

  • :rankby (String)

    Specifies the order in which results are listed. Possible values are:

    • prominence (default). This option sorts results based on their importance. Ranking will favor prominent places within the specified area. Prominence can be affected by a Place’s ranking in Google’s index, the number of check-ins from your application, global popularity, and other factors.

    • distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required.

  • :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:



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
# File 'lib/google_places/spot.rb', line 53

def self.list(lat, lng, api_key, sensor, options = {})
  location = Location.new(lat, lng)
  multipage_request = !!options.delete(:multipage)
  rankby = options.delete(:rankby)
  radius = options.delete(:radius) || 1000 if rankby.nil?
  types  = options.delete(:types)
  name  = options.delete(:name)
  keyword = options.delete(:keyword)
  language  = options.delete(:language)
  exclude = options.delete(:exclude) || []
  retry_options = options.delete(:retry_options) || {}
  zagat_selected = options.delete(:zagat_selected) || false
  exclude = [exclude] unless exclude.is_a?(Array)

  options = {
    :location => location.format,
    :radius => radius,
    :sensor => sensor,
    :rankby => rankby,
    :key => api_key,
    :name => name,
    :language => language,
    :keyword => keyword,
    :retry_options => retry_options
  }

  options[:zagatselected] = zagat_selected if zagat_selected

  # Accept Types as a string or array
  if types
    types = (types.is_a?(Array) ? types.join('|') : types)
    options.merge!(:types => types)
  end

  request(:spots, multipage_request, exclude, options)
end

.list_by_pagetoken(pagetoken, api_key, sensor, options = {}) ⇒ Array<Spot>

Search for Spots with a pagetoken

Parameters:

  • pagetoken (String)

    the token to find next results

  • api_key (String)

    the provided api key

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

Returns:



215
216
217
218
219
220
221
222
223
224
225
226
# File 'lib/google_places/spot.rb', line 215

def self.list_by_pagetoken(pagetoken, api_key, sensor, options = {})
  exclude = options.delete(:exclude) || []
  exclude = [exclude] unless exclude.is_a?(Array)

  options = {
      :pagetoken => pagetoken,
      :sensor => sensor,
      :key => api_key
  }

  request(:spots_by_pagetoken, false, exclude, options)
end

.list_by_query(query, api_key, sensor, options = {}) ⇒ Array<Spot>

Search for Spots with a query

Parameters:

  • query (String)

    the query to search for

  • api_key (String)

    the provided api key

  • sensor (Boolean)

    Indicates whether or not the Place request came from a device using a location sensor (e.g. a GPS) to determine the location sent in this request. Note that this is a mandatory parameter

  • 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) — default: 1000

    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 => ‘distance’ (described below) is specified. Note that this is a mandatory parameter

  • :rankby (String)

    Specifies the order in which results are listed. Possible values are:

    • prominence (default). This option sorts results based on their importance. Ranking will favor prominent places within the specified area. Prominence can be affected by a Place’s ranking in Google’s index, the number of check-ins from your application, global popularity, and other factors.

    • distance. This option sorts results in ascending order by their distance from the specified location. Ranking results by distance will set a fixed search radius of 50km. One or more of keyword, name, or types is required.

  • :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.

  • :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:



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
# File 'lib/google_places/spot.rb', line 271

def self.list_by_query(query, api_key, sensor, options = {})
  if options.has_key?(:lat) && options.has_key?(:lng)
    with_location = true
  else
    with_location = false
  end

  if options.has_key?(:radius)
    with_radius = true
  else
    with_radius = false
  end

  query = query
  sensor = sensor
  multipage_request = !!options.delete(:multipage)
  location = Location.new(options.delete(:lat), options.delete(:lng)) if with_location
  radius = options.delete(:radius) if with_radius
  rankby = options.delete(:rankby)
  language = options.delete(:language)
  types = options.delete(:types)
  exclude = options.delete(:exclude) || []
  retry_options = options.delete(:retry_options) || {}

  exclude = [exclude] unless exclude.is_a?(Array)

  options = {
    :query => query,
    :sensor => sensor,
    :key => api_key,
    :rankby => rankby,
    :language => language,
    :retry_options => retry_options
  }

  options[:location] = location.format if with_location
  options[:radius] = radius if with_radius

  # Accept Types as a string or array
  if types
    types = (types.is_a?(Array) ? types.join('|') : types)
    options.merge!(:types => types)
  end

  request(:spots_by_query, multipage_request, exclude, options)
end

.list_by_radar(lat, lng, api_key, sensor, options = {}) ⇒ Object

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>

Parameters:

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

    a customizable set of options

Options Hash (options):

  • :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:



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
# File 'lib/google_places/spot.rb', line 133

def self.list_by_radar(lat, lng, api_key, sensor, options = {})
  location = Location.new(lat, lng)
  multipage_request = !!options.delete(:multipage)
  radius = options.delete(:radius) || 1000 
  types  = options.delete(:types)
  name  = options.delete(:name)
  keyword = options.delete(:keyword)
  retry_options = options.delete(:retry_options) || {}
  zagat_selected = options.delete(:zagat_selected) || false
  opennow = options.delete(:opennow) || false
  minprice = options.delete(:minprice) || false
  maxprice = options.delete(:maxprice) || false
  exclude = []

  options = {
    :location => location.format,
    :radius => radius,
    :sensor => sensor,
    :key => api_key,
    :name => name,
    :keyword => keyword,
    :retry_options => retry_options
  }

  options[:zagatselected] = zagat_selected if zagat_selected
  options[:opennow] = opennow if opennow
  options[:minprice] = minprice if minprice
  options[:maxprice] = maxprice if maxprice

  # Accept Types as a string or array
  if types
    types = (types.is_a?(Array) ? types.join('|') : types)
    options.merge!(:types => types)
  end

  request(:spots_by_radar, multipage_request, exclude, options)
end

.multi_pages_request(method, multipage_request, options) ⇒ Object



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
# File 'lib/google_places/spot.rb', line 331

def self.multi_pages_request(method, multipage_request, options)
  begin
    response = Request.send(method, options)
    response['results'].each do |result|
      if !multipage_request && !response["next_page_token"].nil? && result == response['results'].last
        # add next page token on the last result
        result.merge!("nextpagetoken" => response["next_page_token"])
      end
      yield(result)
    end

    # request the next page if presence of a "next_page" token
    next_page = false
    if multipage_request && !response["next_page_token"].nil?
      options = {
        :pagetoken => response["next_page_token"],
        :key => options[:key],
        :sensor => options[:sensor]
      }

      # There is a short delay between when a next_page_token is issued, and when it will become valid.
      # If requested too early, it will result in InvalidRequestError.
      # See: https://developers.google.com/places/documentation/search#PlaceSearchPaging
      sleep(2)

      next_page = true
    end

  end while (next_page)
end

.request(method, multipage_request, exclude, options) ⇒ Object



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/google_places/spot.rb', line 318

def self.request(method, multipage_request, exclude, options)
  results = []

  self.multi_pages_request(method, multipage_request, options) do |result|
  	# Some places returned by Google do not have a 'types' property. If the user specified 'types', then
  	# this is a non-issue because those places will not be returned. However, if the user did not specify
  	# 'types', then we do not want to filter out places with a missing 'types' property from the results set.
    results << self.new(result, options[:key], options[:sensor]) if result['types'].nil? || (result['types'] & exclude) == []
  end

  results
end

Instance Method Details

#[](key) ⇒ Object



402
403
404
# File 'lib/google_places/spot.rb', line 402

def [] (key)
  send(key)
end

#address_component(address_component_type, address_component_length) ⇒ Object



406
407
408
409
410
# File 'lib/google_places/spot.rb', line 406

def address_component(address_component_type, address_component_length)
  if component = address_components_of_type(address_component_type)
    component.first[address_component_length] unless component.first.nil?
  end
end

#address_components_of_type(type) ⇒ Object



412
413
414
# File 'lib/google_places/spot.rb', line 412

def address_components_of_type(type)
  @address_components.select{ |c| c['types'].include?(type.to_s) } unless @address_components.nil?
end

#aspects_component(json_aspects) ⇒ Object



432
433
434
# File 'lib/google_places/spot.rb', line 432

def aspects_component(json_aspects)
  json_aspects.to_a.map{ |r| { :type => r['type'], :rating => r['rating'] } }
end

#events_component(json_events) ⇒ Object



452
453
454
# File 'lib/google_places/spot.rb', line 452

def events_component(json_events)
  json_events.to_a.map{ |r| {:event_id => r['event_id'], :summary => r['summary'], :url => r['url'], :start_time => r['start_time']} }
end

#photos_component(json_photos, api_key, sensor) ⇒ Object



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# File 'lib/google_places/spot.rb', line 436

def photos_component(json_photos, api_key, sensor)
  if json_photos
    json_photos.map{ |p| 
      Photo.new(
        p['width'], 
        p['height'],
        p['photo_reference'],
        p['html_attributions'], 
        api_key, 
        sensor
      )
    }
  else []
  end
end

#reviews_component(json_reviews) ⇒ Object



416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/google_places/spot.rb', line 416

def reviews_component(json_reviews)
  if json_reviews
    json_reviews.map { |r|
      Review.new(
          r['rating'],
          r['type'],
          r['author_name'],
          r['author_url'],
          r['text'],
          r['time'].to_i
      )
    }
  else []
  end
end