smartcsv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in smartcsv.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec', :version => 2 do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+ end
9
+
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
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
data/lib/smartcsv.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'csv'
2
+ require 'cmess/guess_encoding'
3
+ require "smartcsv/version"
4
+ require "smartcsv/parser"
5
+
6
+ module Smartcsv
7
+ def self.read(file)
8
+ Parser.read(file)
9
+ end
10
+ end
@@ -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
@@ -0,0 +1,3 @@
1
+ module Smartcsv
2
+ VERSION = "0.0.1"
3
+ 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
@@ -0,0 +1,6 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+
4
+ Bundler.require(:default)
5
+ require 'csv'
6
+ require 'smartcsv'
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