zipcoder 0.1.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.
@@ -0,0 +1,8 @@
1
+ class Integer
2
+
3
+ def zip_info(**kwargs)
4
+ string = self.to_s.rjust(5, '0')
5
+ string.zip_info(**kwargs)
6
+ end
7
+
8
+ end
data/lib/ext/string.rb ADDED
@@ -0,0 +1,34 @@
1
+ require_relative "../zipcoder"
2
+
3
+ class String
4
+
5
+ def zip_info(**kwargs)
6
+ info = Zipcoder.zip_lookup[self]
7
+
8
+ # Filter to the included keys
9
+ if kwargs[:keys] != nil
10
+ new_info = {}
11
+ kwargs[:keys].each { |k| new_info[k] = info[k] }
12
+ info = new_info
13
+ end
14
+
15
+ info
16
+ end
17
+
18
+ def city_info(**kwargs)
19
+ # Cleanup "self"
20
+ key = self.delete(' ').upcase
21
+
22
+ info = Zipcoder.city_lookup[key]
23
+
24
+ # Filter to the included keys
25
+ if kwargs[:keys] != nil
26
+ new_info = {}
27
+ kwargs[:keys].each { |k| new_info[k] = info[k] }
28
+ info = new_info
29
+ end
30
+
31
+ info
32
+ end
33
+
34
+ end
@@ -0,0 +1,3 @@
1
+ module Zipcoder
2
+ VERSION = "0.1.0"
3
+ end
data/lib/zipcoder.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "zipcoder/version"
2
+ require "ext/string"
3
+ require "ext/integer"
4
+ require "yaml"
5
+
6
+ module Zipcoder
7
+
8
+ # Data Structure Load and Lookup
9
+ @@zip_lookup = nil
10
+ def self.zip_lookup
11
+ self.load_data if @@zip_lookup == nil
12
+ @@zip_lookup
13
+ end
14
+
15
+ @@city_lookup = nil
16
+ def self.city_lookup
17
+ self.load_data if @@city_lookup == nil
18
+ @@city_lookup
19
+ end
20
+
21
+ # Loads the data into memory
22
+ def self.load_data
23
+ this_dir = File.expand_path(File.dirname(__FILE__))
24
+
25
+ zip_lookup = File.join(this_dir, 'data', 'zip_lookup.yml')
26
+ @@zip_lookup = YAML.load(File.open(zip_lookup))
27
+
28
+ city_lookup = File.join(this_dir, 'data', 'city_lookup.yml')
29
+ @@city_lookup = YAML.load(File.open(city_lookup))
30
+ end
31
+
32
+ end
@@ -0,0 +1,11 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ if ENV['CODECOV_TOKEN']
5
+ require 'codecov'
6
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
7
+ end
8
+
9
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
10
+ require "zipcoder"
11
+
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe Zipcoder do
4
+ before(:all) {
5
+ described_class.load_data
6
+ }
7
+
8
+ it "has a version number" do
9
+ expect(Zipcoder::VERSION).not_to be nil
10
+ end
11
+
12
+ describe("String") do
13
+ describe "zip lookup" do
14
+ it "returns the info for a particular zip_code" do
15
+ zip_info = "78748".zip_info
16
+ expect(zip_info[:city]).to eq("AUSTIN")
17
+ expect(zip_info[:state]).to eq("TX")
18
+ expect(zip_info[:zip]).to eq("78748")
19
+ expect(zip_info[:lat]).to eq(30.26)
20
+ expect(zip_info[:long]).to eq(-97.74)
21
+ end
22
+
23
+ it "only returns specified keys" do
24
+ zip_info = "78748".zip_info(keys: [:city, :state])
25
+ expect(zip_info[:city]).to eq("AUSTIN")
26
+ expect(zip_info[:state]).to eq("TX")
27
+ expect(zip_info[:zip]).to be_nil
28
+ expect(zip_info[:lat]).to be_nil
29
+ expect(zip_info[:long]).to be_nil
30
+ end
31
+ end
32
+
33
+ describe "city lookup" do
34
+ it "returns the info for a particular city" do
35
+ city_info = "Austin, TX".city_info
36
+ expect(city_info[:city]).to eq("AUSTIN")
37
+ expect(city_info[:state]).to eq("TX")
38
+ expect(city_info[:zips].count).to eq(81)
39
+ expect(city_info[:lats].count).to eq(81)
40
+ expect(city_info[:longs].count).to eq(81)
41
+ end
42
+ it "only returns specified keys" do
43
+ city_info = "Austin, TX".city_info(keys: [:zips, :city])
44
+ expect(city_info[:city]).to eq("AUSTIN")
45
+ expect(city_info[:state]).to be_nil
46
+ expect(city_info[:zips].count).to eq(81)
47
+ expect(city_info[:lats]).to be_nil
48
+ expect(city_info[:longs]).to be_nil
49
+ end
50
+ end
51
+ end
52
+
53
+ describe("Integer") do
54
+ it "returns the info for a particular zip_code" do
55
+ zip_info = 78748.zip_info
56
+ expect(zip_info[:city]).to eq("AUSTIN")
57
+ expect(zip_info[:state]).to eq("TX")
58
+ expect(zip_info[:zip]).to eq("78748")
59
+ expect(zip_info[:lat]).to eq(30.26)
60
+ expect(zip_info[:long]).to eq(-97.74)
61
+ end
62
+
63
+ it "zero pads the zip code when using an integer" do
64
+ zip_info = 705.zip_info
65
+ expect(zip_info[:city]).to eq("AIBONITO")
66
+ end
67
+ end
68
+ end
data/zipcoder.gemspec ADDED
@@ -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 'zipcoder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "zipcoder"
8
+ spec.version = Zipcoder::VERSION
9
+ spec.authors = ["Eric Chapman"]
10
+ spec.email = ["eric.chappy@gmail.com"]
11
+
12
+ spec.summary = %q{Converts zip codes to cities, lat/long, and vice-versa}
13
+ #spec.description = %q{TODO: Write a longer description or delete this line.}
14
+ spec.homepage = 'https://github.com/ericchapman/zipcoder'
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
19
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '>= 1.7'
23
+ spec.add_development_dependency 'rake', '>= 10.0'
24
+ spec.add_development_dependency 'rspec', '>= 3.5.0'
25
+ spec.add_development_dependency 'simplecov'
26
+ spec.add_development_dependency 'codecov'
27
+ spec.required_ruby_version = '>= 2.0'
28
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zipcoder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Eric Chapman
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-03-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: 3.5.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: 3.5.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: codecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - eric.chappy@gmail.com
86
+ executables:
87
+ - console
88
+ - setup
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - .gitignore
93
+ - .rspec
94
+ - .ruby-version
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - circle.yml
102
+ - lib/data/city_lookup.yml
103
+ - lib/data/zip_lookup.yml
104
+ - lib/data/zipcode.csv
105
+ - lib/ext/integer.rb
106
+ - lib/ext/string.rb
107
+ - lib/zipcoder.rb
108
+ - lib/zipcoder/version.rb
109
+ - spec/spec_helper.rb
110
+ - spec/zipcoder_spec.rb
111
+ - zipcoder.gemspec
112
+ homepage: https://github.com/ericchapman/zipcoder
113
+ licenses:
114
+ - MIT
115
+ metadata: {}
116
+ post_install_message:
117
+ rdoc_options: []
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '2.0'
125
+ required_rubygems_version: !ruby/object:Gem::Requirement
126
+ requirements:
127
+ - - '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 2.4.8
133
+ signing_key:
134
+ specification_version: 4
135
+ summary: Converts zip codes to cities, lat/long, and vice-versa
136
+ test_files:
137
+ - spec/spec_helper.rb
138
+ - spec/zipcoder_spec.rb