fias 0.0.2 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (69) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +15 -22
  3. data/.rubocop.yml +7 -0
  4. data/.travis.yml +10 -0
  5. data/Gemfile +1 -1
  6. data/LICENSE.txt +2 -2
  7. data/README.md +259 -155
  8. data/Rakefile +6 -1
  9. data/config/names.txt +0 -0
  10. data/config/synonyms.yml +50 -0
  11. data/examples/create.rb +106 -0
  12. data/examples/generate_index.rb +63 -0
  13. data/fias.gemspec +33 -21
  14. data/lib/fias.rb +197 -10
  15. data/lib/fias/config.rb +74 -0
  16. data/lib/fias/import/copy.rb +62 -0
  17. data/lib/fias/import/dbf.rb +81 -0
  18. data/lib/fias/import/download_service.rb +37 -0
  19. data/lib/fias/import/restore_parent_id.rb +51 -0
  20. data/lib/fias/import/tables.rb +74 -0
  21. data/lib/fias/name/append.rb +30 -0
  22. data/lib/fias/name/canonical.rb +42 -0
  23. data/lib/fias/name/extract.rb +85 -0
  24. data/lib/fias/name/house_number.rb +71 -0
  25. data/lib/fias/name/split.rb +60 -0
  26. data/lib/fias/name/synonyms.rb +93 -0
  27. data/lib/fias/query.rb +43 -0
  28. data/lib/fias/query/estimate.rb +67 -0
  29. data/lib/fias/query/finder.rb +75 -0
  30. data/lib/fias/query/params.rb +101 -0
  31. data/lib/fias/railtie.rb +3 -17
  32. data/lib/fias/version.rb +1 -1
  33. data/spec/fixtures/ACTSTAT.DBF +0 -0
  34. data/spec/fixtures/NORDOC99.DBF +0 -0
  35. data/spec/fixtures/STRSTAT.DBF +0 -0
  36. data/spec/fixtures/addressing.yml +93 -0
  37. data/spec/fixtures/query.yml +79 -0
  38. data/spec/fixtures/query_sanitization.yml +75 -0
  39. data/spec/fixtures/status_append.yml +60 -0
  40. data/spec/lib/import/copy_spec.rb +44 -0
  41. data/spec/lib/import/dbf_spec.rb +28 -0
  42. data/spec/lib/import/download_service_spec.rb +15 -0
  43. data/spec/lib/import/restore_parent_id_spec.rb +34 -0
  44. data/spec/lib/import/tables_spec.rb +26 -0
  45. data/spec/lib/name/append_spec.rb +14 -0
  46. data/spec/lib/name/canonical_spec.rb +20 -0
  47. data/spec/lib/name/extract_spec.rb +67 -0
  48. data/spec/lib/name/house_number_spec.rb +45 -0
  49. data/spec/lib/name/query_spec.rb +21 -0
  50. data/spec/lib/name/split_spec.rb +15 -0
  51. data/spec/lib/name/synonyms_spec.rb +51 -0
  52. data/spec/lib/query/params_spec.rb +15 -0
  53. data/spec/lib/query_spec.rb +27 -0
  54. data/spec/spec_helper.rb +30 -0
  55. data/spec/support/db.rb +30 -0
  56. data/spec/support/query.rb +13 -0
  57. data/tasks/db.rake +52 -0
  58. data/tasks/download.rake +15 -0
  59. metadata +246 -64
  60. data/lib/fias/active_record/address_object.rb +0 -231
  61. data/lib/fias/active_record/address_object_type.rb +0 -15
  62. data/lib/fias/dbf_wrapper.rb +0 -90
  63. data/lib/fias/importer.rb +0 -30
  64. data/lib/fias/importer/base.rb +0 -59
  65. data/lib/fias/importer/pg.rb +0 -81
  66. data/lib/fias/importer/sqlite.rb +0 -38
  67. data/lib/generators/fias/migration.rb +0 -34
  68. data/lib/generators/fias/templates/create_fias_tables.rb +0 -5
  69. data/tasks/fias.rake +0 -68
