uncsv 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +6 -0
- data/.rspec +2 -0
- data/.rubocop.yml +21 -0
- data/.travis.yml +37 -0
- data/.yardopts +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +201 -0
- data/README.md +159 -0
- data/Rakefile +13 -0
- data/bin/check-version +6 -0
- data/bin/console +15 -0
- data/bin/rake +18 -0
- data/bin/rspec +18 -0
- data/bin/rubocop +18 -0
- data/bin/yard +18 -0
- data/bin/yardoc +18 -0
- data/bin/yri +18 -0
- data/lib/uncsv/config.rb +204 -0
- data/lib/uncsv/header.rb +173 -0
- data/lib/uncsv/key_normalizer.rb +61 -0
- data/lib/uncsv/row.rb +109 -0
- data/lib/uncsv/rows.rb +99 -0
- data/lib/uncsv/version.rb +6 -0
- data/lib/uncsv.rb +71 -0
- data/uncsv.gemspec +30 -0
- metadata +181 -0
data/lib/uncsv.rb
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'uncsv/version'
|
4
|
+
require 'uncsv/config'
|
5
|
+
require 'uncsv/key_normalizer'
|
6
|
+
require 'uncsv/header'
|
7
|
+
require 'uncsv/row'
|
8
|
+
require 'uncsv/rows'
|
9
|
+
require 'csv'
|
10
|
+
require 'ostruct'
|
11
|
+
|
12
|
+
# Parses CSV data and iterates through it
|
13
|
+
#
|
14
|
+
# Accepts a `String`, `IO`, or file, and outputs CSV rows. The rows can be
|
15
|
+
# iterated over with `each`. `Uncsv` is also `Enumerable`, so all of those
|
16
|
+
# built-in Ruby methods also work with it, including `map`, `reduce`, `select`,
|
17
|
+
# etc.
|
18
|
+
class Uncsv
|
19
|
+
include Enumerable
|
20
|
+
|
21
|
+
# Create a new `Uncsv` parser
|
22
|
+
#
|
23
|
+
# @param data [String, IO] The input CSV data to parse
|
24
|
+
# @param opts [Hash] A hash of configuration options.
|
25
|
+
# See {Config#initialize}.
|
26
|
+
# @yield An optional block for setting configuration options.
|
27
|
+
# @yieldparam config [Config] The configuration object.
|
28
|
+
def initialize(data, opts = {})
|
29
|
+
@config = Config.new(opts)
|
30
|
+
yield @config if block_given?
|
31
|
+
@csv = CSV.new(data, @config.csv_opts)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Create a new `Uncsv` parser from a file
|
35
|
+
#
|
36
|
+
# @param filename [String] The path of the file to open
|
37
|
+
# @param opts (see #initialize)
|
38
|
+
# @yield (see #initialize)
|
39
|
+
# @yieldparam (see #initialize)
|
40
|
+
def self.open(filename, opts = {}, &block)
|
41
|
+
new(File.open(filename, 'rb'), opts, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
# Iterate over each data row of the CSV
|
45
|
+
#
|
46
|
+
# @yield A block to run for each row
|
47
|
+
# @yieldparam row [Row] A row object
|
48
|
+
# @return [Enumerator] An enumerator over each row
|
49
|
+
def each(&block)
|
50
|
+
rows.each(&block)
|
51
|
+
end
|
52
|
+
|
53
|
+
# Get an array of the headers
|
54
|
+
#
|
55
|
+
# Ordered from left to right
|
56
|
+
#
|
57
|
+
# @return [Array] An array of the header rows
|
58
|
+
def header
|
59
|
+
rows.header
|
60
|
+
end
|
61
|
+
|
62
|
+
private
|
63
|
+
|
64
|
+
# Initialize the internal rows object
|
65
|
+
#
|
66
|
+
# Once initialized, the {Rows} object is cached and reused for
|
67
|
+
# consecutive calls to {#each}.
|
68
|
+
def rows
|
69
|
+
@rows ||= Rows.new(@csv, @config)
|
70
|
+
end
|
71
|
+
end
|
data/uncsv.gemspec
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'uncsv/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'uncsv'
|
9
|
+
spec.version = Uncsv::VERSION
|
10
|
+
spec.authors = ['Justin Howard']
|
11
|
+
spec.email = ['jmhoward0@gmail.com']
|
12
|
+
spec.license = 'Apache-2.0'
|
13
|
+
|
14
|
+
spec.summary = 'A parser for unruly CSVs'
|
15
|
+
spec.homepage = 'https://github.com/machinima/uncsv'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`
|
18
|
+
.split("\x0")
|
19
|
+
.reject { |f| f.match(%r{^spec/}) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
23
|
+
spec.add_development_dependency 'fakefs', '~> 0.8'
|
24
|
+
spec.add_development_dependency 'rake', '>= 10.0'
|
25
|
+
spec.add_development_dependency 'redcarpet', '~> 3.4'
|
26
|
+
spec.add_development_dependency 'rspec', '~> 3.4'
|
27
|
+
spec.add_development_dependency 'rubocop', '~> 0.61'
|
28
|
+
spec.add_development_dependency 'simplecov', '~> 0.11'
|
29
|
+
spec.add_development_dependency 'yard', '>= 0.9.11'
|
30
|
+
end
|
metadata
ADDED
@@ -0,0 +1,181 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: uncsv
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Justin Howard
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-08 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.12'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: fakefs
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.8'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.8'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: redcarpet
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.4'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.4'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.61'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.61'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0.11'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0.11'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.9.11
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.9.11
|
125
|
+
description:
|
126
|
+
email:
|
127
|
+
- jmhoward0@gmail.com
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".rubocop.yml"
|
135
|
+
- ".travis.yml"
|
136
|
+
- ".yardopts"
|
137
|
+
- Gemfile
|
138
|
+
- LICENSE.txt
|
139
|
+
- README.md
|
140
|
+
- Rakefile
|
141
|
+
- bin/check-version
|
142
|
+
- bin/console
|
143
|
+
- bin/rake
|
144
|
+
- bin/rspec
|
145
|
+
- bin/rubocop
|
146
|
+
- bin/yard
|
147
|
+
- bin/yardoc
|
148
|
+
- bin/yri
|
149
|
+
- lib/uncsv.rb
|
150
|
+
- lib/uncsv/config.rb
|
151
|
+
- lib/uncsv/header.rb
|
152
|
+
- lib/uncsv/key_normalizer.rb
|
153
|
+
- lib/uncsv/row.rb
|
154
|
+
- lib/uncsv/rows.rb
|
155
|
+
- lib/uncsv/version.rb
|
156
|
+
- uncsv.gemspec
|
157
|
+
homepage: https://github.com/machinima/uncsv
|
158
|
+
licenses:
|
159
|
+
- Apache-2.0
|
160
|
+
metadata: {}
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0'
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubyforge_project:
|
177
|
+
rubygems_version: 2.7.8
|
178
|
+
signing_key:
|
179
|
+
specification_version: 4
|
180
|
+
summary: A parser for unruly CSVs
|
181
|
+
test_files: []
|