Class: GooglePlaces::Spot
- Inherits:
-
Object
- Object
- GooglePlaces::Spot
- Defined in:
- lib/google_places/spot.rb
Instance Attribute Summary collapse
-
#address_components ⇒ Object
Returns the value of attribute address_components.
-
#aspects ⇒ Object
Returns the value of attribute aspects.
-
#cid ⇒ Object
Returns the value of attribute cid.
-
#city ⇒ Object
Returns the value of attribute city.
-
#country ⇒ Object
Returns the value of attribute country.
-
#events ⇒ Object
Returns the value of attribute events.
-
#formatted_address ⇒ Object
Returns the value of attribute formatted_address.
-
#formatted_phone_number ⇒ Object
Returns the value of attribute formatted_phone_number.
-
#icon ⇒ Object
Returns the value of attribute icon.
-
#id ⇒ Object
Returns the value of attribute id.
-
#international_phone_number ⇒ Object
Returns the value of attribute international_phone_number.
-
#json_result_object ⇒ Object
Returns the value of attribute json_result_object.
-
#lat ⇒ Object
Returns the value of attribute lat.
-
#lng ⇒ Object
Returns the value of attribute lng.
-
#name ⇒ Object
Returns the value of attribute name.
-
#nextpagetoken ⇒ Object
Returns the value of attribute nextpagetoken.
-
#opening_hours ⇒ Object
Returns the value of attribute opening_hours.
-
#permanently_closed ⇒ Object
Returns the value of attribute permanently_closed.
-
#photos ⇒ Object
Returns the value of attribute photos.
-
#place_id ⇒ Object
Returns the value of attribute place_id.
-
#postal_code ⇒ Object
Returns the value of attribute postal_code.
-
#price_level ⇒ Object
Returns the value of attribute price_level.
-
#rating ⇒ Object
Returns the value of attribute rating.
-
#reference ⇒ Object
Returns the value of attribute reference.
-
#region ⇒ Object
Returns the value of attribute region.
-
#review_summary ⇒ Object
Returns the value of attribute review_summary.
-
#reviews ⇒ Object
Returns the value of attribute reviews.
-
#street ⇒ Object
Returns the value of attribute street.
-
#street_number ⇒ Object
Returns the value of attribute street_number.
-
#types ⇒ Object
Returns the value of attribute types.
-
#url ⇒ Object
Returns the value of attribute url.
-
#utc_offset ⇒ Object
Returns the value of attribute utc_offset.
-
#vicinity ⇒ Object
Returns the value of attribute vicinity.
-
#viewport ⇒ Object
Returns the value of attribute viewport.
-
#website ⇒ Object
Returns the value of attribute website.
-
#zagat_reviewed ⇒ Object
Returns the value of attribute zagat_reviewed.
-
#zagat_selected ⇒ Object
Returns the value of attribute zagat_selected.
Class Method Summary collapse
-
.find(place_id, api_key, options = {}) ⇒ Spot
Search for a Spot with a reference key.
-
.list(lat, lng, api_key, options = {}) ⇒ Array<Spot>
Search for Spots at the provided location.
-
.list_by_bounds(bounds, api_key, options = {}) ⇒ Array<Spot>
Search for Spots within a give SW|NE bounds with query.
-
.list_by_pagetoken(pagetoken, api_key, options = {}) ⇒ Array<Spot>
Search for Spots with a pagetoken.
-
.list_by_query(query, api_key, options = {}) ⇒ Array<Spot>
Search for Spots with a query.
-
.list_by_radar(lat, lng, api_key, options = {}) ⇒ Object
Defines the distance (in meters) within which to return Place results.
- .multi_pages_request(method, multipage_request, options) ⇒ Object
- .request(method, multipage_request, exclude, options) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #address_component(address_component_type, address_component_length) ⇒ Object
- #address_components_of_type(type) ⇒ Object
- #aspects_component(json_aspects) ⇒ Object
- #events_component(json_events) ⇒ Object
-
#initialize(json_result_object, api_key) ⇒ Spot
constructor
A newly created spot.
- #photos_component(json_photos, api_key) ⇒ Object
- #reviews_component(json_reviews) ⇒ Object
Constructor Details
#initialize(json_result_object, api_key) ⇒ Spot
Returns a newly created spot.
431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/google_places/spot.rb', line 431 def initialize(json_result_object, api_key) @json_result_object = json_result_object @reference = json_result_object['reference'] @place_id = json_result_object['place_id'] @vicinity = json_result_object['vicinity'] @lat = json_result_object['geometry'] ? json_result_object['geometry']['location']['lat'] : nil @lng = json_result_object['geometry'] ? json_result_object['geometry']['location']['lng'] : nil @viewport = json_result_object['geometry'] ? json_result_object['geometry']['viewport'] : nil @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) @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'] @permanently_closed = json_result_object['permanently_closed'] end |
Instance Attribute Details
#address_components ⇒ Object
Returns the value of attribute address_components.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def address_components @address_components end |
#aspects ⇒ Object
Returns the value of attribute aspects.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def aspects @aspects end |
#cid ⇒ Object
Returns the value of attribute cid.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def cid @cid end |
#city ⇒ Object
Returns the value of attribute city.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def city @city end |
#country ⇒ Object
Returns the value of attribute country.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def country @country end |
#events ⇒ Object
Returns the value of attribute events.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def events @events end |
#formatted_address ⇒ Object
Returns the value of attribute formatted_address.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def formatted_address @formatted_address end |
#formatted_phone_number ⇒ Object
Returns the value of attribute formatted_phone_number.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def formatted_phone_number @formatted_phone_number end |
#icon ⇒ Object
Returns the value of attribute icon.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def icon @icon end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def id @id end |
#international_phone_number ⇒ Object
Returns the value of attribute international_phone_number.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def international_phone_number @international_phone_number end |
#json_result_object ⇒ Object
Returns the value of attribute json_result_object.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def json_result_object @json_result_object end |
#lat ⇒ Object
Returns the value of attribute lat.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def lat @lat end |
#lng ⇒ Object
Returns the value of attribute lng.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def lng @lng end |
#name ⇒ Object
Returns the value of attribute name.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def name @name end |
#nextpagetoken ⇒ Object
Returns the value of attribute nextpagetoken.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def nextpagetoken @nextpagetoken end |
#opening_hours ⇒ Object
Returns the value of attribute opening_hours.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def opening_hours @opening_hours end |
#permanently_closed ⇒ Object
Returns the value of attribute permanently_closed.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def permanently_closed @permanently_closed end |
#photos ⇒ Object
Returns the value of attribute photos.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def photos @photos end |
#place_id ⇒ Object
Returns the value of attribute place_id.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def place_id @place_id end |
#postal_code ⇒ Object
Returns the value of attribute postal_code.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def postal_code @postal_code end |
#price_level ⇒ Object
Returns the value of attribute price_level.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def price_level @price_level end |
#rating ⇒ Object
Returns the value of attribute rating.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def @rating end |
#reference ⇒ Object
Returns the value of attribute reference.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def reference @reference end |
#region ⇒ Object
Returns the value of attribute region.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def region @region end |
#review_summary ⇒ Object
Returns the value of attribute review_summary.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def review_summary @review_summary end |
#reviews ⇒ Object
Returns the value of attribute reviews.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def reviews @reviews end |
#street ⇒ Object
Returns the value of attribute street.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def street @street end |
#street_number ⇒ Object
Returns the value of attribute street_number.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def street_number @street_number end |
#types ⇒ Object
Returns the value of attribute types.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def types @types end |
#url ⇒ Object
Returns the value of attribute url.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def url @url end |
#utc_offset ⇒ Object
Returns the value of attribute utc_offset.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def utc_offset @utc_offset end |
#vicinity ⇒ Object
Returns the value of attribute vicinity.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def vicinity @vicinity end |
#viewport ⇒ Object
Returns the value of attribute viewport.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def @viewport end |
#website ⇒ Object
Returns the value of attribute website.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def website @website end |
#zagat_reviewed ⇒ Object
Returns the value of attribute zagat_reviewed.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def zagat_reviewed @zagat_reviewed end |
#zagat_selected ⇒ Object
Returns the value of attribute zagat_selected.
4 5 6 |
# File 'lib/google_places/spot.rb', line 4 def zagat_selected @zagat_selected end |
Class Method Details
.find(place_id, api_key, options = {}) ⇒ Spot
Search for a Spot with a reference key
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/google_places/spot.rb', line 255 def self.find(place_id, api_key, = {}) fields = .delete(:fields) language = .delete(:language) region = .delete(:region) = .delete(:retry_options) || {} extensions = .delete(:review_summary) ? 'review_summary' : nil = { :placeid => place_id, :key => api_key, :language => language, :extensions => extensions, :retry_options => , :fields => fields } [:region] = region unless region.nil? response = Request.spot() self.new(response['result'], api_key) end |
.list(lat, lng, api_key, options = {}) ⇒ Array<Spot>
Search for Spots at the provided location
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 |
# File 'lib/google_places/spot.rb', line 53 def self.list(lat, lng, api_key, = {}) location = Location.new(lat, lng) multipage_request = !!.delete(:multipage) rankby = .delete(:rankby) radius = .delete(:radius) || 1000 if rankby.nil? || rankby =~ /prominence/ types = .delete(:types) name = .delete(:name) keyword = .delete(:keyword) language = .delete(:language) exclude = .delete(:exclude) || [] = .delete(:retry_options) || {} zagat_selected = .delete(:zagat_selected) || false exclude = [exclude] unless exclude.is_a?(Array) = { :location => location.format, :radius => radius, :rankby => rankby, :key => api_key, :name => name, :language => language, :keyword => keyword, :retry_options => } [:zagatselected] = zagat_selected if zagat_selected # Accept Types as a string or array if types types = (types.is_a?(Array) ? types.join('|') : types) .merge!(:types => types) end request(:spots, multipage_request, exclude, ) end |
.list_by_bounds(bounds, api_key, options = {}) ⇒ Array<Spot>
Search for Spots within a give SW|NE bounds with query
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 |
# File 'lib/google_places/spot.rb', line 122 def self.list_by_bounds(bounds, api_key, = {}) start_lat = bounds[:start_point][:lat] start_lng = bounds[:start_point][:lng] end_lat = bounds[:end_point][:lat] end_lng = bounds[:end_point][:lng] rect = Rectangle.new(start_lat, start_lng, end_lat, end_lng) multipage_request = !!.delete(:multipage) rankby = .delete(:rankby) query = .delete(:query) name = .delete(:name) language = .delete(:language) exclude = .delete(:exclude) || [] = .delete(:retry_options) || {} zagat_selected = .delete(:zagat_selected) || false exclude = [exclude] unless exclude.is_a?(Array) = { :bounds => rect.format, :key => api_key, :language => language, :retry_options => } [:zagatselected] = zagat_selected if zagat_selected # Accept Types as a string or array if query query = (query.is_a?(Array) ? query.join('|') : query) .merge!(:query => query) end request(:spots_by_bounds, multipage_request, exclude, ) end |
.list_by_pagetoken(pagetoken, api_key, options = {}) ⇒ Array<Spot>
Search for Spots with a pagetoken
282 283 284 285 286 287 288 289 290 291 292 |
# File 'lib/google_places/spot.rb', line 282 def self.list_by_pagetoken(pagetoken, api_key, = {}) exclude = .delete(:exclude) || [] exclude = [exclude] unless exclude.is_a?(Array) = { :pagetoken => pagetoken, :key => api_key } request(:spots_by_pagetoken, false, exclude, ) end |
.list_by_query(query, api_key, options = {}) ⇒ Array<Spot>
Search for Spots with a query
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 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 |
# File 'lib/google_places/spot.rb', line 339 def self.list_by_query(query, api_key, = {}) if .has_key?(:lat) && .has_key?(:lng) with_location = true else with_location = false end if .has_key?(:radius) with_radius = true else with_radius = false end query = query multipage_request = !!.delete(:multipage) location = Location.new(.delete(:lat), .delete(:lng)) if with_location radius = .delete(:radius) if with_radius rankby = .delete(:rankby) language = .delete(:language) region = .delete(:region) types = .delete(:types) exclude = .delete(:exclude) || [] = .delete(:retry_options) || {} exclude = [exclude] unless exclude.is_a?(Array) = { :query => query, :key => api_key, :rankby => rankby, :language => language, :retry_options => } [:location] = location.format if with_location [:radius] = radius if with_radius [:region] = region unless region.nil? # Accept Types as a string or array if types types = (types.is_a?(Array) ? types.join('|') : types) .merge!(:types => types) end request(:spots_by_query, multipage_request, exclude, ) end |
.list_by_radar(lat, lng, api_key, 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>
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 |
# File 'lib/google_places/spot.rb', line 196 def self.list_by_radar(lat, lng, api_key, = {}) location = Location.new(lat, lng) multipage_request = !!.delete(:multipage) radius = .delete(:radius) || 1000 types = .delete(:types) name = .delete(:name) keyword = .delete(:keyword) = .delete(:retry_options) || {} zagat_selected = .delete(:zagat_selected) || false opennow = .delete(:opennow) || false minprice = .delete(:minprice) || false maxprice = .delete(:maxprice) || false exclude = [] = { :location => location.format, :radius => radius, :key => api_key, :name => name, :keyword => keyword, :retry_options => } [:zagatselected] = zagat_selected if zagat_selected [:opennow] = opennow if opennow [:minprice] = minprice if minprice [:maxprice] = maxprice if maxprice # Accept Types as a string or array if types types = (types.is_a?(Array) ? types.join('|') : types) .merge!(:types => types) end request(:spots_by_radar, multipage_request, exclude, ) end |
.multi_pages_request(method, multipage_request, options) ⇒ Object
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/google_places/spot.rb', line 399 def self.multi_pages_request(method, multipage_request, ) begin response = Request.send(method, ) 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? = { :pagetoken => response["next_page_token"], :key => [:key] } # 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
386 387 388 389 390 391 392 393 394 395 396 397 |
# File 'lib/google_places/spot.rb', line 386 def self.request(method, multipage_request, exclude, ) results = [] self.multi_pages_request(method, multipage_request, ) 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, [:key]) if result['types'].nil? || (result['types'] & exclude) == [] end results end |
Instance Method Details
#[](key) ⇒ Object
471 472 473 |
# File 'lib/google_places/spot.rb', line 471 def [] (key) send(key) end |
#address_component(address_component_type, address_component_length) ⇒ Object
475 476 477 478 479 |
# File 'lib/google_places/spot.rb', line 475 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
481 482 483 |
# File 'lib/google_places/spot.rb', line 481 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
502 503 504 |
# File 'lib/google_places/spot.rb', line 502 def aspects_component(json_aspects) json_aspects.to_a.map{ |r| { :type => r['type'], :rating => r['rating'] } } end |
#events_component(json_events) ⇒ Object
521 522 523 |
# File 'lib/google_places/spot.rb', line 521 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) ⇒ Object
506 507 508 509 510 511 512 513 514 515 516 517 518 519 |
# File 'lib/google_places/spot.rb', line 506 def photos_component(json_photos, api_key) if json_photos json_photos.map{ |p| Photo.new( p['width'], p['height'], p['photo_reference'], p['html_attributions'], api_key ) } else [] end end |
#reviews_component(json_reviews) ⇒ Object
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 |
# File 'lib/google_places/spot.rb', line 485 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, r['profile_photo_url'] ) } else [] end end |