locality 0.0.2 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 251fe2d5eeff7fd2bec5c9645a1c29158aa06475
4
- data.tar.gz: 4546e8b4eb59c63d5988b8be4666515c31b482d4
3
+ metadata.gz: 87a063187c95946e77731cd381e1e196949c0d35
4
+ data.tar.gz: d74e6d3a5e2b944300e0b544fcac90c545be590d
5
5
  SHA512:
6
- metadata.gz: fef7271ed467a49d229b06ca462ce365dc781a92bfa10507da721d54193083a8c0440dc0e4c70bf66d547f185f37b15191fac7594e8f3a7c76c6f906ea958e95
7
- data.tar.gz: 6d1d3e869708adf6a6221f7fe535a7ca4478bafb49b54c0c8a88ff8139e3147b7b305a878e69c1247769f55618cb840e68ce9a52ad55b2761cbaed6e3b978547
6
+ metadata.gz: fe91e20cf92950d82ea82692d0ed76aa89c44f368ae74ebf404ca5e0945f8ebd6dd9c8e4aa15eb4dfb2172a98044ef8f4dec3d54d6ecc58778a2bc61c5999b15
7
+ data.tar.gz: d02ca7ca25fd003eb0419c3d745e2674096bb3516e8f4a938139dfb60ae3f281aa6809a4c26866039ea2fea4c84ac7d0baae1e5b0fcccef1b15754ddec796983
data/README.md CHANGED
@@ -62,11 +62,13 @@ end
62
62
  ```
63
63
 
64
64
  ### Usage
65
+
66
+ ```ruby
65
67
  # If you want to make sure on application
66
68
  # bootup that everything works, call this method:
67
69
  Locality::IP.check!
68
70
 
69
- ```ruby
71
+ # Lookup an IP
70
72
  lookup = Locality::IP.new '198.51.100.55'
71
73
 
72
74
  # All these attributes return either a String or nil.
@@ -1,21 +1,23 @@
1
1
  require 'locality/postnummerservice'
2
+ require 'locality/postnummerservice/collection'
2
3
 
3
4
  module Locality
4
5
  # See https://sv.wikipedia.org/wiki/A-region
5
6
  class Aregion
7
+ include Postnummerservice::Collection
6
8
 
7
- def initialize(raw_code)
8
- @raw_code = raw_code
9
+ def code
10
+ raw_code.to_i
9
11
  end
10
12
 
11
- def zip_codes
12
- @zip_codes ||= backend.aregions[1].map(&:zip_code)
13
+ def state_name
14
+ state_names.first
13
15
  end
14
16
 
15
17
  private
16
18
 
17
19
  def backend
18
- Locality::Postnummerservice
20
+ Locality::Postnummerservice.aregions
19
21
  end
20
22
 
21
23
  end
data/lib/locality/ip.rb CHANGED
@@ -34,7 +34,7 @@ module Locality
34
34
  end
35
35
 
36
36
  def ip
37
- @ip ||= ::IPAddr.new(raw_ip)
37
+ @ip ||= ::IPAddr.new(raw_ip.to_s)
38
38
  rescue => exception
39
39
  nil
40
40
  end
@@ -0,0 +1,59 @@
1
+ require 'active_support/concern'
2
+
3
+ module Locality
4
+ module Postnummerservice
5
+ module Collection
6
+ extend ActiveSupport::Concern
7
+
8
+ def initialize(raw_code)
9
+ @raw_code = raw_code
10
+ end
11
+
12
+ # Convert raw_code to code
13
+ def code
14
+ fail NotImplementedError
15
+ end
16
+
17
+ def zip_codes
18
+ @zip_codes ||= entities.map(&:zip_code).uniq.sort
19
+ end
20
+
21
+ def city_names
22
+ @city_names ||= entities.map(&:city_name).uniq.sort
23
+ end
24
+
25
+ def state_codes
26
+ @state_codes ||= entities.map(&:state_code).uniq.sort
27
+ end
28
+
29
+ def state_names
30
+ @state_namess ||= entities.map(&:state_name).uniq.sort
31
+ end
32
+
33
+ def province_names
34
+ @province_names ||= entities.map(&:province_name).uniq.sort
35
+ end
36
+
37
+ def province_codes
38
+ @province_codes ||= entities.map(&:province_code).uniq.sort
39
+ end
40
+
41
+ def aregions
42
+ @aregions ||= entities.map(&:aregion).uniq.sort
43
+ end
44
+
45
+ private
46
+
47
+ attr_reader :raw_code
48
+
49
+ def entities
50
+ backend[code]
51
+ end
52
+
53
+ def backend
54
+ fail NotImplementedError
55
+ end
56
+
57
+ end
58
+ end
59
+ end
@@ -1,3 +1,5 @@
1
+ require 'active_support/core_ext/string'
2
+
1
3
  module Locality
2
4
  module Postnummerservice
3
5
  class Entry < Array
@@ -11,11 +13,11 @@ module Locality
11
13
  end
12
14
 
13
15
  def state_code
14
- self[2]
16
+ self[2].to_i
15
17
  end
16
18
 
17
19
  def state_name
18
- self[3]
20
+ self[3].humanize
19
21
  end
20
22
 
21
23
  def province_name
@@ -22,9 +22,7 @@ module Locality
22
22
  def self.load!
23
23
  @aregions = {}
24
24
 
25
- ::CSV.foreach(::Locality.config.postnummerfilen_path, encoding: encoding) do |row|
26
- next if row.blank?
27
- entry = Entry.new(row)
25
+ entries do |entry|
28
26
  @aregions[entry.aregion] ||= []
29
27
  @aregions[entry.aregion] << entry
30
28
  end
@@ -32,6 +30,13 @@ module Locality
32
30
  @loaded = true
33
31
  end
34
32
 
33
+ def self.entries(&block)
34
+ ::CSV.foreach(::Locality.config.postnummerfilen_path, encoding: encoding) do |row|
35
+ next if row.blank?
36
+ yield Entry.new(row)
37
+ end
38
+ end
39
+
35
40
  def self.encoding
36
41
  'ISO-8859-1:UTF-8' # from:to
37
42
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locality
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - bukowskis
@@ -177,6 +177,7 @@ files:
177
177
  - lib/locality/configuration.rb
178
178
  - lib/locality/ip.rb
179
179
  - lib/locality/postnummerservice.rb
180
+ - lib/locality/postnummerservice/collection.rb
180
181
  - lib/locality/postnummerservice/entry.rb
181
182
  homepage: https://github.com/bukowskis/locality
182
183
  licenses: []