Skip to content

Commit

Permalink
Merge pull request #118 from edwinwills/specific-spot-fields
Browse files Browse the repository at this point in the history
Add functionality to specify spot fields for individual spot requests
  • Loading branch information
edwinwills authored Jun 18, 2019
2 parents 6e1a5e7 + 03bbbdc commit 3b4786d
Show file tree
Hide file tree
Showing 23 changed files with 9,520 additions and 10,359 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.0.0
### Breaking Changes for 2.0.0:
- Removed the `spots_by_radar` method

## 1.2.0
### Added
- Add a region option to the `spots` and `spots_by_query` methods
Expand Down
2 changes: 2 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ However <tt>address_components</tt>, <tt>city</tt>, <tt>country</tt>, <tt>format

* To get these values: You can use <tt>@client.spot(place_id)</tt>. This returns the complete information for the spot by making an extra API call per returned spot.
* To get a collection of these detailed spots: You can use <tt>@client.spots(lat, lng, detail: true)</tt>. This makes an extra call per each spot and returns a collection of the detailed spots.
* To only retrieve specific fields for an individual spot, use a comma delimited string of acceptable fields <tt>@client.spot(place_id, fields:'place_id,name')</tt>


=== Retrieving a list of spots

Expand Down
10 changes: 0 additions & 10 deletions lib/google_places/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,6 @@ def spots_by_pagetoken(pagetoken, options = {})
)
end

# Radar Search Service allows you to search for up to 200 Places at once, but with less detail than is typically returned from a Text Search or Nearby Search request. The search response will include up to 200 Places, identified only by their geographic coordinates and reference. You can send a Place Details request for more information about any of them.
#
# @return [Array<Spot>]
# @param [String,Integer] lat the latitude for the search
Expand Down Expand Up @@ -270,15 +269,6 @@ def spots_by_pagetoken(pagetoken, options = {})
# A boolean to return spots with full detail information(its complete address, phone number, user rating, reviews, etc)
# Note) This makes an extra call for each spot for more information.
#
# @see https://developers.google.com/places/documentation/search#RadarSearchRequests
def spots_by_radar(lat, lng, options = {})
options = @options.merge(options)
detail = options.delete(:detail)
collection_detail_level(
Spot.list_by_radar(lat, lng, @api_key, options),
detail
)
end

# Query for Place Predictions
#
Expand Down
2 changes: 0 additions & 2 deletions lib/google_places/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -318,8 +318,6 @@ def initialize(url, options, follow_redirects = true)
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
Expand Down
15 changes: 10 additions & 5 deletions lib/google_places/spot.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
require 'google_places/review'
module GooglePlaces
class Spot
attr_accessor :lat, :lng, :viewport, :name, :icon, :reference, :vicinity, :types, :id, :formatted_phone_number, :international_phone_number, :formatted_address, :address_components, :street_number, :street, :city, :region, :postal_code, :country, :rating, :url, :cid, :website, :reviews, :aspects, :zagat_selected, :zagat_reviewed, :photos, :review_summary, :nextpagetoken, :price_level, :opening_hours, :events, :utc_offset, :place_id, :permanently_closed
attr_accessor :lat, :lng, :viewport, :name, :icon, :reference, :vicinity, :types, :id, :formatted_phone_number,
:international_phone_number, :formatted_address, :address_components, :street_number, :street, :city, :region,
:postal_code, :country, :rating, :url, :cid, :website, :reviews, :aspects, :zagat_selected, :zagat_reviewed,
:photos, :review_summary, :nextpagetoken, :price_level, :opening_hours, :events, :utc_offset, :place_id, :permanently_closed

# Search for Spots at the provided location
#
Expand Down Expand Up @@ -249,6 +252,7 @@ def self.list_by_radar(lat, lng, api_key, options = {})
# @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.find(place_id, api_key, options = {})
fields = options.delete(:fields)
language = options.delete(:language)
region = options.delete(:region)
retry_options = options.delete(:retry_options) || {}
Expand All @@ -259,7 +263,8 @@ def self.find(place_id, api_key, options = {})
:key => api_key,
:language => language,
:extensions => extensions,
:retry_options => retry_options
:retry_options => retry_options,
:fields => fields
}
request_options[:region] = region unless region.nil?
response = Request.spot(request_options)
Expand Down Expand Up @@ -426,9 +431,9 @@ def initialize(json_result_object, api_key)
@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']
@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']
Expand Down
41 changes: 0 additions & 41 deletions spec/google_places/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,47 +162,6 @@
end
end

