geo_seeder 0.0.2

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/.DS_Store ADDED
Binary file
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ keys.yml
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 paris
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # GeoSeeder
2
+
3
+ Seed your test database with real addresses for those apps that impliment maping and/or other location features.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'geo_seeder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install geo_seeder
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it ( http://github.com/<my-github-username>/geo_seeder/fork )
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'geo_seeder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "geo_seeder"
8
+ spec.version = GeoSeeder::VERSION
9
+ spec.authors = ["paris"]
10
+ spec.email = ["paris.yee@gmail.com"]
11
+ spec.summary = %q{Provides real addresses for populating models/databases.}
12
+ spec.description = %q{Provides real addresses for populating models/databases.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.add_runtime_dependency "addressable"
17
+ spec.add_runtime_dependency "rest-client"
18
+ spec.add_runtime_dependency "json"
19
+ spec.add_runtime_dependency "rails"
20
+
21
+ spec.files = `git ls-files -z`.split("\x0")
22
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
23
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.5"
27
+ spec.add_development_dependency "rake"
28
+ end
@@ -0,0 +1,3 @@
1
+ module GeoSeeder
2
+ VERSION = "0.0.2"
3
+ end
data/lib/geo_seeder.rb ADDED
@@ -0,0 +1,168 @@
1
+ require "geo_seeder/version"
2
+ require "addressable/uri"
3
+ require "rest_client"
4
+ require "json"
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
+ module GeoSeeder
49
+ class Location
50
+
51
+ API_KEY = ENV["GOOGLE_KEY"]
52
+
53
+ attr_reader :formatted_address, :street_number, :street, :neighborhood,
54
+ :city, :state, :state_abrv, :zip_code, :county,
55
+ :country, :lat, :lng
56
+
57
+ def initialize(options)
58
+ @formatted_address = options["formatted_address"]
59
+ @lat = options["geometry"]["location"]["lat"]
60
+ @lng = options["geometry"]["location"]["lng"]
61
+
62
+ options["address_components"].each do |comp|
63
+ types = comp["types"]
64
+ ln = comp["long_name"]
65
+ sn = comp["short_name"]
66
+
67
+ if types.include?("street_number")
68
+ @street_number = ln
69
+ elsif types.include?("route")
70
+ @street = ln
71
+ elsif types.include?("administrative_area_level_1")
72
+ @state = ln
73
+ @state_abrv = sn
74
+ elsif types.include?("administrative_area_level_2")
75
+ @county = ln
76
+ elsif types.include?("sublocality") ||
77
+ types.include?("administrative_area_level_3")
78
+ @city = ln
79
+ elsif types.include?("postal_code")
80
+ @zip_code = ln
81
+ elsif types.include?("neighborhood")
82
+ @neighborhood = ln
83
+ end
84
+ end
85
+ end
86
+
87
+ def coordinates
88
+ [self.lat, self.lng]
89
+ end
90
+
91
+ def all
92
+ {
93
+ formatted_address: self.formatted_address,
94
+ lat: self.lat,
95
+ lng: self.lng,
96
+ number: self.number,
97
+ street: self.street,
98
+ city: self.city,
99
+ county: self.county,
100
+ state: self.state,
101
+ country: self.country,
102
+ zip_code: self.zip_code
103
+ }
104
+ end
105
+
106
+ def self.random(defaults = {})
107
+ options = {
108
+ center: "Brooklyn, NY",
109
+ quantity: 1,
110
+ radius: 1
111
+ }.merge(defaults)
112
+
113
+ center_point = latlng(options[:center])
114
+ center_lat = center_point.lat
115
+ center_lng = center_point.lng
116
+ diff = options[:radius] * 1.0 / 70.0
117
+
118
+ results = []
119
+ i = 0
120
+ while results.count < options[:quantity]
121
+ rand_lat = (center_lat + rand(-diff..diff)).round(7)
122
+ rand_lng = (center_lng + rand(-diff..diff)).round(7)
123
+
124
+ rand_address = address([rand_lat, rand_lng])
125
+
126
+ puts i
127
+ i += 1
128
+ if rand_address && rand_address.street_number
129
+ results << rand_address
130
+ end
131
+ end
132
+
133
+ results
134
+ end
135
+
136
+ def self.address(latlng)
137
+ response = api_request({latlng: latlng})
138
+ puts response.first
139
+ response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
140
+ end
141
+
142
+ def self.latlng(address_string)
143
+ response = api_request({address: address_string})
144
+ response.count > 0 ? GeoSeeder::Location.new(response.first) : nil
145
+ end
146
+
147
+ def self.api_request(options = {})
148
+ # options must either contain an address string or a latlng array
149
+ if options[:latlng]
150
+ options[:latlng] = options[:latlng][0].to_s + "," + options[:latlng][1].to_s
151
+ end
152
+
153
+ q_vals = {
154
+ sensor: "false",
155
+ key: API_KEY
156
+ }.merge(options)
157
+
158
+ query_string = Addressable::URI.new(
159
+ :scheme => "https",
160
+ :host => "maps.googleapis.com",
161
+ :path => "maps/api/geocode/json",
162
+ :query_values => q_vals
163
+ ).to_s
164
+
165
+ JSON.parse(RestClient.get(query_string))["results"]
166
+ end
167
+ end
168
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "../lib/geo_seeder"
2
+
3
+ describe "GeoSeeder" do
4
+ context "Location" do
5
+
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]})
9
+ expect(response.is_a?(Array)).to be true
10
+ end
11
+ end
12
+
13
+ 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/)
19
+ end
20
+ end
21
+
22
+ 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")
25
+ end
26
+ end
27
+ end
28
+ end
metadata ADDED
@@ -0,0 +1,153 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: geo_seeder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - paris
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-03-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: addressable
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rest-client
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
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
+ - !ruby/object:Gem::Dependency
79
+ name: bundler
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '1.5'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '1.5'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rake
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ! '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ description: Provides real addresses for populating models/databases.
111
+ email:
112
+ - paris.yee@gmail.com
113
+ executables: []
114
+ extensions: []
115
+ extra_rdoc_files: []
116
+ files:
117
+ - .DS_Store
118
+ - .gitignore
119
+ - Gemfile
120
+ - LICENSE.txt
121
+ - README.md
122
+ - Rakefile
123
+ - geo_seeder.gemspec
124
+ - lib/geo_seeder.rb
125
+ - lib/geo_seeder/version.rb
126
+ - spec/geo_seeder_spec.rb
127
+ homepage: ''
128
+ licenses:
129
+ - MIT
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.23
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Provides real addresses for populating models/databases.
152
+ test_files:
153
+ - spec/geo_seeder_spec.rb