zipcoder 0.9.1 → 0.9.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -4
- data/lib/zipcoder.rb +14 -3
- data/lib/zipcoder/cacher/base.rb +6 -0
- data/lib/zipcoder/version.rb +1 -1
- data/spec/cacher_memory_spec.rb +16 -4
- data/spec/cacher_redis_spec.rb +12 -1
- data/spec/zipcoder_spec.rb +7 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eac9f2d3af269299c92366bff9bcecdf96bfec2c72b54d00ceb808e6008e0d53
|
4
|
+
data.tar.gz: 01d94a09280c77d36ca2b13078de68677064cbc6c427f5b75545b47e92411160
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7084550081121cf6ca274731d3917c3c395ac2b3fb24be2c24a9d5189e02eb02fc6c1d0f728f0efb53bd8a9db6ae74f572a948932635fce8f7bb45bceea26aef
|
7
|
+
data.tar.gz: dd5512b67ad72dbed6f4166246546357272f4f2e2001668338e1b8301b4030f75e0b3e9c9bc2145ed10646ee2be1b3423a2da99efb3ca42a550646290d70e6ba
|
data/README.md
CHANGED
@@ -7,6 +7,8 @@ Gem for performing zip code lookup operations
|
|
7
7
|
|
8
8
|
## Revision History
|
9
9
|
|
10
|
+
- v0.9.2:
|
11
|
+
- added config class
|
10
12
|
- v0.9.1:
|
11
13
|
- added ability to use a different data source
|
12
14
|
- added support for data from unitestateszipcodes.org"
|
@@ -120,8 +122,11 @@ version. I had to use v3.3.5 to test.*
|
|
120
122
|
require 'zipcoder'
|
121
123
|
require 'zipcoder/cacher/redis'
|
122
124
|
|
123
|
-
|
124
|
-
Zipcoder.
|
125
|
+
Zipcoder.config do |config|
|
126
|
+
config.cacher = Zipcoder::Cacher::Redis.new(**args)
|
127
|
+
end
|
128
|
+
|
129
|
+
Zipcoder.load_cache
|
125
130
|
```
|
126
131
|
|
127
132
|
Please visit [Redis Github](https://github.com/redis/redis-rb) for the different options
|
@@ -140,7 +145,11 @@ To override the default, you can create a new YAML file with the same format as
|
|
140
145
|
```ruby
|
141
146
|
require 'zipcoder'
|
142
147
|
|
143
|
-
Zipcoder.
|
148
|
+
Zipcoder.config do |config|
|
149
|
+
config.data = "path_to_data/data.yml"
|
150
|
+
end
|
151
|
+
|
152
|
+
Zipcoder.load_cache
|
144
153
|
```
|
145
154
|
|
146
155
|
##### unitestateszipcodes.org
|
@@ -333,7 +342,7 @@ puts "TX".state_cities names_only: true
|
|
333
342
|
|
334
343
|
#### Method: Zipcoder.state_counties(state, **args)
|
335
344
|
|
336
|
-
This will return the
|
345
|
+
This will return the counties in a state
|
337
346
|
|
338
347
|
**variations:**
|
339
348
|
|
data/lib/zipcoder.rb
CHANGED
@@ -10,6 +10,17 @@ module Zipcoder
|
|
10
10
|
class ZipcoderError < Exception
|
11
11
|
end
|
12
12
|
|
13
|
+
class Config
|
14
|
+
attr_accessor :cacher
|
15
|
+
attr_accessor :data
|
16
|
+
end
|
17
|
+
|
18
|
+
CONFIG = Config.new
|
19
|
+
|
20
|
+
def self.config(&block)
|
21
|
+
block.call(CONFIG)
|
22
|
+
end
|
23
|
+
|
13
24
|
@@cacher = nil
|
14
25
|
def self.cacher
|
15
26
|
if @@cacher == nil
|
@@ -19,9 +30,9 @@ module Zipcoder
|
|
19
30
|
end
|
20
31
|
|
21
32
|
# Loads the data into memory
|
22
|
-
def self.load_cache
|
23
|
-
@@cacher = cacher || Cacher::Memory.new
|
24
|
-
self.cacher.load data:data
|
33
|
+
def self.load_cache
|
34
|
+
@@cacher = CONFIG.cacher || Cacher::Memory.new
|
35
|
+
self.cacher.load data: CONFIG.data
|
25
36
|
end
|
26
37
|
|
27
38
|
# Looks up zip code information
|
data/lib/zipcoder/cacher/base.rb
CHANGED
@@ -58,6 +58,7 @@ List of the states in the US
|
|
58
58
|
module Zipcoder
|
59
59
|
module Cacher
|
60
60
|
class Base
|
61
|
+
attr_accessor :loaded
|
61
62
|
|
62
63
|
KEY_BASE = "zipcoder"
|
63
64
|
KEY_ZIP = "#{KEY_BASE}:zip"
|
@@ -92,9 +93,12 @@ module Zipcoder
|
|
92
93
|
|
93
94
|
def initialize(**kwargs)
|
94
95
|
self._init_cache **kwargs
|
96
|
+
self.loaded = false
|
95
97
|
end
|
96
98
|
|
97
99
|
def load(data: nil)
|
100
|
+
return if self.loaded
|
101
|
+
|
98
102
|
start_time = Time.now
|
99
103
|
|
100
104
|
# Load zip cache from file
|
@@ -166,6 +170,8 @@ module Zipcoder
|
|
166
170
|
|
167
171
|
# Print the alpsed time
|
168
172
|
puts "ZipCoder initialization time: #{Time.now-start_time}"
|
173
|
+
|
174
|
+
self.loaded = true
|
169
175
|
end
|
170
176
|
|
171
177
|
def read_zip_cache(zip)
|
data/lib/zipcoder/version.rb
CHANGED
data/spec/cacher_memory_spec.rb
CHANGED
@@ -3,12 +3,24 @@ require "zipcoder/cacher/memory"
|
|
3
3
|
|
4
4
|
describe Zipcoder::Cacher::Memory do
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
# Load the new data
|
6
|
+
before(:all) {
|
9
7
|
new_data = "#{File.dirname(__FILE__)}/fixtures/files/temp_data.yml"
|
10
|
-
Zipcoder.load_cache data: new_data
|
11
8
|
|
9
|
+
Zipcoder.config do |config|
|
10
|
+
config.data = new_data
|
11
|
+
end
|
12
|
+
|
13
|
+
Zipcoder.load_cache
|
14
|
+
}
|
15
|
+
|
16
|
+
after(:all) {
|
17
|
+
Zipcoder.config do |config|
|
18
|
+
config.data = nil
|
19
|
+
config.cacher = nil
|
20
|
+
end
|
21
|
+
}
|
22
|
+
|
23
|
+
it "allows the default data file to be overridden" do
|
12
24
|
# Check that it was loaded
|
13
25
|
expect(Zipcoder.states.count).to eq(1)
|
14
26
|
expect("PR".state_cities.count).to eq(3)
|
data/spec/cacher_redis_spec.rb
CHANGED
@@ -10,12 +10,23 @@ describe Zipcoder::Cacher::Redis do
|
|
10
10
|
allow(Zipcoder::Cacher::Redis).to receive(:_create_redis_client) do
|
11
11
|
RedisStub.new
|
12
12
|
end
|
13
|
-
Zipcoder.
|
13
|
+
Zipcoder.config do |config|
|
14
|
+
config.cacher = Zipcoder::Cacher::Redis.new
|
15
|
+
end
|
16
|
+
|
17
|
+
Zipcoder.load_cache
|
14
18
|
end
|
15
19
|
|
16
20
|
stub_redis_once = true
|
17
21
|
end
|
18
22
|
|
23
|
+
after(:each) {
|
24
|
+
Zipcoder.config do |config|
|
25
|
+
config.data = nil
|
26
|
+
config.cacher = nil
|
27
|
+
end
|
28
|
+
}
|
29
|
+
|
19
30
|
describe "#zip_info" do
|
20
31
|
it "match" do
|
21
32
|
info = "78748".zip_info
|
data/spec/zipcoder_spec.rb
CHANGED