smartcsv 0.0.1
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/.gitignore +18 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/Guardfile +9 -0
- data/LICENSE +22 -0
- data/README.md +32 -0
- data/Rakefile +2 -0
- data/lib/smartcsv.rb +10 -0
- data/lib/smartcsv/parser.rb +32 -0
- data/lib/smartcsv/version.rb +3 -0
- data/smartcsv.gemspec +22 -0
- data/spec/smartcsv_spec.rb +56 -0
- data/spec/spec_helper.rb +6 -0
- metadata +125 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Guardfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Arthur Harder
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Smartcsv
|
2
|
+
|
3
|
+
A CSV wrapper for read method with autodetect of column separator and encoding to utf-8
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'smartcsv'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install smartcsv
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
# returned csv encoded as utf-8
|
23
|
+
Smartcsv.read(file)
|
24
|
+
```
|
25
|
+
|
26
|
+
## Contributing
|
27
|
+
|
28
|
+
1. Fork it
|
29
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
30
|
+
3. Commit your changes (`git commit -am 'Added some feature'`)
|
31
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
32
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/smartcsv.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'cmess/guess_encoding'
|
2
|
+
|
3
|
+
module Smartcsv
|
4
|
+
class Parser
|
5
|
+
def self.read(file)
|
6
|
+
CSV.read(file,{encoding: get_encoding(file), col_sep: get_separator(file)})
|
7
|
+
end
|
8
|
+
|
9
|
+
def self.get_encoding(file)
|
10
|
+
@encoding ||= begin
|
11
|
+
encoding = CMess::GuessEncoding::Automatic.guess(File.read(file))
|
12
|
+
encoding.downcase =~ /iso|utf/ ? encoding : 'ISO-8859-1'
|
13
|
+
end
|
14
|
+
"%s:UTF-8" % @encoding
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.get_separator(file)
|
18
|
+
line = IO.readlines(file, encoding: get_encoding(file))[0]
|
19
|
+
|
20
|
+
h = {}
|
21
|
+
h["comma"] = line.split(',').count
|
22
|
+
h["semicolon"] = line.split(';').count
|
23
|
+
h["tab"] = line.split("\t").count
|
24
|
+
|
25
|
+
case h.max{|a,b| a[1] <=> b[1]}[0]
|
26
|
+
when "semicolon" then ';'
|
27
|
+
when "tab" then "\t"
|
28
|
+
else ','
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/smartcsv.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/smartcsv/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Arthur Harder"]
|
6
|
+
gem.email = ["info@easywebsolutions.de"]
|
7
|
+
gem.description = %q{}
|
8
|
+
gem.summary = %q{A CSV wrapper for read method with autodetect of column separator and encoding to utf-8}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "smartcsv"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Smartcsv::VERSION
|
17
|
+
|
18
|
+
gem.add_development_dependency 'rspec', '~> 2.10.0'
|
19
|
+
gem.add_development_dependency 'guard-rspec'
|
20
|
+
gem.add_development_dependency 'rb-fsevent'
|
21
|
+
gem.add_dependency 'cmess'
|
22
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Smartcsv do
|
5
|
+
describe "read an exemple csv file" do
|
6
|
+
|
7
|
+
let(:content) do
|
8
|
+
file = './tmp/example.csv'
|
9
|
+
CSV.open(file, "wb:UTF-8:#{encode}", col_sep: col_sep) do |csv|
|
10
|
+
csv << ['Dürüm', 'Kebab']
|
11
|
+
csv << ['Dürüm', 'Kebab']
|
12
|
+
end
|
13
|
+
file
|
14
|
+
end
|
15
|
+
|
16
|
+
context "encoded as ISO-8859-1" do
|
17
|
+
let(:encode) {'ISO-8859-1'}
|
18
|
+
|
19
|
+
context "with semicolon as column separator" do
|
20
|
+
let(:col_sep) {';'}
|
21
|
+
it { Smartcsv.read(content).first[0].should eq "Dürüm" }
|
22
|
+
end
|
23
|
+
|
24
|
+
context "with comma as column separator" do
|
25
|
+
let(:col_sep) {','}
|
26
|
+
it { Smartcsv.read(content).first[0].should eq "Dürüm" }
|
27
|
+
end
|
28
|
+
|
29
|
+
context "with tab as column separator" do
|
30
|
+
let(:col_sep) {"\t"}
|
31
|
+
it { Smartcsv.read(content).first[0].should eq "Dürüm" }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
context "encoded as UTF-8" do
|
36
|
+
let(:encode) {'UTF-8'}
|
37
|
+
|
38
|
+
context "with semicolon as column separator" do
|
39
|
+
let(:col_sep) {';'}
|
40
|
+
it { Smartcsv.read(content).first[0].should eq "Dürüm" }
|
41
|
+
end
|
42
|
+
|
43
|
+
context "with comma as column separator" do
|
44
|
+
let(:col_sep) {','}
|
45
|
+
it { Smartcsv.read(content).first[0].should eq "Dürüm" }
|
46
|
+
end
|
47
|
+
|
48
|
+
context "with tab as column separator" do
|
49
|
+
let(:col_sep) {"\t"}
|
50
|
+
it { Smartcsv.read(content).first[0].should eq "Dürüm" }
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: smartcsv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Arthur Harder
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.10.0
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 2.10.0
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: guard-rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rb-fsevent
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: cmess
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ! '>='
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
type: :runtime
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ! '>='
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
description: ''
|
79
|
+
email:
|
80
|
+
- info@easywebsolutions.de
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- .rspec
|
87
|
+
- Gemfile
|
88
|
+
- Guardfile
|
89
|
+
- LICENSE
|
90
|
+
- README.md
|
91
|
+
- Rakefile
|
92
|
+
- lib/smartcsv.rb
|
93
|
+
- lib/smartcsv/parser.rb
|
94
|
+
- lib/smartcsv/version.rb
|
95
|
+
- smartcsv.gemspec
|
96
|
+
- spec/smartcsv_spec.rb
|
97
|
+
- spec/spec_helper.rb
|
98
|
+
homepage: ''
|
99
|
+
licenses: []
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.25
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: A CSV wrapper for read method with autodetect of column separator and encoding
|
122
|
+
to utf-8
|
123
|
+
test_files:
|
124
|
+
- spec/smartcsv_spec.rb
|
125
|
+
- spec/spec_helper.rb
|