geo_seeder 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +52 -2
- data/lib/geo_seeder/version.rb +1 -1
- data/lib/geo_seeder.rb +20 -57
- metadata +1 -1
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# GeoSeeder
|
2
2
|
|
3
|
-
Seed your test database with real addresses for
|
3
|
+
Seed your test database with real addresses for apps that rely on geo data for mapping and/or other location features.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -17,9 +17,59 @@ Or install it yourself as:
|
|
17
17
|
$ gem install geo_seeder
|
18
18
|
|
19
19
|
## Usage
|
20
|
+
PLEASE NOTE: GeoSeeder employs Google Maps' Geocoder API. If you're going to be making a large number of requests (one request per GeoSeeder::Location object retured), please register your app with google and obtain an api key. I suggest using [figaro](https://github.com/laserlemon/figaro) to manage your keys. Add this your applicaton.yml:
|
21
|
+
```yaml
|
22
|
+
GOOGLE_KEY: "YOUR_GOOGLE_API_KEY"
|
23
|
+
```
|
24
|
+
### Random Addresses
|
25
|
+
Call GeoSeeder::Location.random to get a random an array of GeoSeeder::Location objects. The random method takes center (address_string) radius (in miles) and a quantity.
|
20
26
|
|
21
|
-
|
27
|
+
In your seed.rb:
|
28
|
+
```ruby
|
29
|
+
locations = GeoSeeder.random({
|
30
|
+
center: "10003",
|
31
|
+
radius: 2,
|
32
|
+
quantity: 50
|
33
|
+
})
|
22
34
|
|
35
|
+
locations.each do |location|
|
36
|
+
Event.create!(
|
37
|
+
name: Faker::Name.name,
|
38
|
+
email: Faker::Email.email,
|
39
|
+
street: location.street_number + location.street,
|
40
|
+
city: location.city,
|
41
|
+
state: location.state,
|
42
|
+
zip: location.zip_code
|
43
|
+
)
|
44
|
+
end
|
45
|
+
```
|
46
|
+
### Setting Coordinates
|
47
|
+
If you have models that require lat/lng attributes you could use GeoSeeder in a before_validation like so:
|
48
|
+
```ruby
|
49
|
+
before_validation :set_coordinates
|
50
|
+
|
51
|
+
def set_coordinates
|
52
|
+
address_string = [street, city, state].join(", ")
|
53
|
+
# => "123 Fake Street, Springfield, IL"
|
54
|
+
|
55
|
+
location = GeoSeeder::Location.latlng(address_string)
|
56
|
+
# => #<GeoSeeder::Location:0x007f8771311028>
|
57
|
+
|
58
|
+
if location.lat && location.lng
|
59
|
+
lat, lng = location.lat, location.lng
|
60
|
+
else
|
61
|
+
errors[:base] << "Unable to determine geo data based on provided address."
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
### Finding Addresses
|
67
|
+
If you're starting with a geometric location, perhaps provided by a mobile device, you can user GeoSeeder to return a street address:
|
68
|
+
```ruby
|
69
|
+
location = GeoSeeder::Location.address([40.7492998, -73.8888135])
|
70
|
+
location.formatted_address
|
71
|
+
# => "77-11 37th Avenue, Jackson Heights, NY 11372"
|
72
|
+
```
|
23
73
|
## Contributing
|
24
74
|
|
25
75
|
1. Fork it ( http://github.com/<my-github-username>/geo_seeder/fork )
|
data/lib/geo_seeder/version.rb
CHANGED
data/lib/geo_seeder.rb
CHANGED
@@ -3,48 +3,6 @@ require "addressable/uri"
|
|
3
3
|
require "rest_client"
|
4
4
|
require "json"
|
5
5
|
|
6
|
-
|
7
|
-
# What do I want to be able to do?
|
8
|
-
#
|
9
|
-
# In seed file i want to be able to do
|
10
|
-
#
|
11
|
-
# locations = GeoSeeder.random({
|
12
|
-
# center: "10003",
|
13
|
-
# radius: 2,
|
14
|
-
# quantity: 50
|
15
|
-
# })
|
16
|
-
#
|
17
|
-
# locations.each do |location|
|
18
|
-
# Event.create!(
|
19
|
-
# name: Faker::Name.name,
|
20
|
-
# email: Faker::Email.email,
|
21
|
-
# street: location.street_number + location.street,
|
22
|
-
# city: location.city,
|
23
|
-
# state: location.state,
|
24
|
-
# zip: location.zip_code
|
25
|
-
# )
|
26
|
-
# end
|
27
|
-
#
|
28
|
-
#
|
29
|
-
# In a model I want to be able to use it for before validation
|
30
|
-
#
|
31
|
-
# before_validation :set_coordinates
|
32
|
-
#
|
33
|
-
# def set_coordinates
|
34
|
-
# address_string = [street, city, state].join(", ")
|
35
|
-
# => "123 Fake Street, Springfield, IL"
|
36
|
-
#
|
37
|
-
# location = GeoSeeder::Location.latlng(address_string)
|
38
|
-
# => #<GeoSeeder::Location:0x007f8771311028>
|
39
|
-
#
|
40
|
-
# if location.lat && location.lng
|
41
|
-
# lat, lng = location.lat, location.lng
|
42
|
-
# else
|
43
|
-
# errors[:base] << "Unable to determine geo data based on provided address."
|
44
|
-
# end
|
45
|
-
# end
|
46
|
-
|
47
|
-
|
48
6
|
module GeoSeeder
|
49
7
|
class Location
|
50
8
|
|
@@ -89,18 +47,25 @@ module GeoSeeder
|
|
89
47
|
end
|
90
48
|
|
91
49
|
def all
|
92
|
-
{
|
93
|
-
formatted_address:
|
94
|
-
lat:
|
95
|
-
lng:
|
96
|
-
|
97
|
-
street:
|
98
|
-
city:
|
99
|
-
county:
|
100
|
-
state:
|
101
|
-
|
102
|
-
|
50
|
+
addr_hash = {
|
51
|
+
formatted_address: formatted_address,
|
52
|
+
lat: lat,
|
53
|
+
lng: lng,
|
54
|
+
street_number: street_number,
|
55
|
+
street: street,
|
56
|
+
city: city,
|
57
|
+
county: county,
|
58
|
+
state: state,
|
59
|
+
state_abrv: state_abrv,
|
60
|
+
country: country,
|
61
|
+
zip_code: zip_code
|
103
62
|
}
|
63
|
+
|
64
|
+
if neighborhood
|
65
|
+
addr_hash[:neighborhood] = neighborhood
|
66
|
+
end
|
67
|
+
|
68
|
+
addr_hash
|
104
69
|
end
|
105
70
|
|
106
71
|
def self.random(defaults = {})
|
@@ -116,15 +81,13 @@ module GeoSeeder
|
|
116
81
|
diff = options[:radius] * 1.0 / 70.0
|
117
82
|
|
118
83
|
results = []
|
119
|
-
|
84
|
+
|
120
85
|
while results.count < options[:quantity]
|
121
86
|
rand_lat = (center_lat + rand(-diff..diff)).round(7)
|
122
87
|
rand_lng = (center_lng + rand(-diff..diff)).round(7)
|
123
88
|
|
124
89
|
rand_address = address([rand_lat, rand_lng])
|
125
90
|
|
126
|
-
puts i
|
127
|
-
i += 1
|
128
91
|
if rand_address && rand_address.street_number
|
129
92
|
results << rand_address
|
130
93
|
end
|
@@ -135,7 +98,6 @@ module GeoSeeder
|
|
135
98
|
|
136
99
|
def self.address(latlng)
|
137
100
|
response = api_request({latlng: latlng})
|
138
|
-
puts response.first
|
139
101
|
response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
|
140
102
|
end
|
141
103
|
|
@@ -144,6 +106,7 @@ module GeoSeeder
|
|
144
106
|
response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
|
145
107
|
end
|
146
108
|
|
109
|
+
protected
|
147
110
|
def self.api_request(options = {})
|
148
111
|
# options must either contain an address string or a latlng array
|
149
112
|
if options[:latlng]
|