fias 0.0.2 → 1.0.0
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 +7 -0
- data/.gitignore +15 -22
- data/.rubocop.yml +7 -0
- data/.travis.yml +10 -0
- data/Gemfile +1 -1
- data/LICENSE.txt +2 -2
- data/README.md +259 -155
- data/Rakefile +6 -1
- data/config/names.txt +0 -0
- data/config/synonyms.yml +50 -0
- data/examples/create.rb +106 -0
- data/examples/generate_index.rb +63 -0
- data/fias.gemspec +33 -21
- data/lib/fias.rb +197 -10
- data/lib/fias/config.rb +74 -0
- data/lib/fias/import/copy.rb +62 -0
- data/lib/fias/import/dbf.rb +81 -0
- data/lib/fias/import/download_service.rb +37 -0
- data/lib/fias/import/restore_parent_id.rb +51 -0
- data/lib/fias/import/tables.rb +74 -0
- data/lib/fias/name/append.rb +30 -0
- data/lib/fias/name/canonical.rb +42 -0
- data/lib/fias/name/extract.rb +85 -0
- data/lib/fias/name/house_number.rb +71 -0
- data/lib/fias/name/split.rb +60 -0
- data/lib/fias/name/synonyms.rb +93 -0
- data/lib/fias/query.rb +43 -0
- data/lib/fias/query/estimate.rb +67 -0
- data/lib/fias/query/finder.rb +75 -0
- data/lib/fias/query/params.rb +101 -0
- data/lib/fias/railtie.rb +3 -17
- data/lib/fias/version.rb +1 -1
- data/spec/fixtures/ACTSTAT.DBF +0 -0
- data/spec/fixtures/NORDOC99.DBF +0 -0
- data/spec/fixtures/STRSTAT.DBF +0 -0
- data/spec/fixtures/addressing.yml +93 -0
- data/spec/fixtures/query.yml +79 -0
- data/spec/fixtures/query_sanitization.yml +75 -0
- data/spec/fixtures/status_append.yml +60 -0
- data/spec/lib/import/copy_spec.rb +44 -0
- data/spec/lib/import/dbf_spec.rb +28 -0
- data/spec/lib/import/download_service_spec.rb +15 -0
- data/spec/lib/import/restore_parent_id_spec.rb +34 -0
- data/spec/lib/import/tables_spec.rb +26 -0
- data/spec/lib/name/append_spec.rb +14 -0
- data/spec/lib/name/canonical_spec.rb +20 -0
- data/spec/lib/name/extract_spec.rb +67 -0
- data/spec/lib/name/house_number_spec.rb +45 -0
- data/spec/lib/name/query_spec.rb +21 -0
- data/spec/lib/name/split_spec.rb +15 -0
- data/spec/lib/name/synonyms_spec.rb +51 -0
- data/spec/lib/query/params_spec.rb +15 -0
- data/spec/lib/query_spec.rb +27 -0
- data/spec/spec_helper.rb +30 -0
- data/spec/support/db.rb +30 -0
- data/spec/support/query.rb +13 -0
- data/tasks/db.rake +52 -0
- data/tasks/download.rake +15 -0
- metadata +246 -64
- data/lib/fias/active_record/address_object.rb +0 -231
- data/lib/fias/active_record/address_object_type.rb +0 -15
- data/lib/fias/dbf_wrapper.rb +0 -90
- data/lib/fias/importer.rb +0 -30
- data/lib/fias/importer/base.rb +0 -59
- data/lib/fias/importer/pg.rb +0 -81
- data/lib/fias/importer/sqlite.rb +0 -38
- data/lib/generators/fias/migration.rb +0 -34
- data/lib/generators/fias/templates/create_fias_tables.rb +0 -5
- data/tasks/fias.rake +0 -68
data/lib/fias/importer/sqlite.rb
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
module Fias
|
2
|
-
module Importer
|
3
|
-
# Нужно для :memory: баз в первую очередь
|
4
|
-
class Sqlite < Base
|
5
|
-
def import_table(name, table_name, dbf, &block)
|
6
|
-
truncate_table(table_name)
|
7
|
-
|
8
|
-
qmarks = ['?'] * dbf.columns.keys.size
|
9
|
-
qmarks = qmarks.join(', ')
|
10
|
-
|
11
|
-
dbf.each_with_index do |record, index|
|
12
|
-
data = record.attributes
|
13
|
-
|
14
|
-
should_import = yield(name, data, index) if block_given?
|
15
|
-
|
16
|
-
unless should_import === false
|
17
|
-
columns = data.keys.join(', ')
|
18
|
-
|
19
|
-
values = data.values.map do |value|
|
20
|
-
if value.is_a?(Date)
|
21
|
-
value.to_s
|
22
|
-
else
|
23
|
-
value
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
connection.execute("INSERT INTO #{table_name} (#{columns}) VALUES (#{qmarks});", values)
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
def truncate_table(table_name)
|
34
|
-
connection.execute "DELETE FROM #{table_name} WHERE 1"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
module Fias
|
2
|
-
class MigrationGenerator < Rails::Generators::Base
|
3
|
-
include Rails::Generators::Migration
|
4
|
-
|
5
|
-
class_option :prefix, type: :string, default: :fias, desc: 'Table names prefix'
|
6
|
-
class_option :path, type: :string, default: 'tmp/fias', desc: 'Path to FIAS dbfs'
|
7
|
-
class_option :only, type: :string, default: '', desc: 'Only tables'
|
8
|
-
|
9
|
-
source_root File.expand_path("../templates", __FILE__)
|
10
|
-
|
11
|
-
def generate_migration
|
12
|
-
only = options.only.split(',').map(&:strip)
|
13
|
-
wrapper = Fias::DbfWrapper.new(options.path)
|
14
|
-
importer = Fias::Importer.build(prefix: options.prefix)
|
15
|
-
|
16
|
-
tables = wrapper.tables(only)
|
17
|
-
@schema = importer.schema(tables)
|
18
|
-
@schema.gsub!("\n", "\n ")
|
19
|
-
|
20
|
-
migration_template 'create_fias_tables.rb', 'db/migrate/create_fias_tables'
|
21
|
-
end
|
22
|
-
|
23
|
-
def usage
|
24
|
-
"Generates FIAS migrations for application"
|
25
|
-
end
|
26
|
-
|
27
|
-
# https://rails.lighthouseapp.com/projects/8994/tickets/3820-make-railsgeneratorsmigrationnext_migration_number-method-a-class-method-so-it-possible-to-use-it-in-custom-generators
|
28
|
-
def self.next_migration_number(dirname)
|
29
|
-
orm = Rails.configuration.generators.options[:rails][:orm]
|
30
|
-
require "rails/generators/#{orm}"
|
31
|
-
"#{orm.to_s.camelize}::Generators::Base".constantize.next_migration_number(dirname)
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
data/tasks/fias.rake
DELETED
@@ -1,68 +0,0 @@
|
|
1
|
-
require 'fias'
|
2
|
-
|
3
|
-
namespace :fias do
|
4
|
-
class << self
|
5
|
-
private
|
6
|
-
# Если гем используется внутри рельсов - стоит загрузить энвайронмент
|
7
|
-
# и подключиться к БД.
|
8
|
-
def may_be_rails(name)
|
9
|
-
defined?(Rails) ? {name => :environment} : name
|
10
|
-
end
|
11
|
-
|
12
|
-
# Открывает DBFы ФИАС, соединяется с базой и передает все это блоку
|
13
|
-
def within_connection(&block)
|
14
|
-
require 'active_record'
|
15
|
-
require 'progress_bar'
|
16
|
-
|
17
|
-
begin
|
18
|
-
ActiveRecord::Base.connection
|
19
|
-
rescue ActiveRecord::ConnectionNotEstablished
|
20
|
-
if ENV['DATABASE_URL'].nil?
|
21
|
-
raise ArgumentError, 'Specify database in DATABASE_URL env variable'
|
22
|
-
end
|
23
|
-
|
24
|
-
ActiveRecord::Base.establish_connection
|
25
|
-
end
|
26
|
-
|
27
|
-
fias_path = ENV['FIAS'] || 'tmp/fias'
|
28
|
-
wrapper = Fias::DbfWrapper.new(fias_path)
|
29
|
-
importer = Fias::Importer.build(prefix: ENV['PREFIX'])
|
30
|
-
|
31
|
-
yield(wrapper, importer)
|
32
|
-
end
|
33
|
-
|
34
|
-
def only
|
35
|
-
only = ENV['ONLY'].to_s.split(',').map(&:strip)
|
36
|
-
end
|
37
|
-
end
|
38
|
-
|
39
|
-
desc 'Create FIAS tables (could specify tables PREFIX, PATH to dbfs and DATABASE_URL, EXCLUDE or ONLY tables)'
|
40
|
-
task may_be_rails(:create_tables) do
|
41
|
-
within_connection do |wrapper, importer|
|
42
|
-
tables = wrapper.tables(only)
|
43
|
-
# TODO: Добавить во враппер tables, это убрать
|
44
|
-
raise "DBF file not found for: #{key}" if tables.keys.any? { |key| key.nil? }
|
45
|
-
ActiveRecord::Schema.define do
|
46
|
-
eval(importer.schema(tables))
|
47
|
-
end
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
desc 'Import FIAS data'
|
52
|
-
task may_be_rails(:import) do
|
53
|
-
within_connection do |wrapper, importer|
|
54
|
-
tables = wrapper.tables(only)
|
55
|
-
|
56
|
-
total_record_count = tables.sum do |accessor, dbf|
|
57
|
-
dbf.present? ? dbf.record_count : 0
|
58
|
-
end
|
59
|
-
|
60
|
-
puts 'Importing FIAS data...'
|
61
|
-
|
62
|
-
bar = ProgressBar.new(total_record_count)
|
63
|
-
importer.import(tables) do
|
64
|
-
bar.increment!
|
65
|
-
end
|
66
|
-
end
|
67
|
-
end
|
68
|
-
end
|