locality 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/locality/aregion.rb +7 -5
- data/lib/locality/ip.rb +1 -1
- data/lib/locality/postnummerservice/collection.rb +59 -0
- data/lib/locality/postnummerservice/entry.rb +4 -2
- data/lib/locality/postnummerservice.rb +8 -3
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 87a063187c95946e77731cd381e1e196949c0d35
|
4
|
+
data.tar.gz: d74e6d3a5e2b944300e0b544fcac90c545be590d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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.
|
data/lib/locality/aregion.rb
CHANGED
@@ -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
|
8
|
-
|
9
|
+
def code
|
10
|
+
raw_code.to_i
|
9
11
|
end
|
10
12
|
|
11
|
-
def
|
12
|
-
|
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
@@ -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
|
-
|
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.
|
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: []
|