Commit 55f13175 authored by Immanuel Häussermann's avatar Immanuel Häussermann
Browse files

initial commit

parents
Pipeline #20452 passed with stage
in 14 minutes and 33 seconds
/.bundle/
/.yardoc
/_yardoc/
/coverage/
/doc/
/pkg/
/spec/reports/
/tmp/
image: ruby:2.5
cache:
paths:
- vendor/
test:
stage: test
script:
- apt-get update -qy
- bundle install --path vendor
- bundle exec rake test
source "https://rubygems.org"
git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
# Specify your gem's dependencies in excelsior.gemspec
gemspec
PATH
remote: .
specs:
excelsior (0.1.0)
simple_xlsx_reader (~> 1.0.2)
GEM
remote: https://rubygems.org/
specs:
mini_portile2 (2.3.0)
minitest (5.11.3)
nokogiri (1.8.2)
mini_portile2 (~> 2.3.0)
rake (10.5.0)
rubyzip (1.2.1)
simple_xlsx_reader (1.0.2)
nokogiri
rubyzip
PLATFORMS
ruby
DEPENDENCIES
bundler (~> 1.16)
excelsior!
minitest (~> 5.0)
rake (~> 10.0)
BUNDLED WITH
1.16.1
The MIT License (MIT)
Copyright (c) 2018 Immanuel Häussermann
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
<img src="docs/excelsior-logo.png" width="480">
---
A straightforward way to import data from an excel sheet into your ruby app.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'excelsior'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install excelsior
## How to use it
Create a class which declares how columns from an excel sheet map to your ruby object model by extending from the `Excelsior::Importer` class:
```ruby
class UserImporter < Excelsior::Importer
# declare the source file
source "static/ftp/users.xlsx"
# declare the mapping
map "First Name", to: :firstname
map "Last Name", to: :lastname
map "E-Mail", to: :email
end
```
Then create an instance of your import and run it:
```ruby
import = UserImport.new # you may also pass a file per instance here
import.run do |row|
User.create!(row) # raise an exception if the data doesn't match your expectations
end
```
## Limitations
Be aware of a few limitations when considering this gem:
- only supports the first sheet in an excel file
- only supports `.xlsx` file format
- no export, just import
## Development
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.
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).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/manufaktor/excelsior.
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
require "bundler/gem_tasks"
require "rake/testtask"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb"]
end
task :default => :test
#!/usr/bin/env ruby
require "bundler/setup"
require "excelsior"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start
require "irb"
IRB.start(__FILE__)
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
set -vx
bundle install
# Do any other automated setup that you need to do here
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require "excelsior/version"
Gem::Specification.new do |spec|
spec.name = "excelsior"
spec.version = Excelsior::VERSION
spec.authors = ["Immanuel Häussermann"]
spec.email = ["hai@panter.ch"]
spec.summary = %q{Helps you import data from an excel sheet}
spec.description = %q{Provides a concise DSL to map, validate and import data from an excel sheet into your ruby app}
spec.homepage = "http://github.com/manufaktor/excelsior"
spec.license = "MIT"
spec.files = `git ls-files -z`.split("\x0").reject do |f|
f.match(%r{^(test|spec|features)/})
end
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]
spec.add_development_dependency "bundler", "~> 1.16"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_runtime_dependency "simple_xlsx_reader", "~> 1.0.2"
end
require "excelsior/version"
module Excelsior
end
require "simple_xlsx_reader"
require "excelsior/source"
require "excelsior/mapping"
module Excelsior
class Import
include Source
include Mapping
attr_accessor :source, :fields, :errors
attr_accessor :rows, :columns
def initialize(file = nil)
self.source = file || self.class.source_file
self.fields = self.class.fields
@doc = ::SimpleXlsxReader.open(self.source)
@sheet = @doc.sheets.first
@columns = @sheet.rows.shift
@rows = @sheet.rows
valid?
end
def run(&block)
@rows.map do |row|
yield map_row_values(row, @columns)
end
end
def valid?
@errors = fields.to_a.reduce({}) do |acc, f|
acc[:missing_column] ||= []
unless @columns.include?(f[:header])
acc[:missing_column] << { missing: f[:header] }
end
acc
end
end
end
end
module Excelsior
module Mapping
def self.included(host_class)
host_class.extend ClassMethods
end
module ClassMethods
attr_reader :fields
def map(header, options = {})
@fields ||= []
@fields << {
attribute: options.fetch(:to),
header: header
}
end
end
def map_row_values(row, columns)
@fields.to_a.reduce({}) do |acc, field|
idx = columns.index(field[:header])
acc[field[:attribute]] = row[idx]
acc
end
end
end
end
module Excelsior
module Source
def self.included(host_class)
host_class.extend ClassMethods
end
module ClassMethods
attr_accessor :source_file
def source(file)
self.source_file = file
end
end
end
end
module Excelsior
VERSION = "0.1.0"
end
require "test_helper"
require "excelsior/import"
class UserImport < Excelsior::Import
source "test/files/complete.xlsx"
map "Vorname", to: :firstname
map "Nachname", to: :lastname
map "E-Mail", to: :email
end
class ExcelsiorTest < Minitest::Test
def setup
@import = UserImport.new
end
def test_that_it_has_a_version_number
refute_nil ::Excelsior::VERSION
end
def test_class_level_source
import = UserImport.new
assert_equal "test/files/complete.xlsx", import.source
end
def test_instance_level_source
import = UserImport.new("test/files/missing-column.xlsx")
assert_equal "test/files/missing-column.xlsx", import.source
end
def test_import_rows
assert_equal 2, @import.rows.length
end
def test_import_columns
assert_equal 3, @import.columns.length
assert_equal "E-Mail", @import.columns[0]
assert_equal "Vorname", @import.columns[1]
assert_equal "Nachname", @import.columns[2]
end
def test_mapping
assert_equal @import.fields, [
{attribute: :firstname, header: "Vorname"},
{attribute: :lastname, header: "Nachname"},
{attribute: :email, header: "E-Mail"}
]
end
def test_import_run
results = @import.run { |v| v }
assert_equal results[0], {
firstname: "Hans",
lastname: "Müller",
email: "hans@mueller.com"
}
assert_equal results[1], {
firstname: "Jögi",
lastname: "Brunz",
email: "jb@runz.com"
}
end
def test_validations
import = UserImport.new("test/files/missing-column.xlsx")
assert import.errors[:missing_column].any?
end
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment