csv_option 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.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +38 -0
- data/Rakefile +28 -0
- data/csv_option.gemspec +24 -0
- data/lib/csv_option.rb +7 -0
- data/lib/csv_option/utils.rb +54 -0
- data/lib/csv_option/version.rb +3 -0
- data/spec/csv_option/utils_spec.rb +26 -0
- data/spec/csv_option/utils_tab_csv_spec.rb +26 -0
- data/spec/fixtures/test.csv +3 -0
- data/spec/fixtures/testtab.txt +3 -0
- data/spec/spec_helper.rb +95 -0
- metadata +106 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 377cc7ac0e6a91e324624cffa1f60a81b98b302a
|
4
|
+
data.tar.gz: 9f02fa8971a85526888cd032d25de7fe5e314c98
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 739cce273e04078c67051c68b18d832b66df05f68d8817c54ac0a315e6065b8668ed0a6ef34233bf8d0954ae7af93b8013bf69e36a0c7867794914d6f1250432
|
7
|
+
data.tar.gz: 5d2e9f46ce77dba9c5ebdd9297a81f2dacc9ff58e4dbbde2d0065f1b9842b74fb4cab91b70dd80ad997a56e60e796733a2edf0145200a1ea0d8cbfa6d0699234
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Sanjiv Jha
|
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,38 @@
|
|
1
|
+
# CsvOption
|
2
|
+
|
3
|
+
'csv_options' is rubygem for determing various options passed to CSV parser. It determine options automatically.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'csv_option'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install csv_option
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
utils = CsvOption::Utils.new("#{fixture_path}/test.csv")
|
23
|
+
|
24
|
+
#determine column separator
|
25
|
+
utils.determine_column_separator
|
26
|
+
|
27
|
+
#determine headers of csv file(Assuming the firstline will always be header)
|
28
|
+
utils.parse_headers
|
29
|
+
|
30
|
+
#determing row options
|
31
|
+
utils.determine_row_separator
|
32
|
+
## Contributing
|
33
|
+
|
34
|
+
1. Fork it ( https://github.com/[my-github-username]/csv_option/fork )
|
35
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
36
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
37
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
38
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
#!/usr/bin/env rake
|
2
|
+
require "bundler/gem_tasks"
|
3
|
+
|
4
|
+
require 'rubygems'
|
5
|
+
require 'rake'
|
6
|
+
|
7
|
+
require 'rspec/core/rake_task'
|
8
|
+
|
9
|
+
desc "Run RSpec"
|
10
|
+
RSpec::Core::RakeTask.new do |t|
|
11
|
+
t.verbose = false
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Run specs for all test cases"
|
15
|
+
task :spec_all do
|
16
|
+
system "rake spec"
|
17
|
+
end
|
18
|
+
|
19
|
+
# task :spec_all do
|
20
|
+
# %w[active_record data_mapper mongoid].each do |model_adapter|
|
21
|
+
# puts "MODEL_ADAPTER = #{model_adapter}"
|
22
|
+
# system "rake spec MODEL_ADAPTER=#{model_adapter}"
|
23
|
+
# end
|
24
|
+
# end
|
25
|
+
|
26
|
+
task :default => :spec
|
27
|
+
|
28
|
+
|
data/csv_option.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'csv_option/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "csv_option"
|
8
|
+
spec.version = CsvOption::VERSION
|
9
|
+
spec.authors = ["Sanjiv Jha"]
|
10
|
+
spec.email = ["sanjiv@joshsoftwaree.com"]
|
11
|
+
spec.summary = %q{Ruby gem for determing CSV options.}
|
12
|
+
spec.description = %q{Ruby gem for determing CSV options.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
end
|
data/lib/csv_option.rb
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
module CsvOption
|
2
|
+
class Utils
|
3
|
+
attr_accessor :file
|
4
|
+
|
5
|
+
def initialize(file_path)
|
6
|
+
@file = file_path
|
7
|
+
end
|
8
|
+
|
9
|
+
def first_row
|
10
|
+
open(@file).each_line.first
|
11
|
+
end
|
12
|
+
|
13
|
+
def full_contents
|
14
|
+
open(@file).read
|
15
|
+
end
|
16
|
+
|
17
|
+
def determine_column_separator(data=nil)
|
18
|
+
file_contents = data || first_row
|
19
|
+
counts = Hash.new(0)
|
20
|
+
counts.merge!({"," => 0 , ":" => 0, ";" => 0 , "|" => 0, "\t" => 0 })
|
21
|
+
file_contents.each_char do |c|
|
22
|
+
next if c.match(/^[[:alpha:]]$/) or c.match(/^[[:digit:]]$/)
|
23
|
+
counts[c] += 1
|
24
|
+
end
|
25
|
+
k,v = counts.max_by{|k,v| v}
|
26
|
+
return k
|
27
|
+
end
|
28
|
+
|
29
|
+
def parse_headers
|
30
|
+
data = first_row
|
31
|
+
col_sep = determine_column_separator(data)
|
32
|
+
headers = if data.include?("\r")
|
33
|
+
data.split("\r").first.split(col_sep).map{ |a| a.strip.tr('"', '')}.sort
|
34
|
+
else
|
35
|
+
data.split(col_sep).map{ |a| a.strip.tr('"', '')}.sort
|
36
|
+
end
|
37
|
+
headers
|
38
|
+
end
|
39
|
+
|
40
|
+
def determine_row_separator
|
41
|
+
body = full_contents
|
42
|
+
counts = {"\n" => 0 , "\r" => 0, "\r\n" => 0}
|
43
|
+
quoted_char = false
|
44
|
+
body.each_char do |c|
|
45
|
+
quoted_char = !quoted_char if c == '"'
|
46
|
+
next if quoted_char || c !~ /\r|\n|\r\n/
|
47
|
+
counts[c] += 1
|
48
|
+
|
49
|
+
end
|
50
|
+
k,v = counts.max_by{|k,v| v}
|
51
|
+
return k
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
fixture_path = 'spec/fixtures'
|
4
|
+
|
5
|
+
describe 'CSV file(comma separated file)' do
|
6
|
+
before(:all) do
|
7
|
+
@file_utils = CsvOption::Utils.new("#{fixture_path}/test.csv")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "parse headers" do
|
11
|
+
headers = ["productid", "category", "listprice", "saleprice", "title", "imageurl", "url"]
|
12
|
+
expect(@file_utils.parse_headers).to eq(headers.sort)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parse colseparator" do
|
16
|
+
expect(@file_utils.determine_column_separator).to eq(',')
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parse colseparator with data given" do
|
20
|
+
expect(@file_utils.determine_column_separator("one|two|three\n")).to eq('|')
|
21
|
+
end
|
22
|
+
|
23
|
+
it "parse row separator" do
|
24
|
+
expect(@file_utils.determine_row_separator).to eq("\n")
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
fixture_path = 'spec/fixtures'
|
4
|
+
|
5
|
+
describe 'CSV file(comma separated file)' do
|
6
|
+
before(:all) do
|
7
|
+
@file_utils = CsvOption::Utils.new("#{fixture_path}/testtab.txt")
|
8
|
+
end
|
9
|
+
|
10
|
+
it "parse headers" do
|
11
|
+
headers = ["productid", "category", "listprice", "saleprice", "title", "imageurl", "url"]
|
12
|
+
expect(@file_utils.parse_headers).to eq(headers.sort)
|
13
|
+
end
|
14
|
+
|
15
|
+
it "parse colseparator" do
|
16
|
+
expect(@file_utils.determine_column_separator).to eq("\t")
|
17
|
+
end
|
18
|
+
|
19
|
+
it "parse colseparator with data given" do
|
20
|
+
expect(@file_utils.determine_column_separator("one|two|three\n")).to eq('|')
|
21
|
+
end
|
22
|
+
|
23
|
+
it "parse row separator" do
|
24
|
+
expect(@file_utils.determine_row_separator).to eq("\n")
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,3 @@
|
|
1
|
+
productid,category,listprice,saleprice,title,imageurl,url
|
2
|
+
23c1d99e-f003-4050-bbe4-4e5e29cf5840,Banaran,$26.22,$93.26,Twinte,http://amazon.co.jp/viverra/eget/congue/eget/semper/rutrum/nulla.png,https://shinystat.com/mi/nulla/ac.aspx
|
3
|
+
7e02485d-b0eb-4d3b-ba32-4dbe7cf47a46,Marseille,$26.70,$99.26,Zava,http://reuters.com/ipsum/praesent/blandit/lacinia.aspx,http://slideshare.net/eu/mi/nulla.aspx
|
@@ -0,0 +1,3 @@
|
|
1
|
+
productid category listprice saleprice title imageurl url
|
2
|
+
4804a915-0072-4ec7-b93b-9d241141e5b5 Yanggu $33.21 $45.70 Digitube http://php.net/scelerisque/mauris.jpg http://deviantart.com/pellentesque/viverra/pede/ac/diam/cras.xml
|
3
|
+
9dc1bbfc-d601-4354-b438-8d142733b4c9 Aoji-ri $46.77 $47.07 Brainbox http://de.vu/ipsum.js https://bandcamp.com/a/feugiat/et/eros.xml
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
Bundler.require(:default)
|
5
|
+
|
6
|
+
require 'csv_option'
|
7
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
8
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
9
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause this
|
10
|
+
# file to always be loaded, without a need to explicitly require it in any files.
|
11
|
+
#
|
12
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
13
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
14
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
15
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
16
|
+
# a separate helper file that requires the additional dependencies and performs
|
17
|
+
# the additional setup, and require it from the spec files that actually need it.
|
18
|
+
#
|
19
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
20
|
+
# users commonly want.
|
21
|
+
#
|
22
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
23
|
+
RSpec.configure do |config|
|
24
|
+
# rspec-expectations config goes here. You can use an alternate
|
25
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
26
|
+
# assertions if you prefer.
|
27
|
+
config.expect_with :rspec do |expectations|
|
28
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
29
|
+
# and `failure_message` of custom matchers include text for helper methods
|
30
|
+
# defined using `chain`, e.g.:
|
31
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
32
|
+
# # => "be bigger than 2 and smaller than 4"
|
33
|
+
# ...rather than:
|
34
|
+
# # => "be bigger than 2"
|
35
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
36
|
+
end
|
37
|
+
|
38
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
39
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
40
|
+
config.mock_with :rspec do |mocks|
|
41
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
42
|
+
# a real object. This is generally recommended, and will default to
|
43
|
+
# `true` in RSpec 4.
|
44
|
+
mocks.verify_partial_doubles = true
|
45
|
+
end
|
46
|
+
|
47
|
+
# The settings below are suggested to provide a good initial experience
|
48
|
+
# with RSpec, but feel free to customize to your heart's content.
|
49
|
+
=begin
|
50
|
+
# These two settings work together to allow you to limit a spec run
|
51
|
+
# to individual examples or groups you care about by tagging them with
|
52
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
53
|
+
# get run.
|
54
|
+
config.filter_run :focus
|
55
|
+
config.run_all_when_everything_filtered = true
|
56
|
+
|
57
|
+
# Limits the available syntax to the non-monkey patched syntax that is recommended.
|
58
|
+
# For more details, see:
|
59
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
60
|
+
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
61
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
62
|
+
config.disable_monkey_patching!
|
63
|
+
|
64
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
65
|
+
# be too noisy due to issues in dependencies.
|
66
|
+
config.warnings = true
|
67
|
+
|
68
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
69
|
+
# file, and it's useful to allow more verbose output when running an
|
70
|
+
# individual spec file.
|
71
|
+
if config.files_to_run.one?
|
72
|
+
# Use the documentation formatter for detailed output,
|
73
|
+
# unless a formatter has already been configured
|
74
|
+
# (e.g. via a command-line flag).
|
75
|
+
config.default_formatter = 'doc'
|
76
|
+
end
|
77
|
+
|
78
|
+
# Print the 10 slowest examples and example groups at the
|
79
|
+
# end of the spec run, to help surface which specs are running
|
80
|
+
# particularly slow.
|
81
|
+
config.profile_examples = 10
|
82
|
+
|
83
|
+
# Run specs in random order to surface order dependencies. If you find an
|
84
|
+
# order dependency and want to debug it, you can fix the order by providing
|
85
|
+
# the seed, which is printed after each run.
|
86
|
+
# --seed 1234
|
87
|
+
config.order = :random
|
88
|
+
|
89
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
90
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
91
|
+
# test failures related to randomization by passing the same `--seed` value
|
92
|
+
# as the one that triggered the failure.
|
93
|
+
Kernel.srand config.seed
|
94
|
+
=end
|
95
|
+
end
|
metadata
ADDED
@@ -0,0 +1,106 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: csv_option
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sanjiv Jha
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Ruby gem for determing CSV options.
|
56
|
+
email:
|
57
|
+
- sanjiv@joshsoftwaree.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- Gemfile
|
64
|
+
- LICENSE.txt
|
65
|
+
- README.md
|
66
|
+
- Rakefile
|
67
|
+
- csv_option.gemspec
|
68
|
+
- lib/csv_option.rb
|
69
|
+
- lib/csv_option/utils.rb
|
70
|
+
- lib/csv_option/version.rb
|
71
|
+
- spec/csv_option/utils_spec.rb
|
72
|
+
- spec/csv_option/utils_tab_csv_spec.rb
|
73
|
+
- spec/fixtures/test.csv
|
74
|
+
- spec/fixtures/testtab.txt
|
75
|
+
- spec/spec_helper.rb
|
76
|
+
homepage: ''
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.4.3
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Ruby gem for determing CSV options.
|
100
|
+
test_files:
|
101
|
+
- spec/csv_option/utils_spec.rb
|
102
|
+
- spec/csv_option/utils_tab_csv_spec.rb
|
103
|
+
- spec/fixtures/test.csv
|
104
|
+
- spec/fixtures/testtab.txt
|
105
|
+
- spec/spec_helper.rb
|
106
|
+
has_rdoc:
|