terc_parser 0.0.7

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e0891392b8c802d19f1a84bf41de1af2d97860a3
4
+ data.tar.gz: 50930c5ddb4b6f418fc47f09767591a05879f8a3
5
+ SHA512:
6
+ metadata.gz: 905bcdcd042aba9b888284712787314ab2d8ecb0da6842f1b017e731dcbb717a72301f14247d071a963a64f226a5321c225503497077d521d4fc800f62106315
7
+ data.tar.gz: bbcac2b5488cc3134c8ba736015cd027d9fdb4b3a9a8e000cda1f7e7df9edc0b6ac01896e312ef9261884653a396358ff0910e21d3d1d663761f86810d59497c
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .idea/*
6
+ *TERC*.xml
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in terc_parser.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ #TERC parser
2
+
3
+ Gem składa się praktycznie z jednej metody: `TercParser::parse_terc`. Po jej wywołaniu, opcjonalnie podając jako argument ścieżkę do pliku `TERC.xml`, zwróci ona hash zawierający cztery klucze:
4
+
5
+ * **:stan_na** - zawiera datę (jako string) utworzenia pliku `TERC.xml`
6
+ * **:wojewodztwa** - zawiera tablice hashy, każdy hash zawiera informacje o jednym województwie
7
+ * **:powiaty** - jw. ale dane o powiatach
8
+ * **:gminy** - jw. ale dane o gminach
9
+
10
+ Aktualny plik `TERC.xml` można pobrać ze strony GUSu:
11
+ http://www.stat.gov.pl/broker/access/prefile/listPreFiles.jspa
12
+
13
+ Plik `TERC.xml` jest cyfrową wersją [rejestru identyfikatorów i nazw jednostek podziału administracyjnego Polski](http://pl.wikipedia.org/wiki/TERC) udostępnionym przez [GUS](http://pl.wikipedia.org/wiki/GUS) i jest częścią rejestru [TERYT](http://pl.wikipedia.org/wiki/TERYT).
14
+
15
+ ## Instalacja
16
+
17
+ ``` bash
18
+ gem install terc_parser
19
+ ```
20
+
21
+ ## Przykład
22
+
23
+ Wynik działania:
24
+
25
+ ``` bash
26
+ STAN NA: 2011-01-01
27
+ -------- WOJEWODZTWA (16) ---------
28
+ 2 - dolnośląskie
29
+ 4 - kujawsko-pomorskie
30
+ 6 - lubelskie
31
+ 8 - lubuskie
32
+ 10 - łódzkie
33
+ 12 - małopolskie
34
+ 14 - mazowieckie
35
+ 16 - opolskie
36
+ 18 - podkarpackie
37
+ 20 - podlaskie
38
+ 22 - pomorskie
39
+ 24 - śląskie
40
+ 26 - świętokrzyskie
41
+ 28 - warmińsko-mazurskie
42
+ 30 - wielkopolskie
43
+ 32 - zachodniopomorskie
44
+ -------- POWIATY (379) -------------
45
+ -------- GMINY (2479) -------------
46
+ ```
47
+
48
+ Kod:
49
+
50
+ ``` ruby
51
+ require "terc_parser"
52
+
53
+ terc = TercParser::parse_terc "/path_to_terc/TERC.xml"
54
+
55
+ puts "STAN NA: #{terc[:stan_na]}"
56
+
57
+ puts "-------- WOJEWODZTWA (#{terc[:wojewodztwa].count}) ---------"
58
+ terc[:wojewodztwa].each do |w|
59
+ puts "#{w[:id]} - #{w[:name]}"
60
+ end
61
+
62
+ puts "-------- POWIATY (#{terc[:powiaty].count}) -------------"
63
+ terc[:powiaty].each do |p|
64
+ #puts "#{p[:id]} - #{p[:woj_id]} - #{p[:name]}"
65
+ end
66
+
67
+ puts "-------- GMINY (#{terc[:gminy].count}) -------------"
68
+ terc[:gminy].each do |g|
69
+ #puts "#{g[:id]} - #{g[:woj_id]} - #{g[:pow_id]} - #{g[:name]}"
70
+ end
71
+
72
+ ```
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "terc_parser/version"
2
+ require "terc_parser/terc_parser"
@@ -0,0 +1,54 @@
1
+ #encoding: utf-8
2
+ require "nokogiri"
3
+ require "active_support/core_ext/string/multibyte"
4
+
5
+ module TercParser
6
+ class << self
7
+ def parse_terc(terc_file_path = "TERC.xml")
8
+ stan_na = wojewodztwa = powiaty = gminy = nil
9
+
10
+ process_xml_file terc_file_path do |doc|
11
+ stan_na = doc.xpath("/teryt/catalog/@date").text
12
+
13
+ ### WOJEWODZTWA ## ----------------------------------
14
+ wojewodztwa_nodeset = doc.xpath("//col[@name='NAZDOD' and text()='województwo']/..")
15
+
16
+ wojewodztwa = wojewodztwa_nodeset.map do |woj|
17
+ id = woj.xpath("col[@name = 'WOJ']").text.to_i
18
+ name = woj.xpath("col[@name = 'NAZWA']/text()").text.mb_chars.downcase.to_s
19
+ {id: id, name: name}
20
+ end
21
+
22
+ ### POWIATY ## --------------------------------------
23
+ powiaty_nodeset = doc.xpath("//col[@name='NAZDOD' and contains(text(),'powiat')]/..")
24
+
25
+ powiaty = powiaty_nodeset.map do |woj|
26
+ id = woj.xpath("col[@name = 'POW']").text.to_i
27
+ woj_id = woj.xpath("col[@name = 'WOJ']").text.to_i
28
+ name = woj.xpath("col[@name = 'NAZWA']/text()").text.mb_chars.downcase.to_s
29
+ {id: id, woj_id: woj_id, name: name}
30
+ end
31
+
32
+ ### GMINY ## --------------------------------------
33
+ gminy_nodeset = doc.xpath("//col[@name='NAZDOD' and contains(text(),'gmina')]/..")
34
+
35
+ gminy = gminy_nodeset.map do |woj|
36
+ id = woj.xpath("col[@name = 'GMI']").text.to_i
37
+ woj_id = woj.xpath("col[@name = 'WOJ']").text.to_i
38
+ pow_id = woj.xpath("col[@name = 'POW']").text.to_i
39
+ name = woj.xpath("col[@name = 'NAZWA']/text()").text.mb_chars.downcase.to_s
40
+ {id: id, woj_id: woj_id, pow_id: pow_id, name: name}
41
+ end
42
+ end
43
+
44
+ return {stan_na: stan_na, wojewodztwa: wojewodztwa, powiaty: powiaty, gminy: gminy}
45
+ end
46
+
47
+ def process_xml_file(file_name, &block)
48
+ f = File.open(file_name)
49
+ doc = Nokogiri::XML(f) { |config| config.strict }
50
+ yield doc
51
+ f.close
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module TercParser
2
+ VERSION = "0.0.7"
3
+ end
@@ -0,0 +1 @@
1
+ require 'terc_parser'
@@ -0,0 +1,28 @@
1
+ #encoding: utf-8
2
+ require 'spec_helper'
3
+
4
+ TERC_XML_FILE_PATH = File.expand_path('../../../vendor/TERC.xml', __FILE__)
5
+
6
+ describe TercParser do
7
+ let(:terc) { TercParser::parse_terc TERC_XML_FILE_PATH }
8
+
9
+ it "should collect data that are correct type" do
10
+ terc.should be_instance_of Hash
11
+ terc[:stan_na].class.should == String
12
+
13
+ terc[:wojewodztwa].should be_instance_of Array
14
+ terc[:wojewodztwa][0][:id].should be_instance_of Fixnum
15
+ terc[:wojewodztwa][0][:name].should be_instance_of String
16
+
17
+ terc[:powiaty].should be_instance_of Array
18
+ terc[:powiaty][0][:id].should be_instance_of Fixnum
19
+ terc[:powiaty][0][:woj_id].should be_instance_of Fixnum
20
+ terc[:powiaty][0][:name].should be_instance_of String
21
+
22
+ terc[:gminy].should be_instance_of Array
23
+ terc[:gminy][0][:id].should be_instance_of Fixnum
24
+ terc[:gminy][0][:woj_id].should be_instance_of Fixnum
25
+ terc[:gminy][0][:pow_id].should be_instance_of Fixnum
26
+ terc[:gminy][0][:name].should be_instance_of String
27
+ end
28
+ end
@@ -0,0 +1,22 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "terc_parser/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = "terc_parser"
8
+ s.version = TercParser::VERSION
9
+ s.authors = ["TeWu"]
10
+ s.email = ["tewu.dev@gmail.com"]
11
+ s.homepage = "https://github.com/TeWu/TERC_parser"
12
+ s.summary = %q{Nazwy województw, powiatów i gmin w jednym Hashu}
13
+
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
16
+ s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
17
+ s.require_paths = ["lib"]
18
+
19
+ s.add_development_dependency "rspec", "~> 3.5"
20
+ s.add_runtime_dependency "nokogiri", "~> 1.6"
21
+ s.add_runtime_dependency "activesupport", "~> 5.0"
22
+ end
data/vendor/.gitkeep ADDED
File without changes
metadata ADDED
@@ -0,0 +1,97 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terc_parser
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.7
5
+ platform: ruby
6
+ authors:
7
+ - TeWu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.6'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description:
56
+ email:
57
+ - tewu.dev@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - Gemfile
65
+ - README.md
66
+ - Rakefile
67
+ - lib/terc_parser.rb
68
+ - lib/terc_parser/terc_parser.rb
69
+ - lib/terc_parser/version.rb
70
+ - spec/spec_helper.rb
71
+ - spec/terc_parser/terc_parser_spec.rb
72
+ - terc_parser.gemspec
73
+ - vendor/.gitkeep
74
+ homepage: https://github.com/TeWu/TERC_parser
75
+ licenses: []
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.4.8
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Nazwy województw, powiatów i gmin w jednym Hashu
97
+ test_files: []