us_zipcode 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +7 -0
  3. data/.travis.yml +19 -0
  4. data/Gemfile +21 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.textile +179 -0
  7. data/README.textile~ +184 -0
  8. data/Rakefile +19 -0
  9. data/features/step_definitions/common_steps.rb +76 -0
  10. data/features/step_definitions/rails_setup_steps.rb +17 -0
  11. data/features/step_definitions/rails_setup_steps.rb~ +17 -0
  12. data/features/support/env.rb +10 -0
  13. data/features/support/string.rb +128 -0
  14. data/features/zipcodes.feature +24 -0
  15. data/features/zipcodes.feature~ +24 -0
  16. data/lib/generators/us_zipcode/models_generator.rb +41 -0
  17. data/lib/generators/us_zipcode/models_generator.rb~ +41 -0
  18. data/lib/generators/us_zipcode/templates/county_model.rb +17 -0
  19. data/lib/generators/us_zipcode/templates/county_model.rb~ +17 -0
  20. data/lib/generators/us_zipcode/templates/migration.rb +41 -0
  21. data/lib/generators/us_zipcode/templates/migration.rb~ +41 -0
  22. data/lib/generators/us_zipcode/templates/state_model.rb +15 -0
  23. data/lib/generators/us_zipcode/templates/state_model.rb~ +15 -0
  24. data/lib/generators/us_zipcode/templates/zipcode_model.rb +30 -0
  25. data/lib/generators/us_zipcode/templates/zipcode_model.rb~ +30 -0
  26. data/lib/generators/us_zipcode/templates/zipcodes.rake +163 -0
  27. data/lib/generators/us_zipcode/templates/zipcodes.rake~ +163 -0
  28. data/lib/generators/us_zipcode/version.rb +3 -0
  29. data/lib/generators/us_zipcode/version.rb~ +3 -0
  30. data/lib/my_zipcode_gem.rb~ +34 -0
  31. data/lib/us_zipcode.rb +29 -0
  32. data/lib/us_zipcode.rb~ +29 -0
  33. data/rails3_2.gemfile +21 -0
  34. data/rails4_0.gemfile +23 -0
  35. data/rails4_1.gemfile +23 -0
  36. data/us_zipcode.gemspec +24 -0
  37. data/us_zipcode.gemspec~ +24 -0
  38. metadata +108 -0
