hippie_csv 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 99c371eea25788cf2b155a089ef2378ce564a039
4
+ data.tar.gz: 53c18182171b267f4c6a13f50b8672d39e177296
5
+ SHA512:
6
+ metadata.gz: 30ece254d7ba4e882d529c839fafa551979b3974d2af14ced46587d369a99ff94d9a3528bddac6ea174761a4430bd1d3b223717a5791cf8559218024da14f507
7
+ data.tar.gz: d9ce69502180d757d890d69b30985a7200ab9611f1694bbb140c399c2e577764c07d2839a7e9cea856d8767de23dbe6a463e52f44645c3a3b089a8e49e3acf45
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source "https://rubygems.org"
2
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2014 Intercom, Inc.
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
data/README.md ADDED
@@ -0,0 +1,63 @@
1
+ # HippieCSV
2
+
3
+ [ ![Codeship Status for intercom/hippie_csv](https://codeship.com/projects/f3b188e0-f312-0132-75cb-5ed004d44c71/status?branch=master)](https://codeship.com/projects/85324)
4
+
5
+ Ruby's `CSV` is great. It complies with the [proposed CSV spec](https://www.ietf.org/rfc/rfc4180.txt)
6
+ pretty well. If you pass its methods bad or incompliant CSVs, it’ll rightfully
7
+ and loudly complain. It’s great 👍
8
+
9
+ Except…if you want to be able to deal with files from the real world. At
10
+ [Intercom](https://intercom.io), we’ve seen lots of problematic CSVs from
11
+ customers importing data to our system. You may want to support such cases.
12
+ You may not always know the delimiter, nor the chosen quote character, in
13
+ advance.
14
+
15
+ HippieCSV is a ridiculously tolerant and liberal parser which aims to yield as
16
+ much usable data as possible out of such real-world CSVs.
17
+
18
+ ## Installation
19
+
20
+ Add this line to your application's Gemfile:
21
+
22
+ gem 'hippie_csv'
23
+
24
+ And then execute:
25
+
26
+ $ bundle
27
+
28
+ Or install it yourself as:
29
+
30
+ $ gem install hippie_csv
31
+
32
+ ## Usage
33
+
34
+ Exposes two public methods: `read` (for paths to files), and `parse` (for
35
+ strings).
36
+
37
+ ```ruby
38
+ require 'hippie_csv'
39
+
40
+ HippieCSV.read("path/to/data.csv")
41
+
42
+ HippieCSV.parse(csv_string)
43
+ ```
44
+
45
+ ## Features
46
+
47
+ - Deduces the delimiter (supports `,`, `;`, and `|`)
48
+ - Deduces the quote character (supports `'` and `"`)
49
+ - Forgives backslash escaped quotes in quoted CSVs
50
+ - Forgives invalid newlines in quoted CSVs
51
+ - Heals many encoding issues (and aggressively forces UTF-8)
52
+ - Deals with many miscellaneous malformed types of CSVs
53
+ - Works when a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) is present
54
+
55
+ ## Contributing
56
+
57
+ 1. Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
58
+ 2. Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
59
+ 3. Fork the project.
60
+ 4. Start a feature/bugfix branch.
61
+ 5. Commit and push until you are happy with your contribution.
62
+ 6. Make sure to add tests for it. This is important so we don't break it in a future version unintentionally.
63
+ 7. Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so we can cherry-pick around it.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -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 "hippie_csv/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "hippie_csv"
8
+ spec.version = HippieCSV::VERSION
9
+ spec.authors = ["Stephen O'Brien"]
10
+ spec.email = ["stephen@intercom.io"]
11
+ spec.summary = %q{Tolerant, liberal CSV parsing}
12
+ spec.homepage = ""
13
+ spec.license = "Apache License, Version 2.0"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_development_dependency "bundler", "~> 1.5"
21
+ spec.add_development_dependency "rake"
22
+ spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "pry"
24
+ end
@@ -0,0 +1,10 @@
1
+ require 'csv'
2
+
3
+ module HippieCSV
4
+ QUOTE_CHARACTERS = %w(" ' |).freeze
5
+ DELIMETERS = %W(, ; \t).freeze
6
+ ENCODING = "utf-8".freeze
7
+ ALTERNATE_ENCODING = "utf-16".freeze
8
+ FIELD_SAMPLE_COUNT = 10
9
+ ENCODING_WITH_BOM = "bom|#{ENCODING}".freeze
10
+ end
@@ -0,0 +1,7 @@
1
+ module HippieCSV
2
+ class UnableToParseError < StandardError
3
+ def self.explain
4
+ raise new "Something went wrong. Report this CSV: https://github.com/intercom/hippie_csv"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,63 @@
1
+ require "hippie_csv/constants"
2
+
3
+ module HippieCSV
4
+ module Support
5
+ class << self
6
+ def file_path_to_string(file_path)
7
+ File.read(file_path, encoding: ENCODING_WITH_BOM)
8
+ end
9
+
10
+ def encode!(string)
11
+ unless string.valid_encoding?
12
+ string.encode!(ALTERNATE_ENCODING, ENCODING, invalid: :replace, replace: "")
13
+ string.encode!(ENCODING, ALTERNATE_ENCODING)
14
+ end
15
+
16
+ string.encode!(string.encoding, universal_newline: true)
17
+ end
18
+
19
+ def maybe_parse(string)
20
+ QUOTE_CHARACTERS.find do |quote_character|
21
+ Support.rescuing_malformed do
22
+ return Support.parse_csv(string, quote_character)
23
+ end
24
+ end
25
+ end
26
+
27
+ def parse_csv(string, quote_character)
28
+ CSV.parse(
29
+ tolerate_escaping(string, quote_character),
30
+ quote_char: quote_character,
31
+ col_sep: guess_delimeter(string, quote_character)
32
+ )
33
+ end
34
+
35
+ def rescuing_malformed
36
+ begin; yield; rescue CSV::MalformedCSVError; end
37
+ end
38
+
39
+ def tolerate_escaping(string, quote_character)
40
+ string.gsub("\\#{quote_character}", "#{quote_character}#{quote_character}")
41
+ end
42
+
43
+ def guess_delimeter(string, quote_character)
44
+ results = DELIMETERS.map do |delimeter|
45
+ [delimeter, field_count(string, delimeter, quote_character)]
46
+ end.max_by do |delimeter, count|
47
+ count
48
+ end.each do |delimiter, count|
49
+ return delimiter
50
+ end
51
+ end
52
+
53
+ private
54
+
55
+ def field_count(file, delimeter, quote_character)
56
+ csv = CSV.new(file, col_sep: delimeter, quote_char: quote_character)
57
+ csv.lazy.take(FIELD_SAMPLE_COUNT).map(&:size).inject(:+)
58
+ rescue CSV::MalformedCSVError
59
+ 0
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module HippieCSV
2
+ VERSION = "0.0.1"
3
+ end
data/lib/hippie_csv.rb ADDED
@@ -0,0 +1,15 @@
1
+ require "hippie_csv/version"
2
+ require "hippie_csv/support"
3
+ require "hippie_csv/errors"
4
+
5
+ module HippieCSV
6
+ def self.read(path)
7
+ string = Support.file_path_to_string(path)
8
+ parse(string)
9
+ end
10
+
11
+ def self.parse(string)
12
+ Support.encode!(string)
13
+ Support.maybe_parse(string) || UnableToParseError.explain
14
+ end
15
+ end
@@ -0,0 +1 @@
1
+ m_id,first_name,last_name,email,Gender,Country,Applications2014,Member_Type,Length_Months
Binary file
@@ -0,0 +1,2 @@
1
+ 831819,"Lalo \"ElPapi\" Neymar","lalo@example.com","16/04/2015",,1,"16/04/2015","23/04/2015"
2
+ 831852,"BlazinShovels","blazinshovels@example.com","16/04/2015",,1,"20/04/2015","26/04/2015"
@@ -0,0 +1 @@
1
+ "133";"z3268856";"stephen@example.com";"23/9/2012";"23/9/2012";"1348368705";"1";;"sites/default/files/pictures/default-6.jpg";"2";"example.com"
@@ -0,0 +1,56 @@
1
+ Basic Pricing,,,,,,,,,,,,,,,,,,,,,,,
2
+ ,,,,,,,,Per Package Upgrades,,,,,,,Plan Ratios,,,,,,,,
3
+ Est Company Size,Est. Employee Size,End Users,Product,Support,Marketing,Everything,,Base,Product,Support,Marketing,Everything,Check,,Product ,Support,Marketing,,Check,Check,Check,,
4
+ Side,1,"1,000",-,-,-,$49 ,,,,,,$49 ,,,,,,,,,,,
5
+ Side,2,"2,000",-,-,-,$79 ,,,,,,$79 ,,,,,,,,,,,
6
+ Pre-Seed,2,"3,000",$49 ,$59 ,$69 ,$99 ,,$39 ,$10 ,$20 ,$30 ,$99 ,$0 ,,49% ,60% ,70% ,,$0 ,$0 ,$0 ,,
7
+ Pre-Seed,4,"5,000",$59 ,$69 ,$79 ,$109 ,,$49 ,$10 ,$20 ,$30 ,$109 ,$0 ,,54% ,63% ,72% ,,$0 ,$0 ,$0 ,,
8
+ Seed,4,"10,000",$69 ,$79 ,$99 ,$149 ,,$49 ,$20 ,$30 ,$50 ,$149 ,$0 ,,46% ,53% ,66% ,,$0 ,$0 ,$0 ,,
9
+ Seed,7,"15,000",$109 ,$119 ,$149 ,$219 ,,$79 ,$30 ,$40 ,$70 ,$219 ,$0 ,,50% ,54% ,68% ,,$0 ,$0 ,$0 ,,
10
+ Seed,9,"20,000",$159 ,$179 ,$219 ,$299 ,,$129 ,$30 ,$50 ,$90 ,$299 ,$0 ,,53% ,60% ,73% ,,$0 ,$0 ,$0 ,,
11
+ Series A,10,"25,000",$209 ,$239 ,$289 ,$399 ,,$169 ,$40 ,$70 ,$120 ,$399 ,$0 ,,52% ,60% ,72% ,,$0 ,$0 ,$0 ,,
12
+ Series A,20,"35,000",$299 ,$319 ,$389 ,$549 ,,$229 ,$70 ,$90 ,$160 ,$549 ,$0 ,,54% ,58% ,71% ,,$0 ,$0 ,$0 ,,
13
+ Series B/B+,25,"50,000",$379 ,$409 ,$489 ,$699 ,,$289 ,$90 ,$120 ,$200 ,$699 ,$0 ,,54% ,59% ,70% ,,$0 ,$0 ,$0 ,,
14
+ ,,,,,,,,,,,,,,,,,,,,,,,
15
+ ,,,,,,,,,,,,,,,,,,,,,,,
16
+ Pro Pricing,,Multiple,161% ,,,,,,,,,,,,,,,,,,,,
17
+ ,,,,,,,,Per Package Upgrades,,,,,,,Plan Ratios,,,,,,,,
18
+ Est Company Size,Est. Employee Size,End Users,Product,Support,Marketing,Everything,,Base,Product,Support,Marketing,Everything,Check,,Product ,Support,Marketing,,Check,Check,Check,,
19
+ Side,1,"1,000",,,,$79 ,,,,,,$79 ,,,,,,,,,,,
20
+ Side,2,"2,000",,,,$127 ,,,,,,$127 ,,,,,,,,,,,
21
+ Pre-Seed,2,"3,000",$79 ,$95 ,$111 ,$159 ,,$63 ,$16 ,$32 ,$48 ,$159 ,$0 ,,49% ,60% ,70% ,,$0 ,$0 ,$0 ,,
22
+ Pre-Seed,4,"5,000",$95 ,$111 ,$127 ,$175 ,,$79 ,$16 ,$32 ,$48 ,$175 ,$0 ,,54% ,63% ,72% ,,$0 ,$0 ,$0 ,,
23
+ Seed,4,"10,000",$111 ,$127 ,$159 ,$239 ,,$79 ,$32 ,$48 ,$80 ,$239 ,$0 ,,46% ,53% ,66% ,,$0 ,$0 ,$0 ,,
24
+ Seed,7,"15,000",$175 ,$191 ,$239 ,$352 ,,$127 ,$48 ,$64 ,$112 ,$352 ,$0 ,,50% ,54% ,68% ,,$0 ,$0 ,$0 ,,
25
+ Seed,9,"20,000",$255 ,$288 ,$352 ,$480 ,,$207 ,$48 ,$80 ,$145 ,$480 ,$0 ,,53% ,60% ,73% ,,$0 ,$0 ,$0 ,,
26
+ Series A,10,"25,000",$336 ,$384 ,$464 ,$641 ,,$272 ,$64 ,$112 ,$193 ,$641 ,$0 ,,52% ,60% ,72% ,,$0 ,$0 ,$0 ,,
27
+ Series A,20,"35,000",$480 ,$512 ,$625 ,$882 ,,$368 ,$112 ,$145 ,$257 ,$882 ,$0 ,,54% ,58% ,71% ,,$0 ,$0 ,$0 ,,
28
+ Series B/B+,25,"50,000",$609 ,$657 ,$786 ,"$1,123 ",,$464 ,$145 ,$193 ,$321 ,"$1,123 ",$0 ,,54% ,59% ,70% ,,$0 ,$0 ,$0 ,,
29
+ ,,,,,,,,,,,,,,,,,,,,,,,
30
+ ,,,,,,,,,,,,,,,,,,,,,,,
31
+ ,,,Prices,,,,,,,,,,,,,,,,,,,,
32
+ ,Company 1,,Company 2,Company 3,Company 4,,,,,,,,,,,,,,,,,,
33
+ Side,1,"1,000",,,,$30 ,,,,,,,,,,,,,,,,,
34
+ ,2,"2,000",,,,$48 ,,,,,,,,,,,,,,,,,
35
+ Pre-Seed,2,"3,000",$45 ,$38 ,$31 ,$60 ,,,,,,,,,,,,,,,,,
36
+ ,4,"5,000",$55 ,$49 ,$43 ,$66 ,,,,,,,,,,,,,,,,,
37
+ Seed,4,"10,000",$64 ,$75 ,$55 ,$90 ,,,,,,,,,,,,,,,,,
38
+ ,7,"15,000",$74 ,$102 ,$67 ,$133 ,,,,,,,,,,,,,,,,,
39
+ ,9,"20,000",$83 ,$129 ,$79 ,$181 ,,,,,,,,,,,,,,,,,
40
+ Series A,10,"25,000",$87 ,$186 ,$86 ,$242 ,,,,,,,,,,,,,,,,,
41
+ ,20,"35,000",$125 ,$243 ,$93 ,$333 ,,,,,,,,,,,,,,,,,
42
+ Series B/B+,25,"50,000",$187 ,$300 ,$100 ,$424 ,,,,,,,,,,,,,,,,,
43
+ ,,,,,,,,,,,,,,,,,,,,,,,
44
+ ,Employee Size,,,,,,,,,,,,,,,,,,,,,,
45
+ Side,1,,,,,,,,,,,,,,,,,,,,,,
46
+ ,2,,,,,,,,,,,,,,,,,,,,,,
47
+ Pre-Seed,2,,92% ,64% ,45% ,,,,,,,,,,,,,,,,,,
48
+ ,4,,93% ,71% ,54% ,,,,,,,,,,,,,,,,,,
49
+ Seed,4,,93% ,95% ,56% ,,,,,,,,,,,,,,,,,,
50
+ ,7,,67% ,86% ,45% ,,,,,,,,,,,,,,,,,,
51
+ ,9,,52% ,72% ,36% ,,,,,,,,,,,,,,,,,,
52
+ Series A,10,,42% ,78% ,30% ,,,,,,,,,,,,,,,,,,
53
+ ,20,,42% ,76% ,24% ,,,,,,,,,,,,,,,,,,
54
+ Series B/B+,25,,49% ,73% ,20% ,,,,,,,,,,,,,,,,,,
55
+ ,Avg,,66% ,77% ,39% ,61% ,,,,,,,,,,,,,,,,,
56
+ ,,,,,,,,,,,,,,,,,,,,,,,
@@ -0,0 +1,4 @@
1
+ site,lon,lat,max,min,precip,snow,snowdepth
2
+ 1V4,-72.02,44.42,13,-5,0.01,-99,-99
3
+ MPV,-72.56,44.20,1"2,-4,0.00,-99,-99
4
+ BTV,-73.15,44.47,14,-9,0.00,0.0,7,52
@@ -0,0 +1,14 @@
1
+ id,email,name,country,city,created_at,admin
2
+ 102,patrick@intercom.io,Patrick O'Doherty,United States,San Francisco,2014-04-28 23:57:08 UTC,true
3
+ 103,ben@intercom.io,Ben McRedmond,United States,San Francisco,2014-04-27 23:57:08 UTC,false
4
+ 104,ciaran@intercom.io,Ciaran Lee,Ireland,Dublin,2014-04-26 23:57:08 UTC,true
5
+ 105,eoghan@intercom.io,Eoghan McCabe,United States,San Francisco,2014-04-25 23:57:08 UTC,true
6
+ 106,des@intercom.io,Des Traynor,Ireland,Dublin,2014-04-24 23:57:08 UTC,true
7
+ 107,darragh@intercom.io,Darragh Curran,Ireland,Dublin,2014-04-23 23:57:08 UTC,true
8
+ 108,jeff@intercom.io,Jeff Gardner,Italy,Prata Camportaccio,2014-04-22 23:57:08 UTC,true
9
+ 109,macey@intercom.io,Macey Baker,United States,San Francisco,2014-04-21 23:57:08 UTC,true
10
+ 110,adam@intercom.io,Adam McCarthy,Dublin,Ireland,2014-04-20 23:57:08 UTC,true
11
+ 111,declan@intercom.io,Declan McGrath,Dublin,Ireland,2014-04-19 23:57:08 UTC,false
12
+ 112,peter@intercom.io,Peter McKenna,Dublin,Ireland,2014-04-18 23:57:08 UTC,false
13
+ 113,david@intercom.io,David Barett,Dublin,Ireland,2014-04-17 23:57:08 UTC,true
14
+ 114,eoin@intercom.io,Eoin Hennessy,Dublin,Ireland,2014-04-16 23:57:08 UTC,false
@@ -0,0 +1 @@
1
+ "Name","Email Address","Date Added"
@@ -0,0 +1,68 @@
1
+ require "spec_helper"
2
+
3
+ describe HippieCSV do
4
+
5
+ describe "::QUOTE_CHARACTERS" do
6
+ let(:supported_quote_characters) { ['"', "'", "|"] }
7
+
8
+ it "defines supported quote characters" do
9
+ expect(HippieCSV::QUOTE_CHARACTERS).to match(supported_quote_characters)
10
+ end
11
+
12
+ it "is not modifiable" do
13
+ expect(HippieCSV::QUOTE_CHARACTERS).to be_frozen
14
+ end
15
+ end
16
+
17
+ describe "::DELIMETERS" do
18
+ let(:supported_delimiters) { [",", ";", "\t"] }
19
+
20
+ it "defines supported delimeters" do
21
+ expect(HippieCSV::DELIMETERS).to match(supported_delimiters)
22
+ end
23
+
24
+ it "is not modifiable" do
25
+ expect(HippieCSV::DELIMETERS).to be_frozen
26
+ end
27
+ end
28
+
29
+ describe "::ENCODING" do
30
+ it "encodes as UTF-8" do
31
+ expect(HippieCSV::ENCODING).to eq('utf-8')
32
+ end
33
+
34
+ it "is not modifiable" do
35
+ expect(HippieCSV::ENCODING).to be_frozen
36
+ end
37
+ end
38
+
39
+ describe "::ALTERNATE_ENCODING" do
40
+ it "defines an alternative encoding for temporary force encoding purposes" do
41
+ expect(HippieCSV::ALTERNATE_ENCODING).to eq('utf-16')
42
+ end
43
+
44
+ it "is not modifiable" do
45
+ expect(HippieCSV::ALTERNATE_ENCODING).to be_frozen
46
+ end
47
+ end
48
+
49
+ describe "::FIELD_SAMPLE_COUNT" do
50
+ it "considers the first 10 rows when sampling from a CSV" do
51
+ expect(HippieCSV::FIELD_SAMPLE_COUNT).to eq(10)
52
+ end
53
+
54
+ it "is not modifiable" do
55
+ expect(HippieCSV::FIELD_SAMPLE_COUNT).to be_frozen
56
+ end
57
+ end
58
+
59
+ describe "::ENCODING_WITH_BOM" do
60
+ it "provides an encoding string supporting byte order mark" do
61
+ expect(HippieCSV::ENCODING_WITH_BOM).to eq('bom|utf-8')
62
+ end
63
+
64
+ it "is not modifiable" do
65
+ expect(HippieCSV::ENCODING_WITH_BOM).to be_frozen
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,112 @@
1
+ require "spec_helper"
2
+
3
+ describe HippieCSV::Support do
4
+
5
+ describe ".file_to_string" do
6
+ let(:file_path) { fixture_path(:normal) }
7
+ let(:result) { HippieCSV::Support.file_path_to_string(file_path) }
8
+
9
+ it "provides a string" do
10
+ expect(result.class).to eq String
11
+ end
12
+
13
+ it "reads the file" do
14
+ expect(result.slice(0,8)).to eq 'id,email'
15
+ end
16
+
17
+ context "with a byte order mark" do
18
+ let(:file_path) { fixture_path(:with_byte_order_mark) }
19
+
20
+ it "works" do
21
+ expect(result).to eq '"Name","Email Address","Date Added"'
22
+ end
23
+ end
24
+ end
25
+
26
+ describe ".encode!" do
27
+ context "with invalid byte sequence" do
28
+ let(:string) { "\u0014\xFE\u000E\u0000" }
29
+
30
+ it "would error normally" do
31
+ expect { CSV.parse(string) }.to raise_error(ArgumentError, "invalid byte sequence in UTF-8")
32
+ end
33
+
34
+ it "works" do
35
+ expect(string).not_to be_valid_encoding
36
+
37
+ subject.encode!(string)
38
+
39
+ expect(string).to be_valid_encoding
40
+ end
41
+ end
42
+
43
+ context 'with unquoted fields and \r or \n' do
44
+ let(:string) { "id,first_name,last_name\r123,Heinrich,Schütz\r\n" }
45
+
46
+ it "would error normally" do
47
+ expect { CSV.parse(string) }.to raise_error(CSV::MalformedCSVError, "Unquoted fields do not allow \\r or \\n (line 3).")
48
+ end
49
+
50
+ it "works" do
51
+ subject.encode!(string)
52
+
53
+ result = CSV.parse(string)
54
+ rows, columns = result.size, result.first.size
55
+
56
+ expect(rows).to eq(2)
57
+ expect(columns).to eq(3)
58
+ end
59
+ end
60
+ end
61
+
62
+ describe ".maybe_parse" do
63
+ it "needs to be written" do
64
+ skip # TODO write this test
65
+ end
66
+ end
67
+
68
+ describe ".parse_csv" do
69
+ let(:string) { "name,email\nstephen,test@example.com"}
70
+ let(:quote_character) { "\"" }
71
+
72
+ it "works" do
73
+ expect(subject).to receive(:guess_delimeter).with(string, quote_character).and_return(',')
74
+ expect(subject).to receive(:tolerate_escaping).with(string, quote_character).and_return(string)
75
+
76
+ expect(subject.parse_csv(string, quote_character)).to eq(
77
+ [["name", "email"], ["stephen", "test@example.com"]]
78
+ )
79
+ end
80
+ end
81
+
82
+ describe ".rescuing_malformed" do
83
+ let(:error) { CSV::MalformedCSVError }
84
+ let(:will_error) { -> { raise error } }
85
+
86
+ it "would normally error" do
87
+ expect { will_error.call }.to raise_error(error)
88
+ end
89
+
90
+ it "rescues from CSV::MalformedCSVError" do
91
+ expect { subject.rescuing_malformed { will_error.call } }.not_to raise_error
92
+ end
93
+ end
94
+
95
+ describe ".tolerate_escaping" do
96
+ let(:string) { "'Stephen', 'O\\'Brien'"}
97
+ let(:quote_character) { "'" }
98
+
99
+ it "enforces escaping according to CSV spec" do
100
+ expect(subject.tolerate_escaping(string, quote_character)).to eq("'Stephen', 'O''Brien'")
101
+ end
102
+ end
103
+
104
+ describe ".guess_delimeter" do
105
+ let(:string) { "'a'\t'b'\t'c'\n'd'\t'e'\t'f'\n" }
106
+ let(:quote_character) { "'" }
107
+
108
+ it "deduces the delimiter based on counts" do
109
+ expect(subject.guess_delimeter(string, quote_character)).to eq("\t")
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,9 @@
1
+ require "spec_helper"
2
+
3
+ describe HippieCSV do
4
+
5
+ it "defines a version" do
6
+ expect(HippieCSV::VERSION).to eq('0.0.1')
7
+ end
8
+
9
+ end
@@ -0,0 +1,104 @@
1
+ require "spec_helper"
2
+ require "csv"
3
+
4
+ describe HippieCSV do
5
+ let(:string) { "test" }
6
+
7
+ describe ".read" do
8
+ let(:path) { double }
9
+
10
+ it "converts to string and parses" do
11
+ expect(subject::Support).to receive(:file_path_to_string).with(path).and_return(string)
12
+ expect(subject).to receive(:parse).with(string)
13
+
14
+ subject.read(path)
15
+ end
16
+ end
17
+
18
+ describe ".parse" do
19
+ it "encodes the string" do
20
+ expect(subject::Support).to receive(:encode!).with(string)
21
+
22
+ subject.parse(string)
23
+ end
24
+
25
+ it "defers to support parse method" do
26
+ result = double
27
+ expect(subject::Support).to receive(:maybe_parse).with(string).and_return(result)
28
+
29
+ expect(subject.parse(string)).to eq(result)
30
+ end
31
+
32
+ context "when unable to parse" do
33
+ before do
34
+ expect(subject::Support).to receive(:maybe_parse).and_return(nil)
35
+ end
36
+
37
+ it "raises an error" do
38
+ expect {
39
+ subject.parse(string)
40
+ }.to raise_error(
41
+ subject::UnableToParseError,
42
+ "Something went wrong. Report this CSV: https://github.com/intercom/hippie_csv"
43
+ )
44
+ end
45
+ end
46
+ end
47
+
48
+ context "integration cases: hard/encountered problems" do
49
+ it "works when a BOM is present in the file" do
50
+ path = fixture_path(:with_byte_order_mark)
51
+ expect { CSV.read(path) }.to raise_error(CSV::MalformedCSVError)
52
+
53
+ import = subject.read(path)
54
+ expect(import[0]).to eq(["Name", "Email Address", "Date Added"])
55
+ end
56
+
57
+ it "works with a malformed CSV" do
58
+ path = fixture_path(:malformed)
59
+ expect { CSV.read(path) }.to raise_error(CSV::MalformedCSVError)
60
+
61
+ import = subject.read(path)
62
+ expect(import[0]).to eq(%w(site lon lat max min precip snow snowdepth))
63
+ end
64
+
65
+ it "works with odd encoding & emoji!" do
66
+ path = fixture_path(:encoding)
67
+ expect { CSV.read(path) }.to raise_error(ArgumentError)
68
+
69
+ import = subject.read(path)
70
+ expect(import[0].count).to eq(4)
71
+ end
72
+
73
+ it "works with an excel export" do
74
+ path = fixture_path(:excel)
75
+
76
+ import = subject.read(path)
77
+ expect(import[0].count).to eq(24)
78
+ end
79
+
80
+ it "works with escaped quotes" do
81
+ path = fixture_path(:escaped_quotes)
82
+
83
+ import = subject.read(path)
84
+ expect(import[0][1]).to eq("Lalo \"ElPapi\" Neymar")
85
+ expect(import[0][2]).to eq("lalo@example.com")
86
+ end
87
+
88
+ it "works with an invalid escaped quotes case" do
89
+ path = fixture_path(:escaped_quotes_semicolons)
90
+
91
+ import = subject.read(path)
92
+ expect(import[0][0]).to eq("133")
93
+ expect(import[0][1]).to eq("z3268856")
94
+ expect(import[0][2]).to eq("stephen@example.com")
95
+ end
96
+
97
+ it "works for a complicated case involving bad newlines and quote chars" do
98
+ path = fixture_path(:dos_line_ending)
99
+
100
+ import = subject.read(path)
101
+ expect(import[0].count).to eq(9)
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,5 @@
1
+ require "hippie_csv"
2
+
3
+ def fixture_path(name)
4
+ File.expand_path("../fixtures/#{name}.csv", __FILE__)
5
+ end
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hippie_csv
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen O'Brien
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-06-12 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.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '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
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ - stephen@intercom.io
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - .rspec
78
+ - Gemfile
79
+ - LICENSE.txt
80
+ - README.md
81
+ - Rakefile
82
+ - hippie_csv.gemspec
83
+ - lib/hippie_csv.rb
84
+ - lib/hippie_csv/constants.rb
85
+ - lib/hippie_csv/errors.rb
86
+ - lib/hippie_csv/support.rb
87
+ - lib/hippie_csv/version.rb
88
+ - spec/fixtures/dos_line_ending.csv
89
+ - spec/fixtures/encoding.csv
90
+ - spec/fixtures/escaped_quotes.csv
91
+ - spec/fixtures/escaped_quotes_semicolons.csv
92
+ - spec/fixtures/excel.csv
93
+ - spec/fixtures/malformed.csv
94
+ - spec/fixtures/normal.csv
95
+ - spec/fixtures/with_byte_order_mark.csv
96
+ - spec/hippie_csv/constants_spec.rb
97
+ - spec/hippie_csv/support_spec.rb
98
+ - spec/hippie_csv/version_spec.rb
99
+ - spec/hippie_csv_spec.rb
100
+ - spec/spec_helper.rb
101
+ homepage: ''
102
+ licenses:
103
+ - Apache License, Version 2.0
104
+ metadata: {}
105
+ post_install_message:
106
+ rdoc_options: []
107
+ require_paths:
108
+ - lib
109
+ required_ruby_version: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - '>='
112
+ - !ruby/object:Gem::Version
113
+ version: '0'
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - '>='
117
+ - !ruby/object:Gem::Version
118
+ version: '0'
119
+ requirements: []
120
+ rubyforge_project:
121
+ rubygems_version: 2.0.14
122
+ signing_key:
123
+ specification_version: 4
124
+ summary: Tolerant, liberal CSV parsing
125
+ test_files:
126
+ - spec/fixtures/dos_line_ending.csv
127
+ - spec/fixtures/encoding.csv
128
+ - spec/fixtures/escaped_quotes.csv
129
+ - spec/fixtures/escaped_quotes_semicolons.csv
130
+ - spec/fixtures/excel.csv
131
+ - spec/fixtures/malformed.csv
132
+ - spec/fixtures/normal.csv
133
+ - spec/fixtures/with_byte_order_mark.csv
134
+ - spec/hippie_csv/constants_spec.rb
135
+ - spec/hippie_csv/support_spec.rb
136
+ - spec/hippie_csv/version_spec.rb
137
+ - spec/hippie_csv_spec.rb
138
+ - spec/spec_helper.rb