geo_seeder 0.0.3 → 1.0.0

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 CHANGED
@@ -17,12 +17,12 @@ 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:
20
+ 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](https://github.com/laserlemon/figaro) to manage your keys. Add this your applicaton.yml:
21
21
  ```yaml
22
22
  GOOGLE_KEY: "YOUR_GOOGLE_API_KEY"
23
23
  ```
24
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.
25
+ 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.
26
26
 
27
27
  In your seed.rb:
28
28
  ```ruby
@@ -70,6 +70,11 @@ location = GeoSeeder::Location.address([40.7492998, -73.8888135])
70
70
  location.formatted_address
71
71
  # => "77-11 37th Avenue, Jackson Heights, NY 11372"
72
72
  ```
73
+
74
+ ### Comming Soon!
75
+ * .all instance method - returns a hash containing all of a locations object's attributes.
76
+ * 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
+
73
78
  ## Contributing
74
79
 
75
80
  1. Fork it ( http://github.com/<my-github-username>/geo_seeder/fork )
data/geo_seeder.gemspec CHANGED
@@ -6,7 +6,7 @@ require 'geo_seeder/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "geo_seeder"
8
8
  spec.version = GeoSeeder::VERSION
9
- spec.authors = ["paris"]
9
+ spec.authors = ["Paris Yee"]
10
10
  spec.email = ["paris.yee@gmail.com"]
11
11
  spec.summary = %q{Provides real addresses for populating models/databases.}
12
12
  spec.description = %q{Provides real addresses for populating models/databases.}
@@ -16,7 +16,6 @@ Gem::Specification.new do |spec|
16
16
  spec.add_runtime_dependency "addressable"
17
17
  spec.add_runtime_dependency "rest-client"
18
18
  spec.add_runtime_dependency "json"
19
- spec.add_runtime_dependency "rails"
20
19
 
21
20
  spec.files = `git ls-files -z`.split("\x0")
22
21
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
@@ -1,3 +1,3 @@
1
1
  module GeoSeeder
2
- VERSION = "0.0.3"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/geo_seeder.rb CHANGED
@@ -38,6 +38,8 @@ module GeoSeeder
38
38
  @zip_code = ln
39
39
  elsif types.include?("neighborhood")
40
40
  @neighborhood = ln
41
+ elsif types.include?("country")
42
+ @country = ln
41
43
  end
42
44
  end
43
45
  end
@@ -47,23 +49,12 @@ module GeoSeeder
47
49
  end
48
50
 
49
51
  def all
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
62
- }
52
+ addr_hash = {}
63
53
 
64
- if neighborhood
65
- addr_hash[:neighborhood] = neighborhood
66
- end
54
+ instance_variables.each do |iv|
55
+ variable = iv.to_s[1..-1]
56
+ addr_hash[variable.to_sym] = eval("self.#{variable}")
57
+ end
67
58
 
68
59
  addr_hash
69
60
  end
@@ -98,12 +89,17 @@ module GeoSeeder
98
89
 
99
90
  def self.address(latlng)
100
91
  response = api_request({latlng: latlng})
101
- response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
92
+ location = response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
93
+ return nil if location && location.formatted_address == "Foo Bar, San Francisco, CA 94115, USA"
94
+ # Google returns a dummy address for bad latlng requests.
95
+ location
102
96
  end
103
97
 
104
98
  def self.latlng(address_string)
105
99
  response = api_request({address: address_string})
106
- response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
100
+ location = response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
101
+ return nil if location && location.formatted_address == "Foo Bar, San Francisco, CA 94115, USA"
102
+ location
107
103
  end
108
104
 
109
105
  protected
@@ -125,6 +121,7 @@ module GeoSeeder
125
121
  :query_values => q_vals
126
122
  ).to_s
127
123
 
124
+
128
125
  JSON.parse(RestClient.get(query_string))["results"]
129
126
  end
130
127
  end
@@ -4,24 +4,93 @@ describe "GeoSeeder" do
4
4
  context "Location" do
5
5
 
6
6
  describe "api_request" do
7
- it "should return an array of result hashes" do
8
- response = GeoSeeder::Location.api_request({latlng: [40.7493, -71.8888]})
7
+ it "returns an array of result google address hashes for valid request input" do
8
+ response = GeoSeeder::Location.api_request({latlng: [40.7492998, -73.8888135]})
9
9
  expect(response.is_a?(Array)).to be true
