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
@@ -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
@@ -1,5 +0,0 @@
1
- class CreateFiasTables < ActiveRecord::Migration
2
- def change
3
- <%= @schema %>
4
- end
5
- end
@@ -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