table_importer 0.0.5 → 0.0.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Guardfile +7 -0
- data/README.md +13 -1
- data/lib/table_importer/copy_and_paste.rb +2 -2
- data/lib/table_importer/csv.rb +5 -5
- data/lib/table_importer/excel.rb +4 -4
- data/lib/table_importer/exceptions.rb +1 -1
- data/lib/table_importer/source.rb +5 -4
- data/lib/table_importer/version.rb +1 -1
- data/spec/sources/copy_and_paste_spec.rb +1 -1
- data/spec/sources/csv_spec.rb +1 -1
- data/spec/sources/excel_spec.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7c5fa5aec10db70b4b8391d69151a1da513f755
|
4
|
+
data.tar.gz: f9664947964a4be3e9921c3e04639af19733eed5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa51c6c72fd292b64d0c539f28d86c04608952c3c00701048093d82b881d27e81a408e191011de393e6e948eaabf68bdfac0bc2d29139adc3e3f0a2e75c2253d
|
7
|
+
data.tar.gz: 87c4d41ce3edd2b0b68c0bd811a6120fc020dd034f1b8e7aa84d9e3b0bfee97c209a6c7a86f6aa4b36647c8797e4695224a2c7fdcefdb721dc81789488124b94
|
data/Guardfile
ADDED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
[![Gem Version](https://badge.fury.io/rb/table_importer.svg)](http://badge.fury.io/rb/table_importer) [![Build Status](https://travis-ci.org/pressdoc/table_importer.svg?branch=master)](https://travis-ci.org/pressdoc/table_importer) [![Coverage Status](https://coveralls.io/repos/pressdoc/table_importer/badge.png?branch=master)](https://coveralls.io/r/pressdoc/table_importer?branch=master)
|
1
|
+
[![Gem Version](https://badge.fury.io/rb/table_importer.svg)](http://badge.fury.io/rb/table_importer) [![Build Status](https://travis-ci.org/pressdoc/table_importer.svg?branch=master)](https://travis-ci.org/pressdoc/table_importer) [![Coverage Status](https://coveralls.io/repos/pressdoc/table_importer/badge.png?branch=master)](https://coveralls.io/r/pressdoc/table_importer?branch=master) [![Code Climate](https://codeclimate.com/github/pressdoc/table_importer/badges/gpa.svg)](https://codeclimate.com/github/pressdoc/table_importer)
|
2
2
|
|
3
3
|
==============
|
4
4
|
Table Importer
|
@@ -7,3 +7,15 @@ Table Importer
|
|
7
7
|
Given a file (or a string) containing a container, along with options, it will return a hash of those values. Great for importing poorly formatted CSV files.
|
8
8
|
|
9
9
|
Only works for ruby versions >= 1.9.3.
|
10
|
+
|
11
|
+
# Contributing to Table Importer
|
12
|
+
|
13
|
+
We love your contributions to Table Importer. Before submitting a pull request, please make sure that your changes are well tested.
|
14
|
+
|
15
|
+
Then, you'll need to install bundler and the gem dependencies:
|
16
|
+
|
17
|
+
`gem install bundler && bundle install`
|
18
|
+
|
19
|
+
You should now be able to run the local tests:
|
20
|
+
|
21
|
+
`bundle exec rake`
|
@@ -11,7 +11,7 @@ module TableImporter
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def assign_data(content)
|
14
|
-
raise
|
14
|
+
raise TableImporter::EmptyFileImportError.new if content.blank? || content[0..100].gsub(/[^A-Za-z0-9]/, '').blank?
|
15
15
|
content.gsub!(/\r\n|\r/, "\n")
|
16
16
|
return content
|
17
17
|
end
|
@@ -66,7 +66,7 @@ module TableImporter
|
|
66
66
|
lines[0..7]
|
67
67
|
end
|
68
68
|
rescue StandardError
|
69
|
-
raise
|
69
|
+
raise TableImporter::EmptyStringImportError.new
|
70
70
|
end
|
71
71
|
end
|
72
72
|
|
data/lib/table_importer/csv.rb
CHANGED
@@ -36,7 +36,7 @@ module TableImporter
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
rescue EOFError
|
39
|
-
raise
|
39
|
+
raise TableImporter::EmptyFileImportError.new
|
40
40
|
end
|
41
41
|
end
|
42
42
|
|
@@ -44,12 +44,12 @@ module TableImporter
|
|
44
44
|
begin
|
45
45
|
lines = get_preview_lines
|
46
46
|
if lines.blank? || lines == 0
|
47
|
-
raise
|
47
|
+
raise TableImporter::EmptyFileImportError.new
|
48
48
|
else
|
49
49
|
return lines
|
50
50
|
end
|
51
51
|
rescue NoMethodError
|
52
|
-
raise
|
52
|
+
raise TableImporter::EmptyFileImportError.new
|
53
53
|
end
|
54
54
|
end
|
55
55
|
|
@@ -86,7 +86,7 @@ module TableImporter
|
|
86
86
|
return clean_chunks([chunk], @compulsory_headers)[0].symbolize_keys[:lines][0..7]
|
87
87
|
end
|
88
88
|
rescue SmarterCSV::HeaderSizeMismatch
|
89
|
-
raise
|
89
|
+
raise TableImporter::HeaderMismatchError.new
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
@@ -130,7 +130,7 @@ module TableImporter
|
|
130
130
|
# fix quote_char
|
131
131
|
# bit of a hack here to provide the correct number of default headers to the user (rather than just 100)
|
132
132
|
def default_options(options = {})
|
133
|
-
{:col_sep => @column_separator, :row_sep => @record_separator, :
|
133
|
+
{:col_sep => @column_separator, :row_sep => @record_separator, :force_simple_split => true, :strip_chars_from_headers => /[\-"]/, :remove_empty_values => false,
|
134
134
|
:verbose => false, :headers_in_file => @headers_present, :convert_values_to_numeric => false,
|
135
135
|
:user_provided_headers => @headers_present ? (@headers == nil || @headers == {} ? nil : @headers) : default_headers(100)}.merge(options)
|
136
136
|
end
|
data/lib/table_importer/excel.rb
CHANGED
@@ -11,14 +11,14 @@ module TableImporter
|
|
11
11
|
@compulsory_headers = data[:compulsory_headers]
|
12
12
|
@delete_empty_columns = (File.size(@file_path) < 100000)
|
13
13
|
@mapping = !data[:user_headers].blank? ? data[:user_headers] : data[:headers]
|
14
|
-
raise
|
14
|
+
raise TableImporter::EmptyFileImportError.new if !@file.first_row
|
15
15
|
if !data[:headers].nil?
|
16
16
|
@headers = data[:headers]
|
17
17
|
else
|
18
18
|
@headers = @headers_present ? @file.row(1).map { |header| header.to_sym unless header.nil?} : default_headers
|
19
19
|
end
|
20
20
|
rescue NoMethodError
|
21
|
-
raise
|
21
|
+
raise TableImporter::HeaderMismatchError.new
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
@@ -34,7 +34,7 @@ module TableImporter
|
|
34
34
|
Roo::Excelx.new(@file_path).sheet(0)
|
35
35
|
end
|
36
36
|
rescue TypeError
|
37
|
-
raise
|
37
|
+
raise TableImporter::IncorrectFileError.new
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
@@ -51,7 +51,7 @@ module TableImporter
|
|
51
51
|
clean_chunks([get_lines(start_point+1, end_point+1)], @compulsory_headers, @delete_empty_columns)[0][:lines][0..7]
|
52
52
|
end
|
53
53
|
rescue SystemStackError
|
54
|
-
raise
|
54
|
+
raise TableImporter::EmptyFileImportError.new
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
@@ -3,9 +3,6 @@ module TableImporter
|
|
3
3
|
class Source
|
4
4
|
|
5
5
|
SEPARATORS = {comma: ",", space: " ", tab: "\t", newline_mac: "\n", semicolon: ";", pipe: "|", newline_windows: "\r\n", old_newline_mac: "\r"}
|
6
|
-
require 'table_importer/csv'
|
7
|
-
require 'table_importer/copy_and_paste'
|
8
|
-
require 'table_importer/excel'
|
9
6
|
|
10
7
|
def initialize (data)
|
11
8
|
case data[:type]
|
@@ -16,7 +13,7 @@ module TableImporter
|
|
16
13
|
when 'xls', 'xlsx'
|
17
14
|
@source = Excel.new(data)
|
18
15
|
else
|
19
|
-
raise
|
16
|
+
raise TableImporter::IncorrectFileError.new
|
20
17
|
end
|
21
18
|
@source
|
22
19
|
end
|
@@ -147,3 +144,7 @@ module TableImporter
|
|
147
144
|
end
|
148
145
|
end
|
149
146
|
end
|
147
|
+
|
148
|
+
require 'table_importer/csv'
|
149
|
+
require 'table_importer/copy_and_paste'
|
150
|
+
require 'table_importer/excel'
|
@@ -128,7 +128,7 @@ describe TableImporter::Source do
|
|
128
128
|
it 'raises an error when creating a source object' do
|
129
129
|
expect{
|
130
130
|
TableImporter::Source.new({:content => "", :headers_present => false, :headers => nil, :user_headers => nil, :type => "copy_and_paste", :col_sep => :comma, :rec_sep => :newline_mac, :compulsory_headers => {:email => true}})
|
131
|
-
}.to raise_error(
|
131
|
+
}.to raise_error(TableImporter::EmptyFileImportError)
|
132
132
|
end
|
133
133
|
end
|
134
134
|
end
|
data/spec/sources/csv_spec.rb
CHANGED
@@ -100,7 +100,7 @@ describe TableImporter::Source do
|
|
100
100
|
it 'raises an error when creating a source object' do
|
101
101
|
begin
|
102
102
|
TableImporter::Source.new({:content => File.open([Dir.pwd, "/spec/files/csv/no_content.csv"].join), :headers_present => true, :headers => nil, :user_headers => nil, :type => "csv", :column_separator => "", :record_separator => "", :compulsory_headers => {:email => true}})
|
103
|
-
rescue
|
103
|
+
rescue TableImporter::EmptyFileImportError => e
|
104
104
|
e.message
|
105
105
|
end
|
106
106
|
end
|
data/spec/sources/excel_spec.rb
CHANGED
@@ -117,7 +117,7 @@ describe TableImporter::Source do
|
|
117
117
|
it 'raises an error when creating a source object' do
|
118
118
|
begin
|
119
119
|
TableImporter::Source.new({:content => File.open([Dir.pwd, "/spec/files/excel/no_content.xlsx"].join), :headers => "false", :type => "xls", :column_separator => "", :record_separator => ""})
|
120
|
-
rescue
|
120
|
+
rescue TableImporter::EmptyFileImportError => e
|
121
121
|
e.message
|
122
122
|
end
|
123
123
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: table_importer
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Dowse
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-09-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: spreadsheet
|
@@ -148,6 +148,7 @@ files:
|
|
148
148
|
- .gitignore
|
149
149
|
- .travis.yml
|
150
150
|
- Gemfile
|
151
|
+
- Guardfile
|
151
152
|
- LICENSE
|
152
153
|
- LICENSE.txt
|
153
154
|
- README.md
|