10
+ expect(response.first.is_a?(Hash)).to be true
11
+ expect(response.first["formatted_address"]).to match(/NY 11372/)
12
+ end
13
+
14
+ it "returns an empty array for requests that yield no results" do
15
+ response = GeoSeeder::Location.api_request({address: "asdfjkhsdf"})
16
+ expect(response.is_a?(Array)).to be true
17
+ expect(response).to be_empty
10
18
  end
11
19
  end
12
20
 
13
21
  describe "latlng" do
14
- it "returns a hash with :lat and :lng keys when called with an address string" do
15
- response = GeoSeeder::Location.latlng("77-11 37th Ave, Jackson Heights, NY 11372")
16
- expect(response.is_a?(Hash)).to be true
17
- expect(response["lat"].to_s).to match(/40.7492/)
18
- expect(response["lng"]to_s).to match(/-73.8888/)
22
+ let(:location) { GeoSeeder::Location.latlng("77-11 37th Ave, Jackson Heights, NY 11372") }
23
+
24
+ it "returns an a Location object" do
25
+ expect(location.class).to eq(GeoSeeder::Location)
26
+ end
27
+
28
+ it "returns an object with lat/lng attributes set as floats" do
29
+ expect(location.lat.class).to eq(Float)
30
+ expect(location.lng.class).to eq(Float)
31
+ end
32
+
33
+ it "represent the closest approximation of the provided address (if valid)" do
34
+ expect(location.formatted_address).to match(/77-11 37th Ave/)
35
+ end
36
+
37
+ it "retruns nil for invalid input" do
38
+ bad_location = GeoSeeder::Location.latlng("asdfasdf")
39
+ expect(bad_location).to be_nil
19
40
  end
20
41
  end
21
42
 
22
43
  describe "address" do
23
- it "returns the proper address when given an array of latlng floats" do
24
- expect(GeoSeeder::Location.address([40.7492998, -73.8888135])).to eq("77-11 37th Ave, Jackson Heights, NY 11372")
44
+ let(:location) { GeoSeeder::Location.address([40.7307596, -73.9913105]) }
45
+
46
+ it "returns an a Location object" do
47
+ expect(location.class).to eq(GeoSeeder::Location)
48
+ end
49
+
50
+ it "returns an object with proper address attributes set" do
51
+ expect(location.street).not_to be_nil
52
+ expect(location.city).not_to be_nil
53
+ expect(location.country).not_to be_nil
54
+ expect(location.state).not_to be_nil
55
+ expect(location.zip_code).not_to be_nil
56
+ end
57
+
58
+ it "retruns nil for invalid input" do
59
+ bad_location = GeoSeeder::Location.latlng([3441283, 88942838])
60
+ expect(bad_location).to be_nil
61
+ end
62
+ end
63
+
64
+ describe "all" do
65
+ let(:loc_attrs) { GeoSeeder::Location.latlng("Brooklyn, NY").all }
66
+
67
+ it "returns a hash of all attributes of a location object" do
68
+ expect(loc_attrs.is_a?(Hash)).to be true
69
+ end
70
+
71
+ it "only returns hash with keys of instance variables possessed by Location object" do
72
+ expect(loc_attrs[:city]).to eq("Brooklyn")
73
+ expect(loc_attrs.has_key?(:street_number)).to be false
74
+ end
75
+ end
76
+
77
+ describe "random" do
78
+ let(:locations) {
79
+ GeoSeeder::Location.random({
80
+ center: "54 LeFever Ln, New Paltz, NY 12561",
81
+ radius: 5,
82
+ quantity: 3
83
+ })
84
+ }
85
+
86
+ it "retruns an array of Location objects of length - quantity" do
87
+ expect(locations.is_a?(Array)).to be true
88
+ expect(locations.sample.class).to eq(GeoSeeder::Location)
89
+ expect(locations.count).to eq(3)
90
+ end
91
+
92
+ it "only returns objects with full addresses, i.e. only addresses with street_numbers" do
93
+ expect(locations.all? { |loc| loc.street_number =~ /\d+/}).to be true
25
94
  end
26
95
  end
27
96
  end
metadata CHANGED
@@ -1,15 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geo_seeder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
- - paris
8
+ - Paris Yee
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-07 00:00:00.000000000 Z
12
+ date: 2014-03-08 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: addressable
@@ -59,22 +59,6 @@ dependencies:
59
59
  - - ! '>='
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
- - !ruby/object:Gem::Dependency
63
- name: rails
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
70
- type: :runtime
71
- prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
62
  - !ruby/object:Gem::Dependency
79
63
  name: bundler
80
64
  requirement: !ruby/object:Gem::Requirement