spreadsheet_importer 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.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Nicholas Jakobsen
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,3 @@
1
+ = SpreadsheetImporter
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'SpreadsheetImporter'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,54 @@
1
+ module SpreadsheetImporter
2
+ module Import
3
+ def self.from_xlsx(file_path, options = {}, &block)
4
+ options = {:sheet_name => nil}.merge(options)
5
+
6
+ spreadsheet = []
7
+ Roo::Excelx.new(file_path, :file_warning => :ignore).each_with_pagename do |name, sheet|
8
+ spreadsheet.concat sheet.to_a unless options[:sheet_name] && name.downcase.strip != options[:sheet_name].downcase.strip
9
+ end
10
+ from_spreadsheet(spreadsheet, options, &block)
11
+ end
12
+
13
+ def self.from_csv(file_path, options = {}, &block)
14
+ # Detect the encoding of the file and normalize it to UTF-8
15
+ csv = File.read(file_path)
16
+ encoding = CharlockHolmes::EncodingDetector.detect(csv)[:encoding]
17
+ csv = CharlockHolmes::Converter.convert csv, encoding, 'UTF-8'
18
+
19
+ # Get rid of the UTF-16LE BOM since Charlock doesn't do this for us
20
+ csv.slice!(0) if csv[0].ord == 65279
21
+
22
+ # Determine whether the column separator is a tab or comma
23
+ col_sep = csv.count("\t") > 0 ? "\t" : ","
24
+
25
+ spreadsheet = CSV.parse(csv, :col_sep => col_sep, :headers => true, :header_converters => :downcase)
26
+ from_spreadsheet(spreadsheet, options, &block)
27
+ end
28
+
29
+ def self.from_spreadsheet(spreadsheet, options = {}, &block)
30
+ options = {:start_row => 1, :schema => nil}.merge(options)
31
+
32
+ (options[:start_row] - 1).times { spreadsheet.shift } # Remove intro rows
33
+ spreadsheet = options[:schema].conform(spreadsheet) if options[:schema] # If a Conformist schema is provided, use that to prepare rows
34
+
35
+ errors = []
36
+ rowcount = 0
37
+ spreadsheet.each do |row|
38
+ rowcount += 1
39
+ begin
40
+ block.call(row)
41
+ print '.'
42
+ rescue => e
43
+ progress_indicator = '!'
44
+ errors << "Row #{rowcount}: #{e.message}"
45
+ print '!'
46
+ end
47
+ end
48
+
49
+ rowcount += options[:start_row] if rowcount > 0
50
+
51
+ return {:imported => rowcount - errors.count, :errors => errors, :total => rowcount}
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,3 @@
1
+ module SpreadsheetImporter
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,10 @@
1
+ # GEMS
2
+ require 'roo'
3
+ require 'conformist'
4
+ require 'charlock_holmes'
5
+
6
+ # LIB
7
+ require_relative "spreadsheet_importer/import"
8
+
9
+ module SpreadsheetImporter
10
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: spreadsheet_importer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Nicholas Jakobsen
9
+ - Ryan Wallace
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2015-05-19 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: culturecode-roo
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - "~>"
21
+ - !ruby/object:Gem::Version
22
+ version: 2.0.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: 2.0.2
31
+ - !ruby/object:Gem::Dependency
32
+ name: charlock_holmes
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ">="
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :runtime
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ description: Makes it easy to import spreadsheets. Handles .csv and .xlsx formats
48
+ as well as raw 2D arrays
49
+ email:
50
+ - contact@culturecode.ca
51
+ executables: []
52
+ extensions: []
53
+ extra_rdoc_files: []
54
+ files:
55
+ - lib/spreadsheet_importer/import.rb
56
+ - lib/spreadsheet_importer/version.rb
57
+ - lib/spreadsheet_importer.rb
58
+ - MIT-LICENSE
59
+ - Rakefile
60
+ - README.md
61
+ homepage: https://github.com/culturecode/spreadsheet_importer
62
+ licenses:
63
+ - MIT
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.25
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: Makes it easy to import spreadsheets.
86
+ test_files: []