ruian 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (86) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +25 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +29 -0
  6. data/Rakefile +2 -0
  7. data/example/.gitignore +15 -0
  8. data/example/Gemfile +28 -0
  9. data/example/README.md +23 -0
  10. data/example/Rakefile +7 -0
  11. data/example/app/assets/images/rails.png +0 -0
  12. data/example/app/assets/javascripts/application.js +13 -0
  13. data/example/app/assets/stylesheets/application.css +13 -0
  14. data/example/app/controllers/application_controller.rb +3 -0
  15. data/example/app/helpers/application_helper.rb +2 -0
  16. data/example/app/mailers/.gitkeep +0 -0
  17. data/example/app/models/.gitkeep +0 -0
  18. data/example/app/models/address.rb +5 -0
  19. data/example/app/models/address/city.rb +7 -0
  20. data/example/app/models/address/city_part.rb +7 -0
  21. data/example/app/models/address/county.rb +3 -0
  22. data/example/app/models/address/county_name_count.rb +7 -0
  23. data/example/app/models/address/momc_name.rb +5 -0
  24. data/example/app/models/address/mop_name.rb +5 -0
  25. data/example/app/models/address/name.rb +18 -0
  26. data/example/app/models/address/name_type.rb +6 -0
  27. data/example/app/models/address/place.rb +13 -0
  28. data/example/app/models/address/region.rb +3 -0
  29. data/example/app/models/address/region_name_count.rb +7 -0
  30. data/example/app/models/address/street_name.rb +5 -0
  31. data/example/app/views/layouts/application.html.erb +14 -0
  32. data/example/config.ru +4 -0
  33. data/example/config/application.rb +63 -0
  34. data/example/config/boot.rb +6 -0
  35. data/example/config/database.yml +25 -0
  36. data/example/config/environment.rb +5 -0
  37. data/example/config/environments/development.rb +32 -0
  38. data/example/config/environments/production.rb +54 -0
  39. data/example/config/environments/test.rb +37 -0
  40. data/example/config/initializers/backtrace_silencers.rb +7 -0
  41. data/example/config/initializers/inflections.rb +15 -0
  42. data/example/config/initializers/mime_types.rb +5 -0
  43. data/example/config/initializers/secret_token.rb +7 -0
  44. data/example/config/initializers/session_store.rb +8 -0
  45. data/example/config/initializers/wrap_parameters.rb +14 -0
  46. data/example/config/locales/en.yml +5 -0
  47. data/example/config/routes.rb +58 -0
  48. data/example/db/migrate/20140418190407_create_tables.rb +170 -0
  49. data/example/db/schema.rb +182 -0
  50. data/example/db/seeds.rb +7 -0
  51. data/example/lib/assets/.gitkeep +0 -0
  52. data/example/lib/importer.rb +163 -0
  53. data/example/lib/tasks/.gitkeep +0 -0
  54. data/example/lib/tasks/ruian.rake +4 -0
  55. data/example/log/.gitkeep +0 -0
  56. data/example/public/404.html +26 -0
  57. data/example/public/422.html +26 -0
  58. data/example/public/500.html +25 -0
  59. data/example/public/favicon.ico +0 -0
  60. data/example/public/index.html +241 -0
  61. data/example/public/robots.txt +5 -0
  62. data/example/script/rails +6 -0
  63. data/example/vendor/assets/javascripts/.gitkeep +0 -0
  64. data/example/vendor/assets/stylesheets/.gitkeep +0 -0
  65. data/example/vendor/plugins/.gitkeep +0 -0
  66. data/fixtures/counties.csv +92 -0
  67. data/fixtures/vazby-cr.csv +15072 -0
  68. data/fixtures/vazby-okresy-cr.csv +15072 -0
  69. data/lib/ruian.rb +86 -0
  70. data/lib/ruian/enum_updater.rb +59 -0
  71. data/lib/ruian/fetcher.rb +71 -0
  72. data/lib/ruian/files_updater.rb +54 -0
  73. data/lib/ruian/importer.rb +21 -0
  74. data/lib/ruian/model.rb +10 -0
  75. data/lib/ruian/model/county.rb +6 -0
  76. data/lib/ruian/model/county_integration.rb +6 -0
  77. data/lib/ruian/model/region.rb +6 -0
  78. data/lib/ruian/model/region_integration.rb +6 -0
  79. data/lib/ruian/model/row.rb +9 -0
  80. data/lib/ruian/parser.rb +12 -0
  81. data/lib/ruian/queue.rb +24 -0
  82. data/lib/ruian/railtie.rb +9 -0
  83. data/lib/ruian/tasks/ruian.rake +18 -0
  84. data/lib/ruian/version.rb +3 -0
  85. data/ruian.gemspec +26 -0
  86. metadata +198 -0
