Skip to content

Commit

Permalink
Add place_to to location result
Browse files Browse the repository at this point in the history
  • Loading branch information
tab committed Jul 14, 2023
1 parent 480797d commit f1849f8
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 5 deletions.
6 changes: 4 additions & 2 deletions lib/google_maps/location.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
module Google
module Maps
class Location
attr_reader :address, :latitude, :longitude, :components
attr_reader :address, :latitude, :longitude, :place_id, :components
alias to_s address

def initialize(address, latitude, longitude, components = {})
def initialize(address, latitude, longitude, place_id, components = {})
@address = address
@latitude = latitude
@longitude = longitude
@place_id = place_id
@components = components
end

Expand All @@ -27,6 +28,7 @@ def self.find(address, language = :en)
result.formatted_address,
result.geometry.location.lat,
result.geometry.location.lng,
result.place_id,
format_components(result.address_components)
)
end
Expand Down
3 changes: 2 additions & 1 deletion spec/fixtures/geocoder/amsterdam-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@
}
}
},
"place_id" : "ChIJVXealLU_xkcRja_At0z9AGY",
"types" : [ "locality", "political" ]
}
],
"status" : "OK"
}
}
3 changes: 2 additions & 1 deletion spec/fixtures/geocoder/science-park-400-amsterdam-en.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@
}
}
},
"place_id" : "ChIJIcrTFogJxkcRb0dbtBG4pQc",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
}
3 changes: 2 additions & 1 deletion spec/fixtures/geocoder/science-park-400-amsterdam-nl.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@
}
}
},
"place_id" : "ChIJIcrTFogJxkcRb0dbtBG4pQc",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
}
10 changes: 10 additions & 0 deletions spec/google_maps_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
expect(components['route']).to eq(['Science Park Amsterdam'])
expect(components['street_number']).to eq(['400'])
expect(components['sublocality']).to eq(%w[Middenmeer Watergraafsmeer])
expect(location.place_id).to eq('ChIJIcrTFogJxkcRb0dbtBG4pQc')
end

it 'should handle multiple location for an address' do
Expand All @@ -80,6 +81,7 @@
expect(location.address).to eq('Amsterdam, NY, USA')
expect(location.latitude).to eq(42.93868560)
expect(location.longitude).to eq(-74.18818580)
expect(location.place_id).to eq('ChIJVXealLU_xkcRja_At0z9AGY')
end

it 'should accept languages other than en' do
Expand All @@ -89,6 +91,14 @@
expect(location.address).to eq('Science Park 400, Amsterdam, 1098 XH Amsterdam, Nederland')
end

it 'should return place_id' do
stub_response('geocoder/science-park-400-amsterdam-nl.json')

location = Google::Maps.geocode('Science Park 400, Amsterdam', :nl).first
puts location.inspect
expect(location.place_id).to eq('ChIJIcrTFogJxkcRb0dbtBG4pQc')
end

it 'should return an empty array when an address could not be geocoded' do
stub_response('zero-results.json')

Expand Down

0 comments on commit f1849f8

Please sign in to comment.