describe '::spots_by_radar' do
let(:keywords) { 'landmarks' }
let(:lat) { '51.511627' }
let(:lng) { '-0.183778' }
let(:radius) { 5000 }

it 'should request spots by radar' do
expect(GooglePlaces::Spot).to receive(:list_by_radar).with(lat, lng, api_key, radius: radius, keyword: keywords)
client.spots_by_radar(lat, lng, radius: radius, keyword: keywords)
end

it 'does not call find on GooglePlces::Spot' do
allow(GooglePlaces::Spot).to receive(:list_by_radar) { [fake_spot] }
expect(GooglePlaces::Spot).not_to receive(:find)
client.spots_by_radar(lat, lng, radius: radius, keyword: keywords)
end

context 'with detail set to true' do
it 'calls find on GooglePlaces::Spot' do
allow(GooglePlaces::Spot).to receive(:list_by_radar) { [fake_spot] }
expect(GooglePlaces::Spot).to receive(:find)
client.spots_by_radar(
lat,
lng,
radius: radius,
keyword: keywords,
detail: true
)
end
end

context 'with options' do
let(:client_options) { {radius: 1000} }
let(:method_options) { {radius: radius, keyword: keywords} }
it 'preserves client options while merging with method options' do
allow(GooglePlaces::Spot).to receive(:list_by_radar).with(lat, lng, api_key, method_options)
expect { client.spots_by_radar(lat, lng, method_options) }.not_to change(client, :options)
end
end
end

describe '::predictions_by_input' do
let(:input) { 'Atlanta' }

Expand Down
82 changes: 0 additions & 82 deletions spec/google_places/request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,88 +194,6 @@
end
end



context 'Listing spots by radar', vcr: { cassette_name: 'list_spots_by_radar' } do

context 'with valid options' do
context 'with keyword' do
it do
response = GooglePlaces::Request.spots_by_radar(
:location => @location,
:keyword => @keyword,
:radius => @radius,
:key => api_key
)
expect(response['results']).to_not be_empty
end
end

context 'with name' do
it do
response = GooglePlaces::Request.spots_by_radar(
:location => @location,
:name => 'park',
:radius => @radius,
:key => api_key
)
expect(response['results']).to_not be_empty
end
end
end

context 'without keyword' do
context 'without retry options' do
it do
expect(lambda {
GooglePlaces::Request.spots_by_radar(
:location => @location,
:radius => @radius,
:key => api_key
)
}).to raise_error GooglePlaces::InvalidRequestError
end
end

context 'with retry options' do
context 'without timeout' do
it do
expect(lambda {
GooglePlaces::Request.spots_by_radar(
:location => @location,
:radius => @radius,
:key => api_key,
:retry_options => {
:max => 3,
:status => 'INVALID_REQUEST',
:delay => 1
}
)
}).to raise_error GooglePlaces::RetryError
end
end

context 'with timeout' do
it do
expect(lambda {
GooglePlaces::Request.spots_by_radar(
:location => @location,
:radius => @radius,
:key => api_key,
:retry_options => {
:max => 3,
:status => 'INVALID_REQUEST',
:delay => 10,
:timeout => 1
}
)
}).to raise_error GooglePlaces::RetryTimeoutError
end
end
end
end
end