@@ -0,0 +1,86 @@
1
+ require "ruian/version"
2
+ require 'ruby-progressbar'
3
+ require 'ruian/parser'
4
+ require 'ruian/model'
5
+ require 'ruian/queue'
6
+ require 'ruian/fetcher'
7
+ require 'ruian/files_updater'
8
+ require 'ruian/enum_updater'
9
+ require 'ruian/importer'
10
+ require 'ruian/railtie' if defined?(Rails)
11
+ require 'logger'
12
+
13
+ class Ruian
14
+ attr_accessor :queue, :fetcher, :importer
15
+
16
+ class << self
17
+ attr_accessor :logger
18
+ end
19
+
20
+ self.logger = Logger.new($stdout)
21
+
22
+ def self.load_tasks
23
+ Dir[File.join(File.dirname(__FILE__),'tasks/*.rake')].each { |f| load f }
24
+ end
25
+
26
+ def self.root
27
+ Pathname.new(File.expand_path('../', File.dirname(__FILE__)))
28
+ end
29
+
30
+ def initialize(importer = Ruian::Importer.new)
31
+ self.fetcher = Ruian::Fetcher.new
32
+ self.importer = importer
33
+ self.queue = Ruian::Queue.new(importer)
34
+ end
35
+
36
+ def fetch_rows
37
+ count = fetcher.row_files.count
38
+ bar = ProgressBar.create(total: count,
39
+ title: "Importing #{count} row files.",
40
+ format: "%a |%b>>%i| %c/%C %p%% %t")
41
+
42
+ self.fetcher.parse_rows(self.queue) { bar.increment }
43
+ end
44
+
45
+ def fetch_regions
46
+ bar = ProgressBar.create(total: 1,
47
+ title: 'Importing 1 region file.',
48
+ format: "%a |%b>>%i| %c/%C %p%% %t")
49
+
50
+ self.fetcher.parse_regions(self.queue) { bar.increment }
51
+ end
52
+
53
+ def fetch_counties
54
+ bar = ProgressBar.create(total: 1,
55
+ title: 'Importing 1 county file.',
56
+ format: "%a |%b>>%i| %c/%C %p%% %t")
57
+
58
+ self.fetcher.parse_counties(self.queue) { bar.increment }
59
+ end
60
+
61
+ def fetch_integrate_counties
62
+ bar = ProgressBar.create(total: 1,
63
+ title: 'Importing 1 county integration file.',
64
+ format: "%a |%b>>%i| %c/%C %p%% %t")
65
+
66
+ self.fetcher.parse_counties_integration(self.queue) { bar.increment }
67
+ end
68
+
69
+ def fetch_integrate_regions
70
+ bar = ProgressBar.create(total: 1,
71
+ title: 'Importing 1 region integration file.',
72
+ format: "%a |%b>>%i| %c/%C %p%% %t")
73
+
74
+ self.fetcher.parse_regions_integration(self.queue) { bar.increment }
75
+ end
76
+
77
+ def all
78
+ fetch_rows
79
+ fetch_regions
80
+ fetch_counties
81
+ fetch_integrate_counties
82
+ fetch_integrate_regions
83
+ self.importer.finalize!
84
+ end
85
+ end
86
+
@@ -0,0 +1,59 @@
1
+ require 'open-uri'
2
+ require 'fileutils'
3
+ require 'zip'
4
+
5
+ class Ruian
6
+ class EnumUpdater
7
+ URL = "http://vdp.cuzk.cz/vymenny_format/csv/20140331_strukt_ADR.csv.zip"
8
+
9
+ def update!
10
+ download
11
+ remove_old
12
+ unpack
13
+ remove_temp
14
+ end
15
+
16
+ def download
17
+ File.open(filepath, "wb") do |file|
18
+ open(URL) do |url|
19
+ file.write(url.read)
20
+ end
21
+ end
22
+ end
23
+
24
+ def unpack
25
+ Zip::File.open(filepath) do |zip_file|
26
+ zip_file.each do |entry|
27
+ name = File.basename(entry.name)
28
+ if needed.include?(name)
29
+ fullpath = fixtures_directory.join(name)
30
+ entry.extract(fullpath)
31
+ end
32
+ end
33
+ end
34
+ end
35
+
36
+ def remove_old
37
+ first = fixtures_directory.join(needed.first)
38
+ last = fixtures_directory.join(needed.last)
39
+ FileUtils.rm(first) if File.exists?(first)
40
+ FileUtils.rm(last) if File.exists?(last)
41
+ end
42
+
43
+ def remove_temp
44
+ FileUtils.rm(filepath)
45
+ end
46
+
47
+ def fixtures_directory
48
+ Ruian.root.join('fixtures')
49
+ end
50
+
51
+ def filepath
52
+ Ruian.root.join('fixtures', 'enum.zip')
53
+ end
54
+
55
+ def needed
56
+ ['vazby-cr.csv', 'vazby-okresy-cr.csv']
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,71 @@
1
+ class Ruian
2
+ class Fetcher
3
+
4
+ def parse_rows(queue, &block)
5
+ row_files.each do |file|
6
+ Ruian::Parser.foreach(file) do |attributes|
7
+ attributes = attributes.collect(&:last)
8
+ model = Ruian::Model::Row.new(*attributes)
9
+ queue.push(model)
10
+ end
11
+ yield
12
+ end
13
+ end
14
+
15
+ def parse_regions(queue, &block)
16
+ Ruian::Parser.foreach(region_file, encoding: 'utf-8') do |attributes|
17
+ attributes = attributes.collect(&:last)
18
+ model = Ruian::Model::Region.new(*attributes)
19
+ queue.push(model)
20
+ end
21
+ yield
22
+ end
23
+
24
+ def parse_counties(queue, &block)
25
+ Ruian::Parser.foreach(county_file, encoding: 'utf-8') do |attributes|
26
+ attributes = attributes.collect(&:last)
27
+ model = Ruian::Model::County.new(*attributes)
28
+ queue.push(model)
29
+ end
30
+ yield
31
+ end
32
+
33
+ def parse_counties_integration(queue, &block)
34
+ Ruian::Parser.foreach(county_integration_file) do |attributes|
35
+ attributes = attributes.collect(&:last)
36
+ model = Ruian::Model::CountyIntegration.new(*attributes)
37
+ queue.push(model)
38
+ end
39
+ yield
40
+ end
41
+
42
+ def parse_regions_integration(queue, &block)
43
+ Ruian::Parser.foreach(region_integration_file) do |attributes|
44
+ attributes = attributes.collect(&:last)
45
+ model = Ruian::Model::RegionIntegration.new(*attributes)
46
+ queue.push(model)
47
+ end
48
+ yield
49
+ end
50
+
51
+ def row_files
52
+ @files ||= Dir.glob(Ruian.root.join('fixtures/CSV', '*.csv'))
53
+ end
54
+
55
+ def region_file
56
+ @region_file = Ruian.root.join('fixtures', 'regions.csv')
57
+ end
58
+
59
+ def county_file
60
+ @county_file = Ruian.root.join('fixtures', 'counties.csv')
61
+ end
62
+
63
+ def county_integration_file
64
+ @count_integration_file = Ruian.root.join('fixtures', 'vazby-okresy-cr.csv')
65
+ end
66
+
67
+ def region_integration_file
68
+ @city_integration_file = Ruian.root.join('fixtures', 'vazby-cr.csv')
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,54 @@
1
+ require 'open-uri'
2
+ require 'fileutils'
3
+ require 'zip'
4
+
5
+ class Ruian
6
+ class FilesUpdater
7
+ URL = "http://vdp.cuzk.cz/vymenny_format/csv/20140331_OB_ADR_csv.zip"
8
+
9
+ def update!
10
+ download
11
+ remove_old
12
+ unpack
13
+ remove_temp
14
+ end
15
+
16
+ def download
17
+ File.open(filepath, "wb") do |file|
18
+ open(URL) do |url|
19
+ file.write(url.read)
20
+ end
21
+ end
22
+ end
23
+
24
+ def unpack
25
+ Zip::File.open(filepath) do |zip_file|
26
+ zip_file.each do |entry|
27
+ fullpath = fixtures_directory.join(entry.name)
28
+ FileUtils.mkdir_p(File.dirname(fullpath))
29
+ entry.extract(fullpath)
30
+ end
31
+ end
32
+ end
33
+
34
+ def remove_temp
35
+ FileUtils.rm(filepath)
36
+ end
37
+
38
+ def remove_old
39
+ FileUtils.rm_rf(csv_directory)
40
+ end
41
+
42
+ def fixtures_directory
43
+ Ruian.root.join('fixtures')
44
+ end
45
+
46
+ def csv_directory
47
+ Ruian.root.join('fixtures', 'CSV')
48
+ end
49
+
50
+ def filepath
51
+ Ruian.root.join('fixtures', 'csv.zip')
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,21 @@
1
+ class Ruian
2
+ class Importer
3
+ def import_row(row)
4
+ end
5
+
6
+ def import_region(region)
7
+ end
8
+
9
+ def import_county(county)
10
+ end
11
+
12
+ def import_countyintegration(county_integration)
13
+ end
14
+
15
+ def import_regionintegration(region_integration)
16
+ end
17
+
18
+ def finalize!
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,10 @@
1
+ require 'ruian/model/row'
2
+ require 'ruian/model/region'
3
+ require 'ruian/model/county'
4
+ require 'ruian/model/county_integration'
5
+ require 'ruian/model/region_integration'
6
+
7
+ class Ruian
8
+ class Model
9
+ end
10
+ end
@@ -0,0 +1,6 @@
1
+ class Ruian
2
+ class Model
3
+ class County < Struct.new(:code, :name)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class Ruian
2
+ class Model
3
+ class CountyIntegration < Struct.new(:obce_code, :obec_code, :pou_code, :orp_code, :vusr_code, :regsoudr_code, :stat_code)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class Ruian
2
+ class Model
3
+ class Region < Struct.new(:mvcr_code, :mvcr_name, :ruian_code, :ruian_name)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class Ruian
2
+ class Model
3
+ class RegionIntegration < Struct.new(:cobce_code, :obec_code, :pou_code, :orp_code, :vusc_code, :regsoudr_code, :stat_code)
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,9 @@
1
+ class Ruian
2
+ class Model
3
+ class Row < Struct.new(:adm_code, :city_code, :city_name,
4
+ :momc_name, :mop_name, :city_part_code,
5
+ :city_part_name, :street_name, :so_type,
6
+ :e, :o, :o_code, :zip, :gis_y, :gis_x, :valid_from)
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,12 @@
1
+ require 'csv'
2
+
3
+ class Ruian
4
+ class Parser < CSV
5
+ CSV_OPTIONS = { :encoding => 'windows-1250', :col_sep => ';', :quote_char => '"', :headers => true }
6
+
7
+ def self.foreach(path, options = Hash.new, &block)
8
+ options = Ruian::Parser::CSV_OPTIONS.merge(options)
9
+ super
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,24 @@
1
+ # not a really queue
2
+
3
+ class Ruian
4
+ class Queue
5
+ attr_accessor :importer
6
+
7
+ def initialize(importer)
8
+ self.importer = importer
9
+ end
10
+
11
+ def push(obj)
12
+ key = obj.class.to_s.split('::').last.downcase
13
+ callback(key, obj)
14
+ end
15
+
16
+ def callback(key, obj)
17
+ self.importer.send(callback_key(key), obj)
18
+ end
19
+
20
+ def callback_key(key)
21
+ "import_#{key}"
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ require 'ruian'
2
+ require 'rails'
3
+ class Ruian::Railtie < Rails::Railtie
4
+ railtie_name :ruian
5
+
6
+ rake_tasks do
7
+ load "ruian/tasks/ruian.rake"
8
+ end
9
+ end
@@ -0,0 +1,18 @@
1
+ require 'ruian'
2
+
3
+ namespace :ruian do
4
+ desc "Update address files"
5
+ task :update_files do
6
+ updater = Ruian::FilesUpdater.new
7
+ updater.update!
8
+ end
9
+
10
+ desc "Update counties and regions files"
11
+ task :update_enums do
12
+ updater = Ruian::EnumUpdater.new
13
+ updater.update!
14
+ end
15
+
16
+ desc "Update all files"
17
+ task :update_all => [:update_files, :update_enums]
18
+ end
@@ -0,0 +1,3 @@
1
+ class Ruian
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ruian/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruian"
8
+ spec.version = Ruian::VERSION
9
+ spec.authors = ["Josef Šimánek"]
10
+ spec.email = ["josef.simanek@gmail.com"]
11
+ spec.summary = %q{RUIAN}
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'ruby-progressbar'
21
+ spec.add_dependency 'rubyzip'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.6"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec"
26
+ end