zipcode-fr 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/zipcode-fr.rb +47 -29
- metadata +12 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 25ae394ec05abe31e0c569e482c0921974d5eec9
|
4
|
+
data.tar.gz: 242efa64b48bb3fce200e47ece0251b40b1c9d70
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b866e37bc86951e3d35f8fe475d7d8bf5a0b639a14d1c58ca94739fff53e5c9aabc6813c39f5322173e7fa9e530cb68507ad3cb318609e977b059fa15ed5b171
|
7
|
+
data.tar.gz: 1c753016728b3f915660e1cea0ae94bb0943d8efbd170adcb27c9b48273a09792889a0a1999604be7ad135a49bc169226ed8938e2729d5beb70fb120f7da5abf
|
data/lib/zipcode-fr.rb
CHANGED
@@ -20,45 +20,52 @@ module ZipCode
|
|
20
20
|
@loaded
|
21
21
|
end
|
22
22
|
|
23
|
-
|
23
|
+
def data_source
|
24
24
|
path = 'vendor/data/code_postaux_v201410.csv'
|
25
25
|
File.expand_path(File.join(File.dirname(__FILE__), '..', path))
|
26
26
|
end
|
27
|
+
private :data_source
|
27
28
|
|
28
|
-
|
29
|
+
def reader_options
|
29
30
|
{
|
30
31
|
col_sep: ';',
|
31
32
|
encoding: 'ISO-8859-1',
|
32
33
|
}
|
33
34
|
end
|
35
|
+
private :reader_options
|
34
36
|
|
35
|
-
|
37
|
+
def open
|
36
38
|
CSV.open(data_source, 'rb', reader_options) do |csv|
|
37
39
|
csv.take(1) # skip header manually to preserve tell()
|
38
40
|
yield csv
|
39
41
|
end
|
40
42
|
end
|
43
|
+
private :open
|
41
44
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
45
|
+
def reader
|
46
|
+
return enum_for(:reader) unless block_given?
|
47
|
+
|
48
|
+
open do |io|
|
49
|
+
pos = io.tell
|
50
|
+
io.each { |row| yield(pos, clean(row)); pos = io.tell }
|
48
51
|
end
|
49
52
|
end
|
53
|
+
private :reader
|
50
54
|
|
51
|
-
|
55
|
+
def clean(row)
|
52
56
|
row_to_h(row_clean(row))
|
53
57
|
end
|
58
|
+
private :clean
|
54
59
|
|
55
|
-
|
60
|
+
def row_clean(row)
|
56
61
|
row.map { |e| e.strip.encode('UTF-8') }
|
57
62
|
end
|
63
|
+
private :row_clean
|
58
64
|
|
59
|
-
|
65
|
+
def row_to_h(row)
|
60
66
|
[:insee, :name, :zip, :alt_name].zip(row).to_h
|
61
67
|
end
|
68
|
+
private :row_to_h
|
62
69
|
|
63
70
|
def index!(name, data, modes = nil, key: nil)
|
64
71
|
key ||= name
|
@@ -78,7 +85,7 @@ module ZipCode
|
|
78
85
|
# TODO: create an appender registry
|
79
86
|
# rubocop:disable Metrics/AbcSize
|
80
87
|
# rubocop:disable Metrics/MethodLength
|
81
|
-
|
88
|
+
def appender(idx, key, mode)
|
82
89
|
case mode
|
83
90
|
when :prefix
|
84
91
|
-> (pos, record) { append_prefixes(idx, pos, record[key]) }
|
@@ -92,52 +99,62 @@ module ZipCode
|
|
92
99
|
-> (pos, record) { append_match(idx, pos, record[key]) }
|
93
100
|
end
|
94
101
|
end
|
102
|
+
private :appender
|
95
103
|
|
96
|
-
|
104
|
+
def append_match(idx, pos, val)
|
97
105
|
idx[val.hash] << pos
|
98
106
|
end
|
107
|
+
private :append_match
|
99
108
|
|
100
|
-
|
109
|
+
def append_words(idx, pos, val)
|
101
110
|
each_word(val) { |w| idx[w.hash] << pos }
|
102
111
|
end
|
112
|
+
private :append_words
|
103
113
|
|
104
|
-
|
114
|
+
def append_word_prefixes(idx, pos, val)
|
105
115
|
each_word(val) do |word|
|
106
116
|
each_prefix(word) { |prefix| idx[prefix.hash] << pos }
|
107
117
|
end
|
108
118
|
end
|
119
|
+
private :append_word_prefixes
|
109
120
|
|
110
|
-
|
121
|
+
def append_prefixes(idx, pos, val, min_size: 1)
|
111
122
|
each_prefix(val, min_size: min_size) { |prefix| idx[prefix.hash] << pos }
|
112
123
|
end
|
124
|
+
private :append_prefixes
|
113
125
|
|
114
|
-
|
126
|
+
def each_word(val, &block)
|
115
127
|
val.split.each(&block)
|
116
128
|
end
|
129
|
+
private :each_word
|
117
130
|
|
118
|
-
|
131
|
+
def each_prefix(val, min_size: 1)
|
119
132
|
min_size.upto(val.length) { |i| yield val[0...i] }
|
120
133
|
end
|
134
|
+
private :each_prefix
|
121
135
|
|
122
|
-
|
136
|
+
def each_suffix(val, min_size: 1)
|
123
137
|
min_size.upto(val.length) { |i| yield val[-i..-1] }
|
124
138
|
end
|
139
|
+
private :each_suffix
|
125
140
|
|
126
|
-
|
141
|
+
def append_infixes(idx, pos, val, min_size: 1)
|
127
142
|
each_prefix(val, min_size: min_size) do |prefix|
|
128
143
|
each_suffix(prefix, min_size: min_size) do |infix|
|
129
144
|
idx[infix.hash] << pos
|
130
145
|
end
|
131
146
|
end
|
132
147
|
end
|
148
|
+
private :append_infixes
|
133
149
|
|
134
|
-
|
150
|
+
def index(name)
|
135
151
|
if @indexes.key?(name)
|
136
152
|
@indexes[name]
|
137
153
|
else
|
138
154
|
fail "no index named #{name.inspect}"
|
139
155
|
end
|
140
156
|
end
|
157
|
+
private :index
|
141
158
|
|
142
159
|
def memsize_of_index(name)
|
143
160
|
require 'objspace'
|
@@ -145,16 +162,17 @@ module ZipCode
|
|
145
162
|
@indexes[name].reduce(0) { |a, (_, v)| a + ObjectSpace.memsize_of(v) }
|
146
163
|
end
|
147
164
|
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
165
|
+
def read_at(*positions, count: 1)
|
166
|
+
return enum_for(:read_at, *positions, count: count) unless block_given?
|
167
|
+
|
168
|
+
open do |io|
|
169
|
+
positions.each do |pos|
|
170
|
+
io.seek(pos)
|
171
|
+
io.take(count).each { |row| yield clean(row) }
|
155
172
|
end
|
156
173
|
end
|
157
174
|
end
|
175
|
+
private :read_at
|
158
176
|
|
159
177
|
def search(name, str, case_insensitive: true)
|
160
178
|
str = str.upcase if case_insensitive
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zipcode-fr
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Loic Nageleisen
|
@@ -12,62 +12,62 @@ date: 2015-08-03 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pry
|
15
|
-
|
15
|
+
version_requirements: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
|
-
|
22
|
+
requirement: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rubocop
|
29
|
-
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
|
-
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rake
|
43
|
-
|
43
|
+
version_requirements: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
47
|
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
|
-
|
50
|
+
requirement: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: minitest
|
57
|
-
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
|
-
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '0'
|
69
|
-
description:
|
70
|
-
|
69
|
+
description: " Query city information by zip code and city name, indexed by word
|
70
|
+
prefixes.\n"
|
71
71
|
email: loic.nageleisen@gmail.com
|
72
72
|
executables: []
|
73
73
|
extensions: []
|
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
95
95
|
version: '0'
|
96
96
|
requirements: []
|
97
97
|
rubyforge_project:
|
98
|
-
rubygems_version: 2.4.
|
98
|
+
rubygems_version: 2.4.8
|
99
99
|
signing_key:
|
100
100
|
specification_version: 4
|
101
101
|
summary: French zip codes and cities
|