@@ -0,0 +1,30 @@
1
+ DB = {}
2
+ SHORTCUTS = {}
3
+
4
+ YAML.load_file('spec/fixtures/addressing.yml').each.with_index do |item, index|
5
+ ancestry = item.inject([]) do |ancestry, element|
6
+ name = element.last
7
+ name, _, abbr, _ = Fias::Name::Extract.extract(name)
8
+ tokens = Fias::Name::Synonyms.tokens(name)
9
+ forms = Fias::Name::Synonyms.forms(name)
10
+ id = DB.size + 1
11
+
12
+ DB[id] = {
13
+ id: id,
14
+ parent_id: ancestry.last,
15
+ name: name,
16
+ abbr: abbr,
17
+ ancestry: ancestry.reverse,
18
+ tokens: tokens,
19
+ forms: forms
20
+ }
21
+
22
+ ancestry + [id]
23
+ end
24
+
25
+ SHORTCUTS[index] = DB[ancestry.last]
26
+ end
27
+
28
+ def find_in_addressing_db(tokens)
29
+ DB.values.find_all { |record| (record[:tokens] & tokens).size > 0 }.dup
30
+ end
@@ -0,0 +1,13 @@
1
+ class UndefinedQuery
2
+ include Fias::Query
3
+ end
4
+
5
+ class TestQuery
6
+ include Fias::Query
7
+
8
+ def find(tokens)
9
+ return [] if tokens.empty?
10
+
11
+ find_in_addressing_db(tokens)
12
+ end
13
+ end
@@ -0,0 +1,52 @@
1
+ require 'fias'
2
+ require 'ruby-progressbar'
3
+ require 'sequel'
4
+
5
+ namespace :fias do
6
+ desc 'Create FIAS tables (PREFIX, FIAS_PATH to dbfs, DATABASE_URL and TABLES)'
7
+ task :create_tables do
8
+ within_connection do |tables|
9
+ tables.create
10
+ puts "#{tables.files.keys.join(', ')} created."
11
+ end
12
+ end
13
+
14
+ desc 'Import FIAS data (PREFIX, FIAS_PATH to dbfs, DATABASE_URL and TABLES)'
15
+ task :import do
16
+ within_connection do |tables|
17
+ tables.copy.each do |table|
18
+ puts "Encoding #{table.table_name}..."
19
+ bar = ProgressBar.create(
20
+ total: table.dbf.record_count,
21
+ format: '%a |%B| [%E] (%c/%C) %p%%'
22
+ )
23
+
24
+ table.encode { bar.increment }
25
+ table.copy
26
+ end
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def connect_db
33
+ if ENV['DATABASE_URL'].blank?
34
+ fail 'Specify DATABASE_URL (eg. postgres://localhost/fias)'
35
+ end
36
+
37
+ Sequel.connect(ENV['DATABASE_URL'])
38
+ end
39
+
40
+ def within_connection(&block)
41
+ db = Sequel.connect(ENV['DATABASE_URL'])
42
+ fias_path = ENV['FIAS_PATH'] || 'tmp/fias'
43
+ only = *ENV['TABLES'].to_s.split(',')
44
+ files = Fias::Import::Dbf.new(fias_path).only(*only)
45
+ tables = Fias::Import::Tables.new(db, files)
46
+
47
+ diff = only - files.keys.map(&:to_s)
48
+ puts "WARNING: Missing DBF files for: #{diff.join(', ')}" if diff.any?
49
+
50
+ yield(tables)
51
+ end
52
+ end
@@ -0,0 +1,15 @@
1
+ require 'fias'
2
+
3
+ namespace :fias do
4
+ desc 'Returns lastest url of FIAS db (bundle exec rake fias:download | xargs wget)'
5
+ task :download do
6
+ url = Fias::Import::DownloadService.url
7
+
8
+ if url
9
+ puts url
10
+ else
11
+ puts 'An error occured during SOAP call'
12
+ return 999
13
+ end
14
+ end
15
+ end
metadata CHANGED
@@ -1,182 +1,364 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fias
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
5
- prerelease:
4
+ version: 1.0.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Victor Sokolov
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-01-23 00:00:00.000000000 Z
11
+ date: 2015-04-03 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: dbf
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  - !ruby/object:Gem::Dependency
31
- name: rake
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '3'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sequel
32
43
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
44
  requirements:
35
- - - ! '>='
45
+ - - ">="
36
46
  - !ruby/object:Gem::Version
37
47
  version: '0'
38
48
  type: :runtime
39
49
  prerelease: false
40
50
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
51
  requirements:
43
- - - ! '>='
52
+ - - ">="
44
53
  - !ruby/object:Gem::Version
45
54
  version: '0'
46
55
  - !ruby/object:Gem::Dependency
47
- name: activerecord
56
+ name: pg_data_encoder
48
57
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
58
  requirements:
51
- - - ! '>'
59
+ - - ">="
52
60
  - !ruby/object:Gem::Version
