GeoSeeder
Seed your test database with real addresses for apps that rely on geo data for mapping and/or other location features.
Installation
Add this line to your application's Gemfile:
gem 'geo_seeder'
And then execute:
$ bundle
Or install it yourself as:
$ gem install geo_seeder
Usage
PLEASE NOTE: GeoSeeder employs Google Maps' Geocoder API. If you're going to be making a large - >1000 - number of requests (one request is made per GeoSeeder::Location object retured), please register your app with google and obtain an api key. I suggest using figaro to manage your keys. Add this your applicaton.yml:
GOOGLE_KEY: "YOUR_GOOGLE_API_KEY"
Random Addresses
Call GeoSeeder::Location.random to get an array of random GeoSeeder::Location objects. The random method takes center (address_string) radius (in miles) and a quantity as options.
In your seed.rb:
locations = GeoSeeder.random({
center: "10003",
radius: 2,
quantity: 50
})
locations.each do |location|
Event.create!(
name: Faker::Name.name,
email: Faker::Email.email,
street: location.street_number + location.street,
city: location.city,
state: location.state,
zip: location.zip_code
)
end
Setting Coordinates
If you have models that require lat/lng attributes you could use GeoSeeder in a before_validation like so:
before_validation :set_coordinates
def set_coordinates
address_string = [street, city, state].join(", ")
# => "123 Fake Street, Springfield, IL"
location = GeoSeeder::Location.latlng(address_string)
# => #<GeoSeeder::Location:0x007f8771311028>
if location.lat && location.lng
lat, lng = location.lat, location.lng
else
errors[:base] << "Unable to determine geo data from provided address."
end
end
Finding Addresses
If you're starting with a geometric location, perhaps provided by a mobile device, you can user GeoSeeder to return a street address:
location = GeoSeeder::Location.address([40.7492998, -73.8888135])
location.formatted_address
# => "77-11 37th Avenue, Jackson Heights, NY 11372"
.all
Instance method if you want all Location object attributes in hash form.
location = GeoSeeder::Location.random.first
location.all
# => {:formatted_address=>"2467 Bedford Avenue, Brooklyn, NY 11226, USA",
# :lat=>40.643426,
# :lng=>-73.95428,
# :city=>"Brooklyn", ...}
Comming Soon!
- only option - call any class method with the option - only: ["attribute1", "attribute2", ...] - and it will return a hash/array of hashes with only those attributes, all ready for your merging pleasure.
TODO
- More thorough parsing of API response needed for metropolitan areas. Calling random with a center anywhere in NYC gives slightly unexpected/incorrect output in the city/neighborhood fields.
Contributing
- Fork it ( http://github.com/
/geo_seeder/fork ) - Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request