context 'with an API key not authorized to use the Places API' do
it 'includes the error_message in the exception' do
stub_request(:get, "https://maps.googleapis.com/maps/api/place/details/json").
Expand Down
58 changes: 23 additions & 35 deletions spec/google_places/spot_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
before :each do
@lat = '-33.8670522'
@lng = '151.1957362'
@radius = 200
@radius = 500
@pagetoken = 'CmRVAAAAqKK43TjXKnyEx4-XTWd4bC-iBq88Olspwga_JQbEpznYpfwXYbWBrxmb-1QYD4DMtq8gym5YruCEVjByOlKn8PWKQO5fHvuYD8rWKHUeBvMleM7k3oh9TUG8zqcyuhPmEhCG_C2XuypmkQ20hRvxro4sGhQN3nbWCjgpjyG_E_ayjVIoTGbViw'
@place_id = 'ChIJN1t_tDeuEmsRUsoyG83frY4'
end
Expand All @@ -21,7 +21,7 @@
false,
{
location: "-33.86705220,151.19573620",
radius: 200,
radius: 500,
rankby: "prominence",
key: RSPEC_API_KEY,
name: nil,
Expand All @@ -30,11 +30,9 @@
retry_options: {}
})
end

end

context 'List spots', vcr: { cassette_name: 'list_spots' } do

after(:each) do
expect(@collection.map(&:class).uniq).to eq [GooglePlaces::Spot]
end
Expand All @@ -57,33 +55,20 @@

describe 'with multiple types', vcr: { cassette_name: 'list_spots_with_multiple_types' } do
before(:each) do
@collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :types => ['food','establishment'])
@collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :types => ["atm", "lodging"])
end

it 'should have Spots with specific types' do
@collection.each do |spot|
expect(spot.types & ['food', 'establishment']).to be_any
end
end
end

describe 'searching by name and types', vcr: { cassette_name: 'list_spots_with_name_and_types' } do

before(:each) do
@collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :types => ['food','establishment'], :name => 'italian')
end

it 'should have Spots with specific types' do
@collection.each do |spot|
expect(spot.types & ['food', 'establishment']).to be_any
expect(spot.types & ['atm', 'lodging']).to be_any
end
end
end

describe 'searching by types with exclusion', vcr: { cassette_name: 'list_spots_with_types_and_exclusion' } do

it 'should exclude spots with type "restaurant"' do
@collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :types => ['food','establishment'], :exclude => 'restaurant')
@collection = GooglePlaces::Spot.list(@lat, @lng, api_key, :radius => @radius, :types => ['atm','lodging'], :exclude => 'restaurant')

@collection.map(&:types).each do |types|
expect(types).to_not include('restaurant')
Expand Down Expand Up @@ -143,29 +128,16 @@
it 'should include country in formatted address' do
@collection = GooglePlaces::Spot.list_by_query('Statue of liberty, New York', api_key, region: 'ca')
@collection.each do |spot|
expect(spot.formatted_address).to end_with('United States')
expect(spot.formatted_address).to end_with('USA')
end
end

it 'should not include country in formatted address' do
@collection = GooglePlaces::Spot.list_by_query('Statue of liberty, New York', api_key, region: 'us')
@collection.each do |spot|
expect(spot.formatted_address).to_not end_with('United States')
expect(spot.formatted_address).to_not end_with('USA')
end
end

end

context 'List spots by radar', vcr: { cassette_name: 'list_spots_by_radar' } do

after(:each) do
expect(@collection.map(&:class).uniq).to eq [GooglePlaces::Spot]
end

it 'should be a collection of Spots' do
@collection = GooglePlaces::Spot.list_by_radar('48.8567', '2.3508', api_key, :radius => @radius, :keyword => 'attractions')
end

end

context 'Find a single spot', vcr: { cassette_name: 'single_spot' } do
Expand Down Expand Up @@ -209,4 +181,20 @@
expect(@spot.formatted_address).to_not end_with('Australia')
end
end

context 'Find a single spot with specified params', vcr: { cassette_name: 'single_spot_with_specified_params' } do
it 'should include the specified params in the response' do
@spot = GooglePlaces::Spot.find(@place_id, api_key, fields:'place_id,name')
expect(@spot.place_id).to eq(@place_id)
expect(@spot.name).to eq('Google Australia')
end

it 'should not include unspecified fields' do
@spot = GooglePlaces::Spot.find(@place_id, api_key, region: 'place_id,name')
spot_instance_variable_return_values = (@spot.instance_variables - [@place_id, @name]).map do |iv|
@post.instance_variable_get(iv.to_sym)
end
expect(spot_instance_variable_return_values.compact).to eq([])
end
end
end
Loading

0 comments on commit 3b4786d

Please sign in to comment.