smart_importer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +73 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/smart_importer.rb +49 -0
- data/lib/smart_importer/spreadsheet.rb +51 -0
- data/lib/smart_importer/version.rb +3 -0
- data/smart_importer.gemspec +35 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: b5cacfb4ca850a32db12dbd78bce37f1e42af662
|
4
|
+
data.tar.gz: 910e7c20279f423754cc872a5c3f1f951079e431
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3681792da16c26ebe66ca89fed2b9ccda78910694548cefd870d669f43dca6e74a02f53e6f60d094dc08a20504c14b6a06c7038ef33918295fc02105a4dd55ed
|
7
|
+
data.tar.gz: 13bf729b0ddaee796b9b82c53bd582a55a33eec17c81f1329e57c3ab45544b6eb880617e2d3bf382fbc81395ccfce0319b43e0db0cca8753223df439f4a3a13f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at TODO: Write your email address. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 TODO: Write your name
|
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,73 @@
|
|
1
|
+
# SmartImporter
|
2
|
+
|
3
|
+
Smart importer allows you to import relevant data from spreadsheets (xlsx or csv) to any of your models with one line of code.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'smart_importer'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install smart_importer
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To import data from a spreadsheet to one of your models, first initiate the Importer object. For example:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
importer = SmartImporter::Importer.new(file_path: "/path/to/file", model: User, key_attribute: :name)
|
27
|
+
```
|
28
|
+
|
29
|
+
Where:
|
30
|
+
:file_path => your file_path
|
31
|
+
:model => the active_record model that you want to fill with relevant data from.
|
32
|
+
:key_attribute => is an optional parameter that you are telling the importer must be unique within all records (Can be useful in some cases although your model validations should normally take care of this.)
|
33
|
+
|
34
|
+
and then,
|
35
|
+
|
36
|
+
To import all data in sheets from the spreadsheet:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
importer.import_all
|
40
|
+
```
|
41
|
+
or
|
42
|
+
|
43
|
+
To import all data in sheets from the nth spreadsheet:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
importer.import_sheet(n)
|
47
|
+
```
|
48
|
+
or
|
49
|
+
|
50
|
+
To import all data in sheets from the an array or range array_of_sheets:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
importer.import_sheets(array_of_sheets)
|
54
|
+
```
|
55
|
+
|
56
|
+
Et Voila!! Smart Importer should have found all relevant fields in the spreadsheet and added it to your database.
|
57
|
+
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
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).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smart_importer. 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.
|
68
|
+
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
73
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "smart_importer"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require "smart_importer/version"
|
2
|
+
require "smart_importer/spreadsheet"
|
3
|
+
require 'rubygems'
|
4
|
+
require "roo"
|
5
|
+
require "logger"
|
6
|
+
require "active_support/all"
|
7
|
+
|
8
|
+
module SmartImporter
|
9
|
+
class Importer
|
10
|
+
def initialize(file_path:, model:, key_attribute: id)
|
11
|
+
@file_path = file_path
|
12
|
+
@model = model
|
13
|
+
@xlsx = Roo::Spreadsheet.open(@file_path.to_s)
|
14
|
+
@number_of_imported_records = 0
|
15
|
+
@key_attribute = key_attribute
|
16
|
+
end
|
17
|
+
|
18
|
+
def import_all
|
19
|
+
import_sheets(1..@xlsx.sheets.count-1)
|
20
|
+
end
|
21
|
+
|
22
|
+
def import_sheet(sheet)
|
23
|
+
import_sheets(Array(sheet))
|
24
|
+
end
|
25
|
+
|
26
|
+
def import_sheets(sheet_array)
|
27
|
+
raise 'Import sheets expects an array of sheet numbers.' unless sheet_array.to_a.all? {|i| i.is_a?(Integer) }
|
28
|
+
logger = Logger.new(STDOUT)
|
29
|
+
logger.info "Importing #{@entity_type.to_s.underscore.pluralize} from #{@file_path}..."
|
30
|
+
begin
|
31
|
+
import_these_sheets sheet_array
|
32
|
+
rescue => exception
|
33
|
+
logger.error "Failed to import #{@entity_type} because #{exception}"
|
34
|
+
else
|
35
|
+
logger.info "Done importing. Imported #{@number_of_imported_records} #{@entity_type.to_s.pluralize}."
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def import_these_sheets(sheet_array)
|
42
|
+
@xlsx.sheets.each do |sheet|
|
43
|
+
@xlsx.default_sheet = sheet
|
44
|
+
spreadsheet = Spreadsheet.new(sheet: @xlsx, model: @model, key_attribute: @key_attribute)
|
45
|
+
@number_of_imported_records += spreadsheet.import_objects
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module SmartImporter
|
2
|
+
class Spreadsheet
|
3
|
+
def initialize(sheet:, model:, key_attribute:)
|
4
|
+
@sheet = sheet
|
5
|
+
@key_attribute = key_attribute
|
6
|
+
@number_imports = 0
|
7
|
+
@looking_at_row = 1
|
8
|
+
@model = model
|
9
|
+
end
|
10
|
+
|
11
|
+
def import_objects(added_attributes: {})
|
12
|
+
((right_header_row + 1)..@sheet.last_row).each do |i|
|
13
|
+
unless @sheet.row(i).empty?
|
14
|
+
row = Hash[[@sheet.row(right_header_row), @sheet.row(i)].transpose]
|
15
|
+
if active_record = @model.where(name: row['name']).try(:first)
|
16
|
+
active_record.update(valid_attributes(row))
|
17
|
+
else
|
18
|
+
@model.create(valid_attributes(row))
|
19
|
+
end
|
20
|
+
end
|
21
|
+
@number_imports += 1
|
22
|
+
end
|
23
|
+
@number_imports
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def right_header_row
|
29
|
+
until valid_header_line?
|
30
|
+
@looking_at_row += 1
|
31
|
+
raise "#{@sheet.last_row} sheet does not appear to have any relevant information." if sheet_is_invalid?
|
32
|
+
end
|
33
|
+
@looking_at_row
|
34
|
+
end
|
35
|
+
|
36
|
+
def sheet_is_invalid?
|
37
|
+
@looking_at_row == 10
|
38
|
+
end
|
39
|
+
|
40
|
+
def valid_header_line?
|
41
|
+
@sheet.row(@looking_at_row).any?{|cell| @model.column_names.include?(cell.to_s.underscore.try(:to_sym))}
|
42
|
+
end
|
43
|
+
|
44
|
+
def valid_attributes(row)
|
45
|
+
formatted_row = {}
|
46
|
+
row.each {|key, value| formatted_row[key.downcase.gsub(' ', '_').to_sym] = value if key.present? }
|
47
|
+
attribute_keys = @model.column_names.collect(&:to_sym) & formatted_row.keys
|
48
|
+
formatted_row.with_indifferent_access.slice(*attribute_keys) || {}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'smart_importer/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "smart_importer"
|
8
|
+
spec.version = SmartImporter::VERSION
|
9
|
+
spec.authors = ["Daniel Fugere"]
|
10
|
+
spec.email = ["danielfugere28@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Smart Importer allows you to import relevant data from spreadsheets (xlsx or csv) to any of your models with one line of code.}
|
13
|
+
spec.description = %q{Smart Importer allows you to import relevant data from spreadsheets (xlsx or csv) to any of your models with one line of code.}
|
14
|
+
spec.homepage = "https://github.com/dafuga/smart_importer"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
|
18
|
+
# delete this section to allow pushing this gem to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
spec.metadata['allowed_push_host'] = "https://rubygems.org"
|
21
|
+
else
|
22
|
+
raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_dependency 'roo', '~> 2.3'
|
31
|
+
spec.add_dependency 'activesupport', '~> 4.2'
|
32
|
+
spec.add_development_dependency "bundler", "~> 1.11"
|
33
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
34
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
35
|
+
end
|
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smart_importer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Daniel Fugere
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: roo
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.3'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.2'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.11'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.11'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
description: Smart Importer allows you to import relevant data from spreadsheets (xlsx
|
84
|
+
or csv) to any of your models with one line of code.
|
85
|
+
email:
|
86
|
+
- danielfugere28@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- lib/smart_importer.rb
|
101
|
+
- lib/smart_importer/spreadsheet.rb
|
102
|
+
- lib/smart_importer/version.rb
|
103
|
+
- smart_importer.gemspec
|
104
|
+
homepage: https://github.com/dafuga/smart_importer
|
105
|
+
licenses:
|
106
|
+
- MIT
|
107
|
+
metadata:
|
108
|
+
allowed_push_host: https://rubygems.org
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.5.0
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Smart Importer allows you to import relevant data from spreadsheets (xlsx
|
129
|
+
or csv) to any of your models with one line of code.
|
130
|
+
test_files: []
|