postal-codes-search 1.0.1 → 1.1
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.
- checksums.yaml +4 -4
- data/README.md +35 -3
- data/lib/postal-codes-search.rb +11 -7
- 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: 6bd751b4eee9200e347095667456ba49196eff2fac841c621a5c284624ab9d57
|
|
4
|
+
data.tar.gz: 456bd07c8efeb450690454c0ccc82eba0ca280e9f370f82e7061aa4f7c00f359
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3a3e76304a6008031bb4861d2454804b30e6172a1cbea59ea94b179805ef28016046a16e6dd5cd93bc47b168649e3c4d8435883d048b4d89aebf45a49b93716f
|
|
7
|
+
data.tar.gz: 16d1a3eff69ae8110c41d219fd65f4f39b2143858c5ee9dc1b55965bc3156a09e5414aae48abf6844d441e719e4bd02d436df37d9be503734a85a41dcef86505
|
data/README.md
CHANGED
|
@@ -6,7 +6,7 @@ Gem to obtain postal codes for US and CA only.
|
|
|
6
6
|
|
|
7
7
|
Add this line to your application's Gemfile:
|
|
8
8
|
|
|
9
|
-
gem 'postal-codes-search'
|
|
9
|
+
gem 'postal-codes-search', '~> 1.0', '>= 1.0.1'
|
|
10
10
|
|
|
11
11
|
And then execute:
|
|
12
12
|
|
|
@@ -18,15 +18,47 @@ Or install it yourself as:
|
|
|
18
18
|
|
|
19
19
|
## Usage
|
|
20
20
|
|
|
21
|
+
You can search generally by any keyword like country, county, postal code, state and or city like this
|
|
21
22
|
```ruby
|
|
22
23
|
PostalCodesSearch.find('Y1A')
|
|
23
24
|
# => [{"country"=>"CA", "postal_code"=>"Y1A", "city"=>"Whitehorse", "state"=>"Yukon", "county"=>nil}]
|
|
24
|
-
#
|
|
25
|
+
# This will return an array. If you put an incomplete postal code, it will return the most similar results
|
|
26
|
+
```
|
|
27
|
+
You can use more specific search depending on the attribute you want to use
|
|
28
|
+
|
|
29
|
+
### By code
|
|
30
|
+
```ruby
|
|
31
|
+
PostalCodesSearch.find_by(code: 'Y1A')
|
|
32
|
+
# => [{"country"=>"CA", "postal_code"=>"Y1A", "city"=>"Whitehorse", "state"=>"Yukon", "county"=>nil}]
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### By country
|
|
36
|
+
```ruby
|
|
37
|
+
PostalCodesSearch.find_by(country: 'US').count
|
|
38
|
+
# => 41469
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
### By county
|
|
42
|
+
```ruby
|
|
43
|
+
PostalCodesSearch.find_by(county: 'Yukon-Koyukuk').first
|
|
44
|
+
# => {"country"=>"US", "postal_code"=>99558, "city"=>"Anvik", "state"=>"Alaska", "county"=>"Yukon-Koyukuk (CA)"}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
### By state
|
|
48
|
+
```ruby
|
|
49
|
+
PostalCodesSearch.find_by(state: 'Alaska').count
|
|
50
|
+
# => 273
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
### By city
|
|
54
|
+
```ruby
|
|
55
|
+
PostalCodesSearch.find_by(city: 'Venetie')
|
|
56
|
+
# => [{"country"=>"US", "postal_code"=>99781, "city"=>"Venetie", "state"=>"Alaska", "county"=>"Yukon-Koyukuk (CA)"}]
|
|
25
57
|
```
|
|
26
58
|
|
|
27
59
|
Using rails, you can load all the values in a initializer
|
|
28
60
|
```ruby
|
|
29
|
-
# config/initializers/
|
|
61
|
+
# config/initializers/postal_codes.rb
|
|
30
62
|
PostalCodesSearch.load unless Rails.env.development?
|
|
31
63
|
```
|
|
32
64
|
|
data/lib/postal-codes-search.rb
CHANGED
|
@@ -4,7 +4,7 @@ require 'yaml'
|
|
|
4
4
|
|
|
5
5
|
# Class module that handles all searching methods for postal codes
|
|
6
6
|
module PostalCodesSearch
|
|
7
|
-
VERSION = '1.
|
|
7
|
+
VERSION = '1.1'
|
|
8
8
|
|
|
9
9
|
class << self
|
|
10
10
|
SUPPORTED_COUNTRIES = %i[us ca].freeze
|
|
@@ -13,13 +13,17 @@ module PostalCodesSearch
|
|
|
13
13
|
!(attr.nil? || attr.empty?)
|
|
14
14
|
end
|
|
15
15
|
|
|
16
|
-
def
|
|
16
|
+
def selectable?(term, value)
|
|
17
|
+
attribute_present(term) && value && value.to_s.downcase.include?(term.to_s.downcase)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def find_by(code: nil, country: nil, county: nil, city: nil, state: nil)
|
|
17
21
|
data_source.select do |postal_code|
|
|
18
|
-
(
|
|
19
|
-
(
|
|
20
|
-
(
|
|
21
|
-
(
|
|
22
|
-
(
|
|
22
|
+
selectable?(code, postal_code['postal_code']) ||
|
|
23
|
+
selectable?(country, postal_code['country']) ||
|
|
24
|
+
selectable?(county, postal_code['county']) ||
|
|
25
|
+
selectable?(city, postal_code['city']) ||
|
|
26
|
+
selectable?(state, postal_code['state'])
|
|
23
27
|
end
|
|
24
28
|
end
|
|
25
29
|
|