@@ -0,0 +1,163 @@
1
+ require 'open-uri'
2
+ require 'csv'
3
+ namespace :zipcodes do
4
+
5
+ desc "Update states table"
6
+ task :update_states => :environment do
7
+ puts ">>> Begin update of states table..."
8
+ url = "https://github.com/midwire/free_zipcode_data/raw/master/all_us_states.csv"
9
+ data = open(url)
10
+ file = nil
11
+ if data.is_a? StringIO
12
+ file = Tempfile.new('all_us_states.csv')
13
+ file.write(data.read)
14
+ file.flush
15
+ file.close
16
+ else
17
+ file = data
18
+ end
19
+ CSV.foreach(file.path, :headers => true) do |row|
20
+ puts "Updating state: [#{row['name']}]"
21
+ state = State.where(abbr: row['abbr']).first_or_initialize
22
+ state.update_attribute(:name, row['name'])
23
+ end
24
+ data.close
25
+ puts ">>> End update of states table..."
26
+ end
27
+
28
+ desc "Update counties table"
29
+ task :update_counties => :update_states do
30
+ puts ">>> Begin update of counties table..."
31
+ url = "https://github.com/midwire/free_zipcode_data/raw/master/all_us_counties.csv"
32
+ data = open(url)
33
+ file = nil
34
+ if data.is_a? StringIO
35
+ file = Tempfile.new('all_us_counties.csv')
36
+ file.write(data.read)
37
+ file.flush
38
+ file.close
39
+ else
40
+ file = data
41
+ end
42
+ CSV.foreach(file.path, :headers => true) do |row|
43
+ puts "Updating county: [#{row['name']}]"
44
+ # lookup state
45
+ state = State.find_by_abbr!(row['state'])
46
+ county = County.where(name: row['name'], state_id: state.to_param).first_or_initialize
47
+ county.save
48
+ end
49
+ data.close
50
+ puts ">>> End update of counties table..."
51
+ end
52
+
53
+ desc "Update zipcodes table"
54
+ task :update_zipcodes => :update_counties do
55
+ puts ">>> Begin update of zipcodes table..."
56
+ url = "https://github.com/midwire/free_zipcode_data/raw/master/all_us_zipcodes.csv"
57
+ data = open(url)
58
+ file = nil
59
+ if data.is_a? StringIO
60
+ file = Tempfile.new('all_us_zipcodes.csv')
61
+ file.write(data.read)
62
+ file.flush
63
+ file.close
64
+ else
65
+ file = data
66
+ end
67
+ CSV.foreach(file.path, :headers => true) do |row|
68
+ puts "Updating zipcode: [#{row['code']}], '#{row['city']}, #{row['state']}, #{row['county']}"
69
+ # lookup state
70
+ state = State.find_by_abbr!(row['state'])
71
+ begin
72
+ county = County.find_by_name_and_state_id!(row['county'], state.to_param)
73
+ rescue Exception => e
74
+ puts ">>> e: [#{e}]"
75
+ puts ">>>> No county found for zipcode: [#{row['code']}], '#{row['city']}, #{row['state']}, #{row['county']}... SKIPPING..."
76
+ next
77
+ end
78
+ zipcode = Zipcode.where(code: row['code']).first_or_initialize
79
+ zipcode.update_attributes!(
80
+ :city => row['city'].titleize,
81
+ :state_id => state.to_param,
82
+ :county_id => county.to_param,
83
+ :lat => row['lat'],
84
+ :lon => row['lon']
85
+ )
86
+ end
87
+ data.close
88
+ puts ">>> End update of zipcodes table..."
89
+ end
90
+
91
+ desc "Populate or update the zipcodes related tables"
92
+ task :update => :environment do
93
+ Rake::Task['zipcodes:update_zipcodes'].invoke
94
+ end
95
+
96
+ desc "Export US States to a .csv file"
97
+ task :export_states => :environment do
98
+ @states = State.order("name ASC")
99
+ csv_string = CSV.generate do |csv|
100
+ csv << ["abbr", "name"]
101
+ @states.each do |state|
102
+ csv << [
103
+ state.abbr,
104
+ state.name
105
+ ]
106
+ end
107
+ end
108
+ filename = "all_us_states.csv"
109
+ open("#{Rails.root}/db/#{filename}", 'w') do |f|
110
+ f.write(csv_string)
111
+ end
112
+ end
113
+
114
+ desc "Export all US Counties to a .csv file"
115
+ task :export_counties => :environment do
116
+ @counties = County.order("name ASC")
117
+ csv_string = CSV.generate do |csv|
118
+ csv << ["name", "state", "county_seat"]
119
+ @counties.each do |county|
120
+ csv << [
121
+ county.name,
122
+ county.state.abbr,
123
+ county.county_seat
124
+ ]
125
+ end
126
+ end
127
+ filename = "all_us_counties.csv"
128
+ open("#{Rails.root}/db/#{filename}", 'w') do |f|
129
+ f.write(csv_string)
130
+ end
131
+ end
132
+
133
+ desc "Export the zipcodes with county and state data"
134
+ task :export_zipcodes => :environment do
135
+ @zipcodes = Zipcode.order("code ASC")
136
+ csv_string = CSV.generate do |csv|
137
+ csv << ["code", "city", "state", "county", "area_code", "lat", "lon"]
138
+ @zipcodes.each do |zip|
139
+ csv << [
140
+ zip.code,
141
+ zip.city,
142
+ zip.state.abbr,
143
+ zip.county.nil? ? '' : zip.county.name,
144
+ zip.area_code,
145
+ zip.lat,
146
+ zip.lon
147
+ ]
148
+ end
149
+ end
150
+ filename = "all_us_zipcodes.csv"
151
+ open("#{Rails.root}/db/#{filename}", 'w') do |f|
152
+ f.write(csv_string)
153
+ end
154
+ end
155
+
156
+ desc "Export zipcodes, states and counties tables"
157
+ task :export => :environment do
158
+ Rake::Task['zipcodes:export_states'].invoke
159
+ Rake::Task['zipcodes:export_counties'].invoke
160
+ Rake::Task['zipcodes:export_zipcodes'].invoke
161
+ end
162
+
163
+ end
@@ -0,0 +1,3 @@
1
+ module UsZipcode
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,3 @@
1
+ module UsZipcode
2
+ VERSION = "0.1.4"
3
+ end
@@ -0,0 +1,34 @@
1
+ require 'rails/generators/actions'
2
+ require 'rails/generators/base'
3
+
4
+ module MyZipcodeGem
5
+ class Base < Rails::Generators::Base #:nodoc:
6
+ # def self.source_root
7
+ # @_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'nifty', generator_name, 'templates'))
8
+ # puts ">>> @_source_root: [#{@_source_root}]"
9
+ # @_source_root
10
+ # end
11
+
12
+ def self.banner
13
+ "rails generate my_zipcode_gem:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
14
+ end
15
+
16
+ private
17
+
18
+ def add_gem(name, options = {})
19
+ gemfile_content = File.read(destination_path("Gemfile"))
20
+ File.open(destination_path("Gemfile"), 'a') { |f| f.write("\n") } unless gemfile_content =~ /\n\Z/
21
+ gem name, options unless gemfile_content.include? name
22
+ end
23
+
24
+ def print_usage
25
+ self.class.help(Thor::Base.shell.new)
26
+ exit
27
+ end
28
+
29
+ def destination_path(path)
30
+ File.join(destination_root, path)
31
+ end
32
+
33
+ end
34
+ end
data/lib/us_zipcode.rb ADDED
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/actions'
2
+ require 'rails/generators/base'
3
+
4
+ module UsZipcode
5
+ class Base < Rails::Generators::Base #:nodoc:
6
+
7
+ def self.banner
8
+ "rails generate us_zipcode:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
9
+ end
10
+
11
+ private
12
+
13
+ def add_gem(name, options = {})
14
+ gemfile_content = File.read(destination_path("Gemfile"))
15
+ File.open(destination_path("Gemfile"), 'a') { |f| f.write("\n") } unless gemfile_content =~ /\n\Z/
16
+ gem name, options unless gemfile_content.include? name
17
+ end
18
+
19
+ def print_usage
20
+ self.class.help(Thor::Base.shell.new)
21
+ exit
22
+ end
23
+
24
+ def destination_path(path)
25
+ File.join(destination_root, path)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,29 @@
1
+ require 'rails/generators/actions'
2
+ require 'rails/generators/base'
3
+
4
+ module UsZipcode
5
+ class Base < Rails::Generators::Base #:nodoc:
6
+
7
+ def self.banner
8
+ "rails generate my_zipcode_gem:#{generator_name} #{self.arguments.map{ |a| a.usage }.join(' ')} [options]"
9
+ end
10
+
11
+ private
12
+
13
+ def add_gem(name, options = {})
14
+ gemfile_content = File.read(destination_path("Gemfile"))
15
+ File.open(destination_path("Gemfile"), 'a') { |f| f.write("\n") } unless gemfile_content =~ /\n\Z/
16
+ gem name, options unless gemfile_content.include? name
17
+ end
18
+
19
+ def print_usage
20
+ self.class.help(Thor::Base.shell.new)
21
+ exit
22
+ end
23
+
24
+ def destination_path(path)
25
+ File.join(destination_root, path)
26
+ end
27
+
28
+ end
29
+ end
data/rails3_2.gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rails', '~> 3.2.0'
5
+
6
+ # jquery-rails is used by the dummy application
7
+ gem 'jquery-rails'
8
+ gem 'sqlite3'
9
+ gem 'mysql2'
10
+ gem 'pg'
11
+
12
+ group :test do
13
+ gem 'capybara', '~> 2.0.2'
14
+ gem 'guard-rspec'
15
+ gem 'guard-spork'
16
+ gem 'shoulda', '~> 3.5.0'
17
+ gem 'rspec'
18
+ gem 'rspec-rails', '~> 2.12.0'
19
+ gem 'cucumber'
20
+ gem 'cucumber-rails', '~> 1.3.0', require: false
21
+ end
data/rails4_0.gemfile ADDED
@@ -0,0 +1,23 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rails', '~> 4.0.0'
5
+
6
+ gem 'protected_attributes'
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem 'jquery-rails'
10
+ gem 'sqlite3'
11
+ gem 'mysql2'
12
+ gem 'pg'
13
+
14
+ group :test do
15
+ gem 'capybara', '~> 2.0.2'
16
+ gem 'guard-rspec'
17
+ gem 'guard-spork'
18
+ gem 'shoulda', '~> 3.5.0'
19
+ gem 'rspec'
20
+ gem 'rspec-rails', '~> 2.12.0'
21
+ gem 'cucumber'
22
+ gem 'cucumber-rails', '~> 1.3.0', require: false
23
+ end
data/rails4_1.gemfile ADDED
@@ -0,0 +1,23 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem 'rails', '~> 4.1.0'
5
+
6
+ gem 'protected_attributes'
7
+
8
+ # jquery-rails is used by the dummy application
9
+ gem 'jquery-rails'
10
+ gem 'sqlite3'
11
+ gem 'mysql2'
12
+ gem 'pg'
13
+
14
+ group :test do
15
+ gem 'capybara', '~> 2.0.2'
16
+ gem 'guard-rspec'
17
+ gem 'guard-spork'
18
+ gem 'shoulda', '~> 3.5.0'
19
+ gem 'rspec'
20
+ gem 'rspec-rails', '~> 2.12.0'
21
+ gem 'cucumber'
22
+ gem 'cucumber-rails', '~> 1.3.0', require: false
23
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "generators/us_zipcode/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "us_zipcode"
7
+ s.version = UsZipcode::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Chakreshwar"]
10
+ s.email = ["chakreshwar.sharma@yahoo.com"]
11
+ s.summary = %q{A Ruby gem to handle all things zipcode.}
12
+ s.description = %q{A Ruby gem for looking up and manipulating US postal codes and geocodes.}
13
+ s.licenses = ['MIT']
14
+
15
+ s.rubyforge_project = "us_zipcode"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('rails', '>= 3.0.0')
23
+ s.add_dependency('memoist', '~> 0.11.0')
24
+ end
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "generators/my_zipcode_gem/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "us_zipcode"
7
+ s.version = UsZipcode::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Chakreshwar"]
10
+ s.email = ["chakreshwar.sharma@yahoo.com"]
11
+ s.summary = %q{A Ruby gem to handle all things zipcode.}
12
+ s.description = %q{A Ruby gem for looking up and manipulating US postal codes and geocodes.}
13
+ s.licenses = ['MIT']
14
+
15
+ s.rubyforge_project = "us_zipcode"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency('rails', '>= 3.0.0')
23
+ s.add_dependency('memoist', '~> 0.11.0')
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: us_zipcode
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Chakreshwar
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 3.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 3.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: memoist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.11.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.11.0
41
+ description: A Ruby gem for looking up and manipulating US postal codes and geocodes.
42
+ email:
43
+ - chakreshwar.sharma@yahoo.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - MIT-LICENSE
52
+ - README.textile
53
+ - README.textile~
54
+ - Rakefile
55
+ - features/step_definitions/common_steps.rb
56
+ - features/step_definitions/rails_setup_steps.rb
57
+ - features/step_definitions/rails_setup_steps.rb~
58
+ - features/support/env.rb
59
+ - features/support/string.rb
60
+ - features/zipcodes.feature
61
+ - features/zipcodes.feature~
62
+ - lib/generators/us_zipcode/models_generator.rb
63
+ - lib/generators/us_zipcode/models_generator.rb~
64
+ - lib/generators/us_zipcode/templates/county_model.rb
65
+ - lib/generators/us_zipcode/templates/county_model.rb~
66
+ - lib/generators/us_zipcode/templates/migration.rb
67
+ - lib/generators/us_zipcode/templates/migration.rb~
68
+ - lib/generators/us_zipcode/templates/state_model.rb
69
+ - lib/generators/us_zipcode/templates/state_model.rb~
70
+ - lib/generators/us_zipcode/templates/zipcode_model.rb
71
+ - lib/generators/us_zipcode/templates/zipcode_model.rb~
72
+ - lib/generators/us_zipcode/templates/zipcodes.rake
73
+ - lib/generators/us_zipcode/templates/zipcodes.rake~
74
+ - lib/generators/us_zipcode/version.rb
75
+ - lib/generators/us_zipcode/version.rb~
76
+ - lib/my_zipcode_gem.rb~
77
+ - lib/us_zipcode.rb
78
+ - lib/us_zipcode.rb~
79
+ - rails3_2.gemfile
80
+ - rails4_0.gemfile
81
+ - rails4_1.gemfile
82
+ - us_zipcode.gemspec
83
+ - us_zipcode.gemspec~
84
+ homepage:
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project: us_zipcode
104
+ rubygems_version: 2.5.1
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: A Ruby gem to handle all things zipcode.
108
+ test_files: []