53
- version: '3'
61
+ version: '0'
54
62
  type: :runtime
55
63
  prerelease: false
56
64
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
65
  requirements:
59
- - - ! '>'
66
+ - - ">="
60
67
  - !ruby/object:Gem::Version
61
- version: '3'
68
+ version: '0'
62
69
  - !ruby/object:Gem::Dependency
63
- name: progress_bar
70
+ name: httparty
64
71
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
72
  requirements:
67
- - - ! '>='
73
+ - - ">="
68
74
  - !ruby/object:Gem::Version
69
75
  version: '0'
70
76
  type: :runtime
71
77
  prerelease: false
72
78
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
79
  requirements:
75
- - - ! '>='
80
+ - - ">="
76
81
  - !ruby/object:Gem::Version
77
82
  version: '0'
78
83
  - !ruby/object:Gem::Dependency
79
84
  name: pg
80
85
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
86
  requirements:
83
- - - ! '>='
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: ruby-progressbar
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :runtime
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: unicode
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: bundler
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '1.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '1.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '10.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '10.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: simplecov
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - ">="
172
+ - !ruby/object:Gem::Version
173
+ version: '0'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ - !ruby/object:Gem::Dependency
182
+ name: rubocop
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ">="
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: webmock
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
84
200
  - !ruby/object:Gem::Version
85
201
  version: '0'
86
202
  type: :development
87
203
  prerelease: false
88
204
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
205
  requirements:
91
- - - ! '>='
206
+ - - ">="
92
207
  - !ruby/object:Gem::Version
93
208
  version: '0'
94
209
  - !ruby/object:Gem::Dependency
95
210
  name: sqlite3
96
211
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
212
  requirements:
99
- - - ! '>='
213
+ - - ">="
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ type: :development
217
+ prerelease: false
218
+ version_requirements: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ - !ruby/object:Gem::Dependency
224
+ name: codeclimate-test-reporter
225
+ requirement: !ruby/object:Gem::Requirement
226
+ requirements:
227
+ - - ">="
100
228
  - !ruby/object:Gem::Version
101
229
  version: '0'
102
230
  type: :development
103
231
  prerelease: false
104
232
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
233
  requirements:
107
- - - ! '>='
234
+ - - ">="
108
235
  - !ruby/object:Gem::Version
109
236
  version: '0'
110
237
  - !ruby/object:Gem::Dependency
111
- name: hirb
238
+ name: ruby-prof
112
239
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
240
  requirements:
115
- - - ! '>='
241
+ - - ">="
116
242
  - !ruby/object:Gem::Version
117
243
  version: '0'
118
244
  type: :development
119
245
  prerelease: false
120
246
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
247
  requirements:
123
- - - ! '>='
248
+ - - ">="
124
249
  - !ruby/object:Gem::Version
125
250
  version: '0'
126
- description: Ruby wrapper to FIAS database
251
+ description: Imports Russian FIAS database into SQL (for Ruby on Rails on PostgreSQL
252
+ projects)
127
253
  email:
128
254
  - gzigzigzeo@evilmartians.com
129
255
  executables: []
130
256
  extensions: []
131
257
  extra_rdoc_files: []
132
258
  files:
133
- - .gitignore
259
+ - ".gitignore"
260
+ - ".rubocop.yml"
261
+ - ".travis.yml"
134
262
  - Gemfile
135
263
  - LICENSE.txt
136
264
  - README.md
137
265
  - Rakefile
266
+ - config/names.txt
267
+ - config/synonyms.yml
268
+ - examples/create.rb
269
+ - examples/generate_index.rb
138
270
  - fias.gemspec
139
271
  - lib/fias.rb
140
- - lib/fias/active_record/address_object.rb
141
- - lib/fias/active_record/address_object_type.rb
142
- - lib/fias/dbf_wrapper.rb
143
- - lib/fias/importer.rb
144
- - lib/fias/importer/base.rb
145
- - lib/fias/importer/pg.rb
146
- - lib/fias/importer/sqlite.rb
272
+ - lib/fias/config.rb
273
+ - lib/fias/import/copy.rb
274
+ - lib/fias/import/dbf.rb
275
+ - lib/fias/import/download_service.rb
276
+ - lib/fias/import/restore_parent_id.rb
277
+ - lib/fias/import/tables.rb
278
+ - lib/fias/name/append.rb
279
+ - lib/fias/name/canonical.rb
280
+ - lib/fias/name/extract.rb
281
+ - lib/fias/name/house_number.rb
282
+ - lib/fias/name/split.rb
283
+ - lib/fias/name/synonyms.rb
284
+ - lib/fias/query.rb
285
+ - lib/fias/query/estimate.rb
286
+ - lib/fias/query/finder.rb
287
+ - lib/fias/query/params.rb
147
288
  - lib/fias/railtie.rb
148
289
  - lib/fias/version.rb
149
- - lib/generators/fias/migration.rb
150
- - lib/generators/fias/templates/create_fias_tables.rb
151
- - tasks/fias.rake
290
+ - spec/fixtures/ACTSTAT.DBF
291
+ - spec/fixtures/NORDOC99.DBF
292
+ - spec/fixtures/STRSTAT.DBF
293
+ - spec/fixtures/addressing.yml
294
+ - spec/fixtures/query.yml
295
+ - spec/fixtures/query_sanitization.yml
296
+ - spec/fixtures/status_append.yml
297
+ - spec/lib/import/copy_spec.rb
298
+ - spec/lib/import/dbf_spec.rb
299
+ - spec/lib/import/download_service_spec.rb
300
+ - spec/lib/import/restore_parent_id_spec.rb
301
+ - spec/lib/import/tables_spec.rb
302
+ - spec/lib/name/append_spec.rb
303
+ - spec/lib/name/canonical_spec.rb
304
+ - spec/lib/name/extract_spec.rb
305
+ - spec/lib/name/house_number_spec.rb
306
+ - spec/lib/name/query_spec.rb
307
+ - spec/lib/name/split_spec.rb
308
+ - spec/lib/name/synonyms_spec.rb
309
+ - spec/lib/query/params_spec.rb
310
+ - spec/lib/query_spec.rb
311
+ - spec/spec_helper.rb
312
+ - spec/support/db.rb
313
+ - spec/support/query.rb
314
+ - tasks/db.rake
315
+ - tasks/download.rake
152
316
  homepage: http://github.com/evilmartians/fias
153
- licenses: []
317
+ licenses:
318
+ - MIT
319
+ metadata: {}
154
320
  post_install_message:
155
321
  rdoc_options: []
156
322
  require_paths:
157
323
  - lib
158
324
  required_ruby_version: !ruby/object:Gem::Requirement
159
- none: false
160
325
  requirements:
161
- - - ! '>='
326
+ - - ">="
162
327
  - !ruby/object:Gem::Version
163
328
  version: '0'
164
- segments:
165
- - 0
166
- hash: 493497803985021140
167
329
  required_rubygems_version: !ruby/object:Gem::Requirement
168
- none: false
169
330
  requirements:
170
- - - ! '>='
331
+ - - ">="
171
332
  - !ruby/object:Gem::Version
172
333
  version: '0'
173
- segments:
174
- - 0
175
- hash: 493497803985021140
176
334
  requirements: []
177
335
  rubyforge_project:
178
- rubygems_version: 1.8.24
336
+ rubygems_version: 2.2.2
179
337
  signing_key:
180
- specification_version: 3
181
- summary: Ruby wrapper to FIAS database
182
- test_files: []
338
+ specification_version: 4
339
+ summary: Imports Russian FIAS database into SQL
340
+ test_files:
341
+ - spec/fixtures/ACTSTAT.DBF
342
+ - spec/fixtures/NORDOC99.DBF
343
+ - spec/fixtures/STRSTAT.DBF
344
+ - spec/fixtures/addressing.yml
345
+ - spec/fixtures/query.yml
346
+ - spec/fixtures/query_sanitization.yml
347
+ - spec/fixtures/status_append.yml
348
+ - spec/lib/import/copy_spec.rb
349
+ - spec/lib/import/dbf_spec.rb
350
+ - spec/lib/import/download_service_spec.rb
351
+ - spec/lib/import/restore_parent_id_spec.rb
352
+ - spec/lib/import/tables_spec.rb
353
+ - spec/lib/name/append_spec.rb
354
+ - spec/lib/name/canonical_spec.rb
355
+ - spec/lib/name/extract_spec.rb
356
+ - spec/lib/name/house_number_spec.rb
357
+ - spec/lib/name/query_spec.rb
358
+ - spec/lib/name/split_spec.rb
359
+ - spec/lib/name/synonyms_spec.rb
360
+ - spec/lib/query/params_spec.rb
361
+ - spec/lib/query_spec.rb
362
+ - spec/spec_helper.rb
363
+ - spec/support/db.rb
364
+ - spec/support/query.rb