geo_seeder 1.0.0 → 1.0.1

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/.gitignore CHANGED
@@ -16,3 +16,4 @@ test/tmp
16
16
  test/version_tmp
17
17
  tmp
18
18
  keys.yml
19
+ .DS_Store
data/README.md CHANGED
@@ -58,7 +58,7 @@ def set_coordinates
58
58
  if location.lat && location.lng
59
59
  lat, lng = location.lat, location.lng
60
60
  else
61
- errors[:base] << "Unable to determine geo data based on provided address."
61
+ errors[:base] << "Unable to determine geo data from provided address."
62
62
  end
63
63
  end
64
64
  ```
@@ -71,8 +71,18 @@ location.formatted_address
71
71
  # => "77-11 37th Avenue, Jackson Heights, NY 11372"
72
72
  ```
73
73
 
74
+ ### .all
75
+ Instance method if you want all Location object attributes in hash form.
76
+ ```ruby
77
+ location = GeoSeeder::Location.random.first
78
+ location.all
79
+ # => {:formatted_address=>"2467 Bedford Avenue, Brooklyn, NY 11226, USA",
80
+ # :lat=>40.643426,
81
+ # :lng=>-73.95428,
82
+ # :city=>"Brooklyn", ...}
83
+ ```
84
+
74
85
  ### Comming Soon!
75
- * .all instance method - returns a hash containing all of a locations object's attributes.
76
86
  * output option - call any class method with the option - output: ["attribute1", "attribute2", ...] - and it will return a hash/array of hashes with only those attributes, all ready for your merging pleasure.
77
87
 
78
88
  ## Contributing
@@ -1,3 +1,3 @@
1
1
  module GeoSeeder
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
data/lib/geo_seeder.rb CHANGED
@@ -7,9 +7,21 @@ module GeoSeeder
7
7
  class Location
8
8
 
9
9
  API_KEY = ENV["GOOGLE_KEY"]
10
-
10
+ ADDRESS_COMPONENT_TYPES = {
11
+ "street_number" => "@street_number",
12
+ "route" => "@street",
13
+ "neighborhood" => "@neighborhood",
14
+ "administrative_area_level_1" => "@state",
15
+ "administrative_area_level_2" => "@county",
16
+ "locality" => "@city",
17
+ "administrative_area_level_3" => "@city",
18
+ "sublocality" => "@city",
19
+ "postal_code" => "@zip_code",
20
+ "country" => "@country"
21
+ }
22
+
11
23
  attr_reader :formatted_address, :street_number, :street, :neighborhood,
12
- :city, :state, :state_abrv, :zip_code, :county,
24
+ :city, :state, :zip_code, :county, :region,
13
25
  :country, :lat, :lng
14
26
 
15
27
  def initialize(options)
@@ -18,28 +30,11 @@ module GeoSeeder
18
30
  @lng = options["geometry"]["location"]["lng"]
19
31
 
20
32
  options["address_components"].each do |comp|
21
- types = comp["types"]
33
+ type = comp["types"].first
22
34
  ln = comp["long_name"]
23
- sn = comp["short_name"]
24
35
 
25
- if types.include?("street_number")
26
- @street_number = ln
27
- elsif types.include?("route")
28
- @street = ln
29
- elsif types.include?("administrative_area_level_1")
30
- @state = ln
31
- @state_abrv = sn
32
- elsif types.include?("administrative_area_level_2")
33
- @county = ln
34
- elsif types.include?("sublocality") ||
35
- types.include?("administrative_area_level_3")
36
- @city = ln
37
- elsif types.include?("postal_code")
38
- @zip_code = ln
39
- elsif types.include?("neighborhood")
40
- @neighborhood = ln
41
- elsif types.include?("country")
42
- @country = ln
36
+ unless eval(ADDRESS_COMPONENT_TYPES[type])
37
+ eval("#{ADDRESS_COMPONENT_TYPES[type]} = '#{ln}'")
43
38
  end
44
39
  end
45
40
  end
@@ -66,18 +61,10 @@ module GeoSeeder
66
61
  radius: 1
67
62
  }.merge(defaults)
68
63
 
69
- center_point = latlng(options[:center])
70
- center_lat = center_point.lat
71
- center_lng = center_point.lng
72
- diff = options[:radius] * 1.0 / 70.0
73
-
74
64
  results = []
75
65
 
76
66
  while results.count < options[:quantity]
77
- rand_lat = (center_lat + rand(-diff..diff)).round(7)
78
- rand_lng = (center_lng + rand(-diff..diff)).round(7)
79
-
80
- rand_address = address([rand_lat, rand_lng])
67
+ rand_address = address(rand_coords(options[:center], options[:radius]))
81
68
 
82
69
  if rand_address && rand_address.street_number
83
70
  results << rand_address
@@ -87,6 +74,7 @@ module GeoSeeder
87
74
  results
88
75
  end
89
76
 
77
+
90
78
  def self.address(latlng)
91
79
  response = api_request({latlng: latlng})
92
80
  location = response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
@@ -103,6 +91,18 @@ module GeoSeeder
103
91
  end
104
92
 
105
93
  protected
94
+ def self.rand_coords(center, radius)
95
+ center_pt = nil
96
+ until center_pt
97
+ center_pt = latlng(center)
98
+ end
99
+ center_coords = [center_pt.lat, center_pt.lng]
100
+ diff = radius * 1.0 / 70.0
101
+ center_coords.map do |coord|
102
+ (coord + rand(-diff..diff)).round(7)
103
+ end
104
+ end
105
+
106
106
  def self.api_request(options = {})
107
107
  # options must either contain an address string or a latlng array
108
108
  if options[:latlng]
@@ -121,7 +121,6 @@ module GeoSeeder
121
121
  :query_values => q_vals
122
122
  ).to_s
123
123
 
124
-
125
124
  JSON.parse(RestClient.get(query_string))["results"]
126
125
  end
127
126
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_seeder
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-08 00:00:00.000000000 Z
12
+ date: 2014-03-10 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable