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.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +35 -3
  3. data/lib/postal-codes-search.rb +11 -7
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9c255a854ed08b36121a42cd9a4030fcd3c7abd8b462c09184f7792ee77ac601
4
- data.tar.gz: 2c56351624b73fc227fabe6c422867f9797ea29ccb46d77838d6421fcd7c2f6f
3
+ metadata.gz: 6bd751b4eee9200e347095667456ba49196eff2fac841c621a5c284624ab9d57
4
+ data.tar.gz: 456bd07c8efeb450690454c0ccc82eba0ca280e9f370f82e7061aa4f7c00f359
5
5
  SHA512:
6
- metadata.gz: 139034cbcfda628349fa991bdb03e60eaa46bd2d2d7e4ad85335f356bb4b3d2cf42e1d428b4266e6182f767e6dd40abc7c2d82218c30e74c511cc6e7db234d7a
7
- data.tar.gz: eb6c0f08447e16601ebb8fa3399c380cb3b7ce211ce8ceeb721bea163525448419f1165218bed9235fc12bfc9f111d2eb67a45e3cd3ab2ed01e7b8fb9d2103ed
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
- # First run will take a while, as the yaml has to be loaded
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/load_zip_codes.rb
61
+ # config/initializers/postal_codes.rb
30
62
  PostalCodesSearch.load unless Rails.env.development?
31
63
  ```
32
64
 
@@ -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.0.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 find_by(code: nil, country: nil, county: nil, city: nil, state: nil)
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
- (attribute_present(code) && postal_code['postal_code'] && postal_code['postal_code'].to_s.include?(code)) ||
19
- (attribute_present(country) && postal_code['country'] && postal_code['country'].to_s.include?(country)) ||
20
- (attribute_present(county) && postal_code['county'] && postal_code['county'].to_s.include?(county)) ||
21
- (attribute_present(city) && postal_code['city'] && postal_code['city'].to_s.include?(city)) ||
22
- (attribute_present(state) && postal_code['state'] && postal_code['state'].to_s.include?(state))
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
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: postal-codes-search
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: '1.1'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jefferson Alvarez Alvarez