gus-importer 0.0.1

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,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ NzUxYmU3Njg2MDlkNGM2YjllZDE0YzdlMjQyOGU1NmMzNzlkNzJhMA==
5
+ data.tar.gz: !binary |-
6
+ NjNiOTYxY2VmNmI4ODBmYTk4ZTFiYTZiYWZkNzU3MmJiM2ZhZjdiMA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ ZWQ0YWNhODM1NThjMjdjNzY2YTQxOTg1YjlkYzMyODQ1NzE0ZjlkM2M2OTcw
10
+ OWZlNzE1M2MwOTMwZGZhYTRmYzM5M2RmOTE3NGMwYTVkZmQwYWE5ZGViOWM1
11
+ NDQwODNhYzk0YjJmMjY1MDViNGE1YzdiYTAzY2JmNTU4NTA3M2Q=
12
+ data.tar.gz: !binary |-
13
+ OWJiN2IxNjI3ZTg1ZmQzNmQ5ZDU0Yzg3NjkwNzFhMGMwYzA2OGU3YTk0MjA4
14
+ MDc1OWQxMDZhNTcyNjQ1MTM5N2UwNDc2OGZiM2FhZDBiMTVhODcwZDllMzk1
15
+ ODMzNGNmY2Q1YTkwMTM2YjNjN2UwNzY3OWNhODJmZDI4N2UyMTg=
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.xml
19
+ .Trash-*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gus-importer.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ # Gus::Importer
2
+
3
+ Simple gem for extracting data from gus XML
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'gus-importer'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install gus-importer
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ Gus::Importer::ProvinceParser.parse(file) do |name, uuid|
23
+ puts "#{name} => #{uuid}"
24
+ end
25
+ ```
26
+
27
+ ``` ruby
28
+ Gus::Importer::StreetsParser.parse(file) do |street|
29
+ puts "#{street.feature} #{street.second_name} #{street.name} => #{street.uuid}, #{street.city_uuid}"
30
+ end
31
+ ```
32
+ ## Contributing
33
+
34
+ 1. Fork it ( http://github.com/<my-github-username>/gus-importer/fork )
35
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
36
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
37
+ 4. Push to the branch (`git push origin my-new-feature`)
38
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/gus-importer ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
4
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
5
+
6
+ require 'rubygems'
7
+ require 'thor'
8
+ require 'gus/importer'
9
+ require 'gus/cli'
10
+
11
+ Gus::Importer::Cli.start(ARGV)
@@ -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 'gus/importer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gus-importer"
8
+ spec.version = Gus::Importer::VERSION
9
+ spec.authors = ["macbury@gmail.com"]
10
+ spec.email = ["macbury@gmail.com"]
11
+ spec.summary = %q{Simple gem for extracting data from gus XML}
12
+ spec.description = %q{Simple gem for extracting data from gus XML}
13
+ spec.homepage = "https://github.com/macbury/gus-importer"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_dependency "libxml-ruby"
24
+ spec.add_dependency "thor"
25
+ spec.add_dependency "pry"
26
+ end
data/lib/gus/cli.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Gus::Importer
2
+
3
+ class Cli < Thor
4
+ desc "province file", ""
5
+ def province(file)
6
+ Gus::Importer::ProvinceParser.parse(file) do |name, uuid|
7
+ puts "#{name} => #{uuid}"
8
+ end
9
+ end
10
+
11
+ desc "streets file", ""
12
+ def streets(file)
13
+ Gus::Importer::StreetsParser.parse(file) do |street|
14
+ puts "#{street.feature} #{street.second_name} #{street.name} => #{street.uuid}, #{street.city_uuid}"
15
+ end
16
+ end
17
+ end
18
+
19
+ end
@@ -0,0 +1,57 @@
1
+ module Gus::Importer
2
+
3
+ class ProvinceParser
4
+ include LibXML::XML::SaxParser::Callbacks
5
+
6
+ def initialize(callback)
7
+ @callback = callback
8
+ end
9
+
10
+ def on_start_element(element, attributes)
11
+ @current_tag = element.to_sym
12
+
13
+ if @current_tag == :row
14
+ @name = ""
15
+ @uuid = ""
16
+ elsif @current_tag == :col
17
+ @current_attr_name = attributes["name"].to_sym
18
+ end
19
+ end
20
+
21
+ def on_cdata_block(cdata)
22
+ #puts "on on_cdata_block element: #{cdata}"
23
+ end
24
+
25
+ def on_characters(chars)
26
+ if @current_tag == :col
27
+ if @current_attr_name == :SYM
28
+ @uuid += chars
29
+ elsif @current_attr_name == :NAZWA
30
+ @name += chars
31
+ end
32
+ end
33
+ end
34
+
35
+ def on_end_element(element)
36
+ if element.to_sym == :row
37
+ @current_tag = nil
38
+
39
+ @callback.call(@name, @uuid)
40
+ @name = ""
41
+ @uuid = ""
42
+ end
43
+
44
+ if element.to_sym == :col
45
+ @current_attr_name = nil
46
+ end
47
+ end
48
+
49
+ def self.parse(file_path, &block)
50
+ parser = LibXML::XML::SaxParser.file(file_path)
51
+ parser.callbacks = ProvinceParser.new(block)
52
+ parser.parse
53
+ end
54
+
55
+ end
56
+
57
+ end
@@ -0,0 +1,85 @@
1
+ module Gus::Importer
2
+
3
+ class StreetsParser
4
+ include LibXML::XML::SaxParser::Callbacks
5
+
6
+ def initialize(callback)
7
+ @callback = callback
8
+ @street = nil
9
+ end
10
+
11
+ def on_start_element(element, attributes)
12
+ @current_tag = element.to_sym
13
+
14
+ if @current_tag == :row
15
+ @street = Street.new
16
+ elsif @current_tag == :col
17
+ @current_attr_name = attributes["name"].to_sym
18
+ end
19
+ end
20
+
21
+ def on_cdata_block(cdata)
22
+ #puts "on on_cdata_block element: #{cdata}"
23
+ end
24
+
25
+ def on_characters(chars)
26
+ if @current_tag == :col
27
+ if @current_attr_name == :CECHA
28
+ @street.feature += chars
29
+ elsif @current_attr_name == :SYM
30
+ @street.city_uuid += chars
31
+ elsif @current_attr_name == :SYM_UL
32
+ @street.uuid += chars
33
+ elsif @current_attr_name == :NAZWA_1
34
+ @street.name += chars
35
+ elsif @current_attr_name == :NAZWA_2
36
+ @street.second_name += chars
37
+ end
38
+ end
39
+ end
40
+
41
+ def on_end_element(element)
42
+ if element.to_sym == :row
43
+ @current_tag = nil
44
+
45
+ @callback.call(@street.get!)
46
+ @street = nil
47
+ end
48
+
49
+ if element.to_sym == :col
50
+ @current_attr_name = nil
51
+ end
52
+ end
53
+
54
+ def self.parse(file_path, &block)
55
+ parser = LibXML::XML::SaxParser.file(file_path)
56
+ parser.callbacks = StreetsParser.new(block)
57
+ parser.parse
58
+ end
59
+
60
+ end
61
+
62
+ class Street
63
+ attr_accessor :name, :uuid, :city_uuid, :feature, :second_name
64
+
65
+ def initialize
66
+ self.second_name = ""
67
+ self.name = ""
68
+ self.uuid = ""
69
+ self.city_uuid = ""
70
+ self.feature = ""
71
+ end
72
+
73
+ def get!
74
+ self.second_name.strip!
75
+ self.name.strip!
76
+ self.uuid.strip!
77
+ self.city_uuid.strip!
78
+ self.feature.strip!
79
+
80
+ self.uuid = self.uuid.to_i
81
+ self.city_uuid = self.city_uuid.to_i
82
+ self
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,5 @@
1
+ module Gus
2
+ module Importer
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ require "gus/importer/version"
2
+ require "uri"
3
+ require "tempfile"
4
+ require "net/http"
5
+ require "libxml"
6
+ require "gus/importer/province"
7
+ require "gus/importer/streets"
8
+ require "pry"
9
+ module Gus
10
+ module Importer
11
+
12
+ def self.download(url, to_file)
13
+ uri = URI.parse(url)
14
+
15
+ Net::HTTP.start(uri.host, uri.port) do |http|
16
+ request = Net::HTTP::Get.new uri.request_uri
17
+
18
+ http.request(request) do |response|
19
+ open(to_file, 'w') do |io|
20
+ response.read_body do |chunk|
21
+ io.write chunk
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ end
29
+ end
metadata ADDED
@@ -0,0 +1,127 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gus-importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - macbury@gmail.com
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: libxml-ruby
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ! '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Simple gem for extracting data from gus XML
84
+ email:
85
+ - macbury@gmail.com
86
+ executables:
87
+ - gus-importer
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - bin/gus-importer
97
+ - gus-importer.gemspec
98
+ - lib/gus/cli.rb
99
+ - lib/gus/importer.rb
100
+ - lib/gus/importer/province.rb
101
+ - lib/gus/importer/streets.rb
102
+ - lib/gus/importer/version.rb
103
+ homepage: https://github.com/macbury/gus-importer
104
+ licenses:
105
+ - MIT
106
+ metadata: {}
107
+ post_install_message:
108
+ rdoc_options: []
109
+ require_paths:
110
+ - lib
111
+ required_ruby_version: !ruby/object:Gem::Requirement
112
+ requirements:
113
+ - - ! '>='
114
+ - !ruby/object:Gem::Version
115
+ version: '0'
116
+ required_rubygems_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ requirements: []
122
+ rubyforge_project:
123
+ rubygems_version: 2.2.1
124
+ signing_key:
125
+ specification_version: 4
126
+ summary: Simple gem for extracting data from gus XML
127
+ test_files: []