data_imp 0.1.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 +14 -0
- data/.rspec +2 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +90 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/data/hero.json +1 -0
- data/data/hero.yaml +4 -0
- data/data/heros.csv +2 -0
- data/data/heros.json +1 -0
- data/data/heros.ods +0 -0
- data/data/heros.xls +0 -0
- data/data/heros.xlsx +0 -0
- data/data/heros.yaml +4 -0
- data/data_imp.gemspec +30 -0
- data/lib/data_imp.rb +70 -0
- data/lib/data_imp/class_methods.rb +63 -0
- data/lib/data_imp/no_importer.rb +11 -0
- data/lib/data_imp/no_parser.rb +11 -0
- data/lib/data_imp/parser.rb +41 -0
- data/lib/data_imp/parser/csv.rb +16 -0
- data/lib/data_imp/parser/json.rb +7 -0
- data/lib/data_imp/parser/ods.rb +7 -0
- data/lib/data_imp/parser/spreadsheet.rb +20 -0
- data/lib/data_imp/parser/stream.rb +16 -0
- data/lib/data_imp/parser/xls.rb +7 -0
- data/lib/data_imp/parser/xlsx.rb +6 -0
- data/lib/data_imp/parser/yaml.rb +7 -0
- data/lib/data_imp/porter.rb +63 -0
- data/lib/data_imp/porter/sample.rb +2 -0
- data/lib/data_imp/version.rb +3 -0
- metadata +163 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 86e11f4b6817ad75532ca9af858c261295516645
|
4
|
+
data.tar.gz: 7cdd8b503afa39ce898eaf8a77a5034f86af448e
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 84f36d210ff7c43709501f10eed5289e2f7d4d965c2cb1fcfb0edca5134e2af08e5a0821b307ef22ee20c414e43e73398e26879b773ab2cbef33d9f7af7dba91
|
7
|
+
data.tar.gz: 9735f74a69ccee71adb71b3b4bb28273525cbad80eb7158f337f7357a38bd83d413cc920b95ceea2fb2adf9157ee3d06f3af7a6e6aad98229b89cccc40fbbac0
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at robert.w.ferney@uscis.dhs.gov. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Robert W. Ferney
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
# Data Imp Porter
|
2
|
+
|
3
|
+
## Installation
|
4
|
+
|
5
|
+
Add this line to your application's Gemfile:
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
gem 'data_imp'
|
9
|
+
```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install data_imp
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
The use of this gem requires that you define an 'importer' for your files.
|
22
|
+
|
23
|
+
the fields from your data will be available to the importer as if they were local variables.
|
24
|
+
```ruby
|
25
|
+
class SampleImporter < DataImp::Porter
|
26
|
+
def import
|
27
|
+
User.create_with(
|
28
|
+
name: name,
|
29
|
+
).find_or_create_by(
|
30
|
+
email: email
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
```
|
35
|
+
|
36
|
+
If you had your users in a csv file like
|
37
|
+
|
38
|
+
```csv
|
39
|
+
name,email
|
40
|
+
George,george@example.com
|
41
|
+
Fred,fred@example.net
|
42
|
+
```
|
43
|
+
|
44
|
+
You could then use
|
45
|
+
```ruby
|
46
|
+
DataImp.import 'user.csv'
|
47
|
+
```
|
48
|
+
|
49
|
+
If you had your users in a yaml file like
|
50
|
+
|
51
|
+
```yaml
|
52
|
+
---
|
53
|
+
- name: George
|
54
|
+
email: george@example.com
|
55
|
+
- name: Fred
|
56
|
+
email: fred@example.net
|
57
|
+
```
|
58
|
+
|
59
|
+
You could then use
|
60
|
+
```ruby
|
61
|
+
DataImp.import 'user.yaml'
|
62
|
+
```
|
63
|
+
|
64
|
+
The same importer would be used for either example.
|
65
|
+
|
66
|
+
The importer is looked up by the base name, or provided by the `importer:` parameter. . 'user' in this case.
|
67
|
+
|
68
|
+
Parsing of the file is done by a Parser class and is looked up by the file extension, or the `parser:` parameter.
|
69
|
+
|
70
|
+
Pre defined parsers are located in the lib/data_imp/parsers/ folder
|
71
|
+
|
72
|
+
If you need to write your own, define it as 'class ExtParser < DataImp::Parser' to create or override the 'ext' extension parser.
|
73
|
+
|
74
|
+
## Development
|
75
|
+
|
76
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
77
|
+
|
78
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
79
|
+
|
80
|
+
## Contributing
|
81
|
+
|
82
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/data-porter. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
83
|
+
|
84
|
+
## License
|
85
|
+
|
86
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
87
|
+
|
88
|
+
## Code of Conduct
|
89
|
+
|
90
|
+
Everyone interacting in the Data::Porter project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/data-porter/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "data/porter"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/data/hero.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"name":"George","rank":"Sargent","serial_number":9991212}
|
data/data/hero.yaml
ADDED
data/data/heros.csv
ADDED
data/data/heros.json
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
[{"name":"George","rank":"Sargent","serial_number":9991212}]
|
data/data/heros.ods
ADDED
Binary file
|
data/data/heros.xls
ADDED
Binary file
|
data/data/heros.xlsx
ADDED
Binary file
|
data/data/heros.yaml
ADDED
data/data_imp.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "data_imp/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "data_imp"
|
8
|
+
spec.version = DataImp::VERSION
|
9
|
+
spec.authors = ["Robert W. Ferney"]
|
10
|
+
spec.email = ["rob@ferney.org"]
|
11
|
+
|
12
|
+
spec.summary = %q{Scaffolding to make importing data easy}
|
13
|
+
spec.homepage = "https://github.com/capnregex/data_imp"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = "exe"
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ["lib"]
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'activesupport'
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.14.6"
|
26
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
27
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
28
|
+
spec.add_development_dependency "roo" #, "~> 3.0"
|
29
|
+
spec.add_development_dependency "roo-xls" #, "~> 3.0"
|
30
|
+
end
|
data/lib/data_imp.rb
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
|
2
|
+
require 'active_support/core_ext/string'
|
3
|
+
require_relative "data_imp/version"
|
4
|
+
require_relative "data_imp/class_methods"
|
5
|
+
|
6
|
+
# Dir[File.dirname(__FILE__) + '/data_imp/*.rb'].each {|file| require file }
|
7
|
+
class DataImp
|
8
|
+
extend DataImp::ClassMethods
|
9
|
+
attr_accessor :file, :parser, :importer, :extname, :basename
|
10
|
+
|
11
|
+
def initialize file = nil, parser: nil, importer: nil
|
12
|
+
self.file = data_dir.join(file)
|
13
|
+
extname = File.extname(file)
|
14
|
+
self.basename ||= File.basename(file, extname)
|
15
|
+
self.extname = extname = extname.downcase.sub('.','')
|
16
|
+
self.importer = find_importer(importer || basename) # returns class
|
17
|
+
self.parser = find_parser(parser || extname) # returns class
|
18
|
+
end
|
19
|
+
|
20
|
+
def import
|
21
|
+
puts "Importing #{basename} with #{importer}"
|
22
|
+
importer.before_all_imports
|
23
|
+
parser.new(file).process_file do |hash, index|
|
24
|
+
porter = importer.new(hash,index)
|
25
|
+
begin
|
26
|
+
porter.before_import
|
27
|
+
porter.import
|
28
|
+
porter.after_import
|
29
|
+
show_progress index
|
30
|
+
rescue StandardError => e
|
31
|
+
warn "#{basename}:#{index}:#{e.class.name}"
|
32
|
+
porter.on_error e
|
33
|
+
end
|
34
|
+
end
|
35
|
+
importer.after_all_imports
|
36
|
+
puts
|
37
|
+
end
|
38
|
+
|
39
|
+
def show_progress index
|
40
|
+
puts if index % 10_000 == 0
|
41
|
+
print '.' if index % 100 == 0
|
42
|
+
end
|
43
|
+
|
44
|
+
# def import_file file
|
45
|
+
#
|
46
|
+
# parser_class_name = ext.classify
|
47
|
+
# importer_class_name = base.concat('_importer').classify
|
48
|
+
# file = data_dir.join(file)
|
49
|
+
#
|
50
|
+
# begin
|
51
|
+
# parser = const_get(parser_class_name)
|
52
|
+
# importer = importer_class_name.constantize
|
53
|
+
# rescue NameError => e
|
54
|
+
# warn "No importer class: #{e}"
|
55
|
+
# return
|
56
|
+
# end
|
57
|
+
# parser.new(file, importer).import
|
58
|
+
# end
|
59
|
+
|
60
|
+
# def reader= reader
|
61
|
+
# return unless reader
|
62
|
+
# @reader =
|
63
|
+
# case reader
|
64
|
+
# when IO,DataImp::Reader then reader
|
65
|
+
# when String then open(reader)
|
66
|
+
# when Pathname then reader.open
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
end
|
70
|
+
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
|
3
|
+
require_relative "parser"
|
4
|
+
require_relative "porter"
|
5
|
+
|
6
|
+
module DataImp::ClassMethods
|
7
|
+
def self.extended(mod)
|
8
|
+
mod.delegate :data_dir, :find_parser, :find_importer, to: :class
|
9
|
+
end
|
10
|
+
|
11
|
+
def root= dir
|
12
|
+
@root = dir
|
13
|
+
end
|
14
|
+
|
15
|
+
def root
|
16
|
+
@root ||= Pathname.pwd
|
17
|
+
end
|
18
|
+
|
19
|
+
def data_dir= dir
|
20
|
+
path = Pathname.new(dir)
|
21
|
+
@data_dir =
|
22
|
+
if path.absolute?
|
23
|
+
path
|
24
|
+
else
|
25
|
+
root.join(path)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def data_dir
|
30
|
+
@data_dir ||= root.join('data')
|
31
|
+
end
|
32
|
+
|
33
|
+
def import(file=nil,*args,&block)
|
34
|
+
file.strip!
|
35
|
+
return if file =~ /^#/
|
36
|
+
new(file, *args,&block).import
|
37
|
+
end
|
38
|
+
|
39
|
+
def import_list *args
|
40
|
+
args.each do |arg|
|
41
|
+
arg.each_line do |file|
|
42
|
+
import file
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def find_parser type
|
48
|
+
return type if type.kind_of? DataImp::Parser
|
49
|
+
type = type.to_s.camelize
|
50
|
+
const_get "#{type}Parser"
|
51
|
+
rescue NameError => e
|
52
|
+
DataImp::Parser.find_parser type
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_importer type
|
56
|
+
return type if type.kind_of? DataImp::Porter
|
57
|
+
type = type.to_s.camelize
|
58
|
+
const_get "#{type}Importer"
|
59
|
+
rescue NameError => e
|
60
|
+
DataImp::Porter.find_importer type
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class DataImp::NoImporter < StandardError
|
2
|
+
attr_accessor :porter_type
|
3
|
+
def initialize(porter_type)
|
4
|
+
@porter_type = porter_type
|
5
|
+
super <<-MSG.strip_heredoc
|
6
|
+
Unable to find Importer for #{porter_type.inspect}
|
7
|
+
Please define: class #{porter_type.classify}Importer < DataImp::Porter
|
8
|
+
MSG
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class DataImp::NoParser < StandardError
|
2
|
+
attr_accessor :parser_type
|
3
|
+
def initialize(parser_type)
|
4
|
+
@parser_type = parser_type
|
5
|
+
super <<-MSG.strip_heredoc
|
6
|
+
Unable to find Parser for #{parser_type.inspect}
|
7
|
+
Please define: class #{parser_type.camelize}Parser < DataImp::Parser
|
8
|
+
MSG
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require_relative 'no_parser'
|
2
|
+
class DataImp::Parser
|
3
|
+
attr_reader :filename
|
4
|
+
|
5
|
+
def initialize filename=nil
|
6
|
+
@filename = filename
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.find_parser type
|
10
|
+
return self if type.blank?
|
11
|
+
begin
|
12
|
+
const_get type.camelize
|
13
|
+
rescue NameError => e
|
14
|
+
if require_relative "parser/#{type.underscore}"
|
15
|
+
retry
|
16
|
+
end
|
17
|
+
end
|
18
|
+
rescue LoadError => e
|
19
|
+
raise DataImp::NoParser.new(type)
|
20
|
+
end
|
21
|
+
|
22
|
+
def parse chunk
|
23
|
+
chunk # assume chunk is a hash
|
24
|
+
end
|
25
|
+
|
26
|
+
def process input, &block
|
27
|
+
index = 1
|
28
|
+
input.each do |chunk|
|
29
|
+
hash = parse(chunk)
|
30
|
+
yield hash, index
|
31
|
+
index += 1
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def process_file &block
|
36
|
+
open(filename) do |file|
|
37
|
+
process file, &block
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'csv'
|
2
|
+
class DataImp::Parser::Csv < DataImp::Parser
|
3
|
+
def options
|
4
|
+
{ headers: true }
|
5
|
+
end
|
6
|
+
|
7
|
+
def parse chunk
|
8
|
+
chunk.to_h
|
9
|
+
end
|
10
|
+
|
11
|
+
def process_file &block
|
12
|
+
CSV.open(filename, options) do |csv|
|
13
|
+
process csv, &block
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'roo'
|
2
|
+
class DataImp::Parser::Spreadsheet < DataImp::Parser
|
3
|
+
def options
|
4
|
+
{ }
|
5
|
+
end
|
6
|
+
|
7
|
+
def process_file &block
|
8
|
+
spreadsheet = Roo::Spreadsheet.open(filename, options)
|
9
|
+
header = spreadsheet.row(1)
|
10
|
+
(2..spreadsheet.last_row).each do |index|
|
11
|
+
row = spreadsheet.row(index)
|
12
|
+
hash = [header, row].transpose.to_h
|
13
|
+
yield hash, index
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def process input, &block
|
18
|
+
raise "unable to process spreadsheet as a stream"
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
class DataImp::Parser::Stream < DataImp::Parser
|
2
|
+
def process input
|
3
|
+
if input.respond_to? :read
|
4
|
+
input = input.read
|
5
|
+
end
|
6
|
+
data = process_string(input)
|
7
|
+
case data
|
8
|
+
when Hash
|
9
|
+
yield data, 0
|
10
|
+
when Array
|
11
|
+
data.each_with_index do |node, index|
|
12
|
+
yield node, index
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_support/hash_with_indifferent_access'
|
2
|
+
require_relative 'no_importer'
|
3
|
+
class DataImp::Porter
|
4
|
+
attr_reader :hash, :index
|
5
|
+
|
6
|
+
def self.find_importer type
|
7
|
+
return self if type.blank?
|
8
|
+
begin
|
9
|
+
const_get type.classify
|
10
|
+
rescue NameError => e
|
11
|
+
if require_relative "porter/#{type.underscore}"
|
12
|
+
retry
|
13
|
+
end
|
14
|
+
end
|
15
|
+
rescue LoadError => e
|
16
|
+
raise DataImp::NoImporter.new(type)
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(hash, index = nil)
|
20
|
+
@hash = HashWithIndifferentAccess.new(hash)
|
21
|
+
@index = index
|
22
|
+
end
|
23
|
+
|
24
|
+
def respond_to_missing?(method, include_private = false)
|
25
|
+
hash.has_key?(method) || false # super
|
26
|
+
end
|
27
|
+
|
28
|
+
def method_missing(method, *_args)
|
29
|
+
return nil unless respond_to_missing?(method)
|
30
|
+
value = hash[method]
|
31
|
+
parse(method,value)
|
32
|
+
end
|
33
|
+
|
34
|
+
def parse method, value
|
35
|
+
value # override for special handling
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.before_all_imports
|
39
|
+
# called before any data is processed
|
40
|
+
end
|
41
|
+
|
42
|
+
def before_import
|
43
|
+
# called for a record before import
|
44
|
+
end
|
45
|
+
|
46
|
+
def import
|
47
|
+
# called to import a record
|
48
|
+
warn "Import: #{self.class.name}:#{index}:#{hash.inspect}"
|
49
|
+
end
|
50
|
+
|
51
|
+
def after_import
|
52
|
+
# called after a record was imported
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.after_all_imports
|
56
|
+
# called after all data is processed
|
57
|
+
end
|
58
|
+
|
59
|
+
def on_error e
|
60
|
+
warn "Error: #{self.class.name}:#{index}:#{hash.inspect}"
|
61
|
+
warn e
|
62
|
+
end
|
63
|
+
end
|
metadata
ADDED
@@ -0,0 +1,163 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: data_imp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robert W. Ferney
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-09-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.14.6
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.14.6
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: roo
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: roo-xls
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- rob@ferney.org
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- data/hero.json
|
115
|
+
- data/hero.yaml
|
116
|
+
- data/heros.csv
|
117
|
+
- data/heros.json
|
118
|
+
- data/heros.ods
|
119
|
+
- data/heros.xls
|
120
|
+
- data/heros.xlsx
|
121
|
+
- data/heros.yaml
|
122
|
+
- data_imp.gemspec
|
123
|
+
- lib/data_imp.rb
|
124
|
+
- lib/data_imp/class_methods.rb
|
125
|
+
- lib/data_imp/no_importer.rb
|
126
|
+
- lib/data_imp/no_parser.rb
|
127
|
+
- lib/data_imp/parser.rb
|
128
|
+
- lib/data_imp/parser/csv.rb
|
129
|
+
- lib/data_imp/parser/json.rb
|
130
|
+
- lib/data_imp/parser/ods.rb
|
131
|
+
- lib/data_imp/parser/spreadsheet.rb
|
132
|
+
- lib/data_imp/parser/stream.rb
|
133
|
+
- lib/data_imp/parser/xls.rb
|
134
|
+
- lib/data_imp/parser/xlsx.rb
|
135
|
+
- lib/data_imp/parser/yaml.rb
|
136
|
+
- lib/data_imp/porter.rb
|
137
|
+
- lib/data_imp/porter/sample.rb
|
138
|
+
- lib/data_imp/version.rb
|
139
|
+
homepage: https://github.com/capnregex/data_imp
|
140
|
+
licenses:
|
141
|
+
- MIT
|
142
|
+
metadata: {}
|
143
|
+
post_install_message:
|
144
|
+
rdoc_options: []
|
145
|
+
require_paths:
|
146
|
+
- lib
|
147
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
148
|
+
requirements:
|
149
|
+
- - ">="
|
150
|
+
- !ruby/object:Gem::Version
|
151
|
+
version: '0'
|
152
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
|
+
requirements:
|
154
|
+
- - ">="
|
155
|
+
- !ruby/object:Gem::Version
|
156
|
+
version: '0'
|
157
|
+
requirements: []
|
158
|
+
rubyforge_project:
|
159
|
+
rubygems_version: 2.5.1
|
160
|
+
signing_key:
|
161
|
+
specification_version: 4
|
162
|
+
summary: Scaffolding to make importing data easy
|
163
|
+
test_files: []
|