├── .rspec ├── Rakefile ├── Gemfile ├── spec ├── fixtures │ ├── small_file.csv │ ├── with_byte_order_mark.csv │ ├── internal_quotes.csv │ ├── encoding.csv │ ├── never_ordered.csv │ ├── accents_semicolon_windows_1252.csv │ ├── escaped_quotes_semicolons.csv │ ├── malformed.csv │ ├── escaped_quotes.csv │ ├── blank_lines_crlf.csv │ ├── bad_quoting.csv │ ├── dos_line_ending.csv │ ├── normal.csv │ ├── trailing_leading_blank_lines.csv │ └── excel.csv ├── spec_helper.rb ├── hippie_csv │ ├── version_spec.rb │ ├── constants_spec.rb │ └── support_spec.rb └── hippie_csv_spec.rb ├── lib ├── hippie_csv │ ├── version.rb │ ├── errors.rb │ ├── constants.rb │ └── support.rb └── hippie_csv.rb ├── .gitignore ├── hippie_csv.gemspec ├── .circleci └── config.yml ├── README.md └── LICENSE.txt /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format progress 3 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | gemspec 3 | -------------------------------------------------------------------------------- /spec/fixtures/small_file.csv: -------------------------------------------------------------------------------- 1 | name,email 2 | stephen,test@example.com 3 | -------------------------------------------------------------------------------- /spec/fixtures/with_byte_order_mark.csv: -------------------------------------------------------------------------------- 1 | "Name","Email Address","Date Added" -------------------------------------------------------------------------------- /lib/hippie_csv/version.rb: -------------------------------------------------------------------------------- 1 | module HippieCSV 2 | VERSION = "0.1.0" 3 | end 4 | -------------------------------------------------------------------------------- /spec/fixtures/internal_quotes.csv: -------------------------------------------------------------------------------- 1 | "Email","ID","Name" 2 | "test@example.com","123","James "Jimmy" Doe" 3 | -------------------------------------------------------------------------------- /spec/fixtures/encoding.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/josephfrazier/hippie_csv/master/spec/fixtures/encoding.csv -------------------------------------------------------------------------------- /spec/fixtures/never_ordered.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/josephfrazier/hippie_csv/master/spec/fixtures/never_ordered.csv -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | require "hippie_csv" 2 | 3 | def fixture_path(name) 4 | File.expand_path("../fixtures/#{name}.csv", __FILE__) 5 | end 6 | -------------------------------------------------------------------------------- /spec/fixtures/accents_semicolon_windows_1252.csv: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/josephfrazier/hippie_csv/master/spec/fixtures/accents_semicolon_windows_1252.csv -------------------------------------------------------------------------------- /spec/fixtures/escaped_quotes_semicolons.csv: -------------------------------------------------------------------------------- 1 | "133";"z3268856";"stephen@example.com";"23/9/2012";"23/9/2012";"1348368705";"1";;"sites/default/files/pictures/default-6.jpg";"2";"example.com" 2 | -------------------------------------------------------------------------------- /spec/fixtures/malformed.csv: -------------------------------------------------------------------------------- 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 5 | -------------------------------------------------------------------------------- /spec/hippie_csv/version_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | describe HippieCSV do 4 | 5 | it "defines a version" do 6 | expect(HippieCSV::VERSION).to eq("0.1.0") 7 | end 8 | 9 | end 10 | -------------------------------------------------------------------------------- /spec/fixtures/escaped_quotes.csv: -------------------------------------------------------------------------------- 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" 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 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 | -------------------------------------------------------------------------------- /lib/hippie_csv/errors.rb: -------------------------------------------------------------------------------- 1 | module HippieCSV 2 | class UnableToParseError < StandardError 3 | def initialize(msg = "Something went wrong. Report this CSV: https://github.com/intercom/hippie_csv") 4 | super(msg) 5 | end 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /spec/fixtures/blank_lines_crlf.csv: -------------------------------------------------------------------------------- 1 | Email,First Name,Last Name 2 | admin@example.com,The,Admin 3 | ,, 4 | msonstot@example.com,Matt, Clemens 5 | ,, 6 | nathan@example.com,NATHAN,O'BRIEN 7 | ,, 8 | ,, 9 | garry@example.com,Garry,Redmond 10 | -------------------------------------------------------------------------------- /lib/hippie_csv.rb: -------------------------------------------------------------------------------- 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 = File.read(path, encoding: ENCODING_WITH_BOM) 8 | parse(string) 9 | end 10 | 11 | def self.parse(string) 12 | Support.maybe_parse(string) || (raise UnableToParseError) 13 | end 14 | 15 | def self.stream(path, &block) 16 | Support.maybe_stream(path, &block) 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /lib/hippie_csv/constants.rb: -------------------------------------------------------------------------------- 1 | require "csv" 2 | 3 | module HippieCSV 4 | QUOTE_CHARACTERS = %w(" ' | ∑ ⦿ ◉).freeze 5 | # The latter three characters are not expected to intentionally used as 6 | # quotes. Rather, when usual quote characters are badly misused, we want 7 | # to fall back to a character _unlikely_ to be in the file, such that 8 | # we can at least parse. 9 | 10 | DELIMETERS = %W(, ; \t).freeze 11 | ENCODING = "utf-8".freeze 12 | ALTERNATE_ENCODING = "utf-16".freeze 13 | FIELD_SAMPLE_COUNT = 10.freeze 14 | ENCODING_SAMPLE_CHARACTER_COUNT = 10000.freeze 15 | ENCODING_WITH_BOM = "bom|#{ENCODING}".freeze 16 | end 17 | -------------------------------------------------------------------------------- /spec/fixtures/bad_quoting.csv: -------------------------------------------------------------------------------- 1 | id,full_name,email,support,admin_of,user_payment_count,user_payment_total,click_count,registration_date,user_status,email_validation 2 | 1,Tom Stonew,tom@example.com,Stone's Soccer Club,Stone's Soccer Club,5,£1.16,96,3/31/2015 17:28:39,active,Y 3 | 262,Mark J Collins,info@example.co,|Run N Hide Things Get Cancelled!|,Run N Hide | Things Get Cancelled!,0,NULL,0,7/2/2015 18:41:05,active,N 4 | 474,Francis Daly,francisdaly@live.com,"\A Junction\"" Project Association""",NULL,0,NULL,1,8/13/2015 14:50:05,active,N 5 | 640,mitchell miller,test@example.co.uk,Berkely ⦿ Club,NULL,0,NULL,1,9/9/2015 10:35:00,active,N 6 | 646,Ash ,hi@example.com,"\NSAAA A" """,NULL,0,NULL,0,9/9/2015 14:21:21,active,N 7 | 649,Ken Kenneth,ken@example.com,Lusk ∑ Club,Lusk ∑ Club,0,NULL,1,9/9/2015 21:45:24,active,Y 8 | 650,Bam Gouldstone,bam@example.com,$ Shave Club,NULL,0,NULL,0,9/10/2015 6:43:07,active,N 9 | -------------------------------------------------------------------------------- /spec/fixtures/dos_line_ending.csv: -------------------------------------------------------------------------------- 1 | m_id,first_name,last_name,email,Gender,Country,Applications2014,Member_Type,Length_Months 2353505,Sana'a,Gala,yo@example.com,Female,AUS,149,Talent,23 2448540,Galina,Zaitia,linka@example.ru,Female,UK,NULL,Talent,23 2427960,Della,Braun,missd@example.co.uk,Female,UK,8,Talent,23 1885115,Jennifer,Rose,jennifer.rose@example.com,Female,US,1,Talent,12 2198882,Eloise,*,helenek@example.com,Female,AUS,59,Talent,12 1956375,Lyn,McHughes,lyn@example.co.nz,Female,NZ,NULL,Talent,12 902570,"Ian""Sheepie""",Smythe,sheepie@example.co.uk,Male,UK,NULL,Talent,18 1834416,Charlotte,Cooper-Mencha,charlotte@example.com,Female,UK,16,Talent,18 1638723,Debbie,Gordon,debbie@example.com,Female,UK,7,Talent,18 763791,Nina,Benvenuti,nina@example.com,Female,AUS,2,Talent,18 728947,"Amanda""Harper""",Kannegi,a_kannegi@example.com,Female,AUS,45,Talent,18 548803,JAMES,NORMAN,jamesnorman@example.com,Male,UK,NULL,Talent,18 3053858,Felipe,Matteiu,matteiu@example.com,Male,UK,109,Hybrid,12 2 | -------------------------------------------------------------------------------- /hippie_csv.gemspec: -------------------------------------------------------------------------------- 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 | 25 | spec.add_dependency "rchardet" 26 | end 27 | -------------------------------------------------------------------------------- /spec/fixtures/normal.csv: -------------------------------------------------------------------------------- 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 15 | -------------------------------------------------------------------------------- /spec/fixtures/trailing_leading_blank_lines.csv: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | ,,,,,, 7 | id,email,name,country,city,created_at,admin 8 | 102,patrick@intercom.io,Patrick O'Doherty,United States,San Francisco,2014-04-28 23:57:08 UTC,true 9 | 103,ben@intercom.io,Ben McRedmond,United States,San Francisco,2014-04-27 23:57:08 UTC,false 10 | 104,ciaran@intercom.io,Ciaran Lee,Ireland,Dublin,2014-04-26 23:57:08 UTC,true 11 | 105,eoghan@intercom.io,Eoghan McCabe,United States,San Francisco,2014-04-25 23:57:08 UTC,true 12 | 106,des@intercom.io,Des Traynor,Ireland,Dublin,2014-04-24 23:57:08 UTC,true 13 | ,,,,,, 14 | ,,,,,, 15 | ,,,,,, 16 | 107,darragh@intercom.io,Darragh Curran,Ireland,Dublin,2014-04-23 23:57:08 UTC,true 17 | 108,jeff@intercom.io,Jeff Gardner,Italy,Prata Camportaccio,2014-04-22 23:57:08 UTC,true 18 | 109,macey@intercom.io,Macey Baker,United States,San Francisco,2014-04-21 23:57:08 UTC,true 19 | 110,adam@intercom.io,Adam McCarthy,Dublin,Ireland,2014-04-20 23:57:08 UTC,true 20 | ,,,,,, 21 | 111,declan@intercom.io,Declan McGrath,Dublin,Ireland,2014-04-19 23:57:08 UTC,false 22 | 112,peter@intercom.io,Peter McKenna,Dublin,Ireland,2014-04-18 23:57:08 UTC,false 23 | 113,david@intercom.io,David Barett,Dublin,Ireland,2014-04-17 23:57:08 UTC,true 24 | 114,eoin@intercom.io,Eoin Hennessy,Dublin,Ireland,2014-04-16 23:57:08 UTC,false 25 | ,,,,,, 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # Ruby CircleCI 2.0 configuration file 2 | # 3 | # Check https://circleci.com/docs/2.0/language-ruby/ for more details 4 | # 5 | version: 2 6 | jobs: 7 | build: 8 | docker: 9 | # specify the version you desire here 10 | - image: circleci/ruby:2.2 11 | 12 | # Specify service dependencies here if necessary 13 | # CircleCI maintains a library of pre-built images 14 | # documented at https://circleci.com/docs/2.0/circleci-images/ 15 | # - image: circleci/postgres:9.4 16 | 17 | working_directory: ~/hippie_csv 18 | 19 | steps: 20 | - checkout 21 | 22 | - run: 23 | name: install dependencies 24 | command: | 25 | bundle install --jobs=4 --retry=3 26 | 27 | # run tests! 28 | - run: 29 | name: run tests 30 | command: | 31 | mkdir /tmp/test-results 32 | TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)" 33 | 34 | bundle exec rspec --format progress \ 35 | --out /tmp/test-results/rspec.xml \ 36 | --format progress \ 37 | $TEST_FILES 38 | 39 | # collect reports 40 | - store_test_results: 41 | path: /tmp/test-results 42 | - store_artifacts: 43 | path: /tmp/test-results 44 | destination: test-results 45 | -------------------------------------------------------------------------------- /spec/hippie_csv/constants_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | describe HippieCSV do 4 | 5 | describe "::QUOTE_CHARACTERS" do 6 | let(:supported_quote_characters) { ['"', "'", "|"] } 7 | 8 | it "supports common quote characters" do 9 | expect(HippieCSV::QUOTE_CHARACTERS).to include(*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 69 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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 non-compliant 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 three public methods: 35 | 1. `.read` a file path to an array. Reads from the file all at once, building the whole CSV object in memory. 36 | 2. `.parse` an in memory string to an array. 37 | 3. `.stream` from a file path and parse line by line, calling a given block on each row. 38 | 39 | **Note**: Processing large files using read or parse is a memory intensive operation. Use stream for parsing a CSV file line by line from the file to save memory. This method will use less memory but take longer, as we run each line through parse. 40 | 41 | 42 | ```ruby 43 | require 'hippie_csv' 44 | 45 | HippieCSV.read("path/to/data.csv") 46 | 47 | HippieCSV.stream("path/to/data.csv") do |row| 48 | # use row here... 49 | end 50 | 51 | HippieCSV.parse(csv_string) 52 | ``` 53 | 54 | ## Features 55 | 56 | - Deduces the delimiter (supports `,`, `;`, and `\t`) 57 | - Deduces the quote character (supports `'`, `"`, and `|`) 58 | - Forgives backslash escaped quotes in quoted CSVs 59 | - Forgives invalid newlines in quoted CSVs 60 | - Heals many encoding issues (and aggressively forces UTF-8) 61 | - Deals with many miscellaneous malformed types of CSVs 62 | - Works when a [byte order mark](https://en.wikipedia.org/wiki/Byte_order_mark) is present 63 | 64 | ## Contributing 65 | 66 | 1. Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet. 67 | 2. Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it. 68 | 3. Fork the project. 69 | 4. Start a feature/bugfix branch. 70 | 5. Commit and push until you are happy with your contribution. 71 | 6. Make sure to add tests for it. This is important so we don't break it in a future version unintentionally. 72 | 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. 73 | -------------------------------------------------------------------------------- /lib/hippie_csv/support.rb: -------------------------------------------------------------------------------- 1 | require "hippie_csv/constants" 2 | require "rchardet" 3 | 4 | module HippieCSV 5 | module Support 6 | class << self 7 | def encode(string) 8 | string = ensure_valid_encoding(string) 9 | 10 | DELIMETERS.each do |delimiter| 11 | string.gsub!(blank_line_regex(delimiter), "") 12 | end 13 | 14 | string.encode(string.encoding, universal_newline: true) 15 | end 16 | 17 | def maybe_parse(string) 18 | encoded_string = encode(string) 19 | 20 | QUOTE_CHARACTERS.find do |quote_character| 21 | [encoded_string, tolerate_escaping(encoded_string, quote_character), dump_quotes(encoded_string, quote_character)].find do |string_to_parse| 22 | rescuing_malformed do 23 | return parse_csv(string_to_parse.squeeze("\n").strip, quote_character) 24 | end 25 | end 26 | end 27 | end 28 | 29 | def parse_csv(string, quote_character) 30 | CSV.parse( 31 | string, 32 | quote_char: quote_character, 33 | col_sep: guess_delimeter(string, quote_character) 34 | ) 35 | end 36 | 37 | def maybe_stream(path, &block) 38 | File.foreach(path, encoding: ENCODING_WITH_BOM) do |line| 39 | row = maybe_parse(line) 40 | block.call(row.first) if row.first 41 | end 42 | end 43 | 44 | def dump_quotes(string, quote_character) 45 | string.gsub(quote_character, "") 46 | end 47 | 48 | def rescuing_malformed 49 | begin; yield; rescue CSV::MalformedCSVError; end 50 | end 51 | 52 | def tolerate_escaping(string, quote_character) 53 | string.gsub("\\#{quote_character}", "#{quote_character}#{quote_character}") 54 | end 55 | 56 | def guess_delimeter(string, quote_character) 57 | results = DELIMETERS.map do |delimeter| 58 | [delimeter, field_count(string, delimeter, quote_character)] 59 | end.max_by do |delimeter, count| 60 | count 61 | end.each do |delimiter, count| 62 | return delimiter 63 | end 64 | end 65 | 66 | private 67 | 68 | def ensure_valid_encoding(string) 69 | return string if string.valid_encoding? 70 | 71 | current_encoding = detect_encoding(string) 72 | 73 | if !current_encoding.nil? && current_encoding != ENCODING 74 | string.encode(ENCODING, current_encoding) 75 | else 76 | magical_encode(string) 77 | end 78 | rescue Encoding::InvalidByteSequenceError 79 | magical_encode(string) 80 | end 81 | 82 | def blank_line_regex(delimiter) 83 | /^#{delimiter}+(\r\n|\r)$/ 84 | end 85 | 86 | def detect_encoding(string) 87 | CharDet.detect(string[0..ENCODING_SAMPLE_CHARACTER_COUNT])["encoding"] 88 | end 89 | 90 | def magical_encode(string) 91 | string.encode(ALTERNATE_ENCODING, ENCODING, invalid: :replace, replace: "") 92 | .encode(ENCODING, ALTERNATE_ENCODING) 93 | end 94 | 95 | def field_count(file, delimeter, quote_character) 96 | csv = CSV.new(file, col_sep: delimeter, quote_char: quote_character) 97 | csv.lazy.take(FIELD_SAMPLE_COUNT).map(&:size).inject(:+) 98 | rescue CSV::MalformedCSVError 99 | 0 100 | end 101 | end 102 | end 103 | end 104 | -------------------------------------------------------------------------------- /spec/hippie_csv/support_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | 3 | describe HippieCSV::Support do 4 | 5 | describe ".encode" do 6 | context "with invalid byte sequence" do 7 | let(:string) { "\u0014\xFE\u000E\u0000" } 8 | 9 | it "would error normally" do 10 | expect { CSV.parse(string) }.to raise_error(ArgumentError, "invalid byte sequence in UTF-8") 11 | end 12 | 13 | it "works" do 14 | expect(string).not_to be_valid_encoding 15 | 16 | expect(subject.encode(string)).to be_valid_encoding 17 | end 18 | end 19 | 20 | context "with a string detected to be UTF-8 but with an invalid byte sequence" do 21 | let(:utf8_string) { ("Rubyのメ" * HippieCSV::ENCODING_SAMPLE_CHARACTER_COUNT).force_encoding("utf-8") } 22 | let(:string) { utf8_string << "\xBF"} 23 | 24 | it "ensures encoding becomes valid" do 25 | expect(string).not_to be_valid_encoding 26 | 27 | expect(subject.encode(string)).to be_valid_encoding 28 | end 29 | end 30 | 31 | context 'with unquoted fields and \r or \n' do 32 | let(:string) { "id,first_name,last_name\r123,Heinrich,Schütz\r\n" } 33 | 34 | it "would error normally" do 35 | expect { CSV.parse(string) }.to raise_error(CSV::MalformedCSVError, "Unquoted fields do not allow \\r or \\n (line 3).") 36 | end 37 | 38 | it "works" do 39 | result = CSV.parse(subject.encode(string)) 40 | 41 | rows, columns = result.size, result.first.size 42 | 43 | expect(rows).to eq(2) 44 | expect(columns).to eq(3) 45 | end 46 | end 47 | end 48 | 49 | describe ".maybe_parse" do 50 | let(:file_path) { fixture_path(:small_file) } 51 | it "works" do 52 | expect(subject.maybe_parse(File.read(file_path))).to eq( 53 | [["name", "email"], ["stephen", "test@example.com"]] 54 | ) 55 | end 56 | end 57 | 58 | describe ".maybe_stream" do 59 | let(:file_path) { fixture_path(:small_file) } 60 | it "works" do 61 | result = [] 62 | subject.maybe_stream(file_path) { |row| result << row } 63 | 64 | expect(result).to eq( 65 | [["name", "email"], ["stephen", "test@example.com"]] 66 | ) 67 | end 68 | end 69 | 70 | describe ".parse_csv" do 71 | let(:string) { "name,email\nstephen,test@example.com"} 72 | let(:quote_character) { "\"" } 73 | 74 | it "works" do 75 | expect(subject).to receive(:guess_delimeter).with(string, quote_character).and_return(',') 76 | 77 | expect(subject.parse_csv(string, quote_character)).to eq( 78 | [["name", "email"], ["stephen", "test@example.com"]] 79 | ) 80 | end 81 | end 82 | 83 | describe ".rescuing_malformed" do 84 | let(:error) { CSV::MalformedCSVError } 85 | let(:will_error) { -> { raise error } } 86 | 87 | it "would normally error" do 88 | expect { will_error.call }.to raise_error(error) 89 | end 90 | 91 | it "rescues from CSV::MalformedCSVError" do 92 | expect { subject.rescuing_malformed { will_error.call } }.not_to raise_error 93 | end 94 | end 95 | 96 | describe ".tolerate_escaping" do 97 | let(:string) { "'Stephen', 'O\\'Brien'"} 98 | let(:quote_character) { "'" } 99 | 100 | it "enforces escaping according to CSV spec" do 101 | expect(subject.tolerate_escaping(string, quote_character)).to eq("'Stephen', 'O''Brien'") 102 | end 103 | end 104 | 105 | describe ".guess_delimeter" do 106 | let(:string) { "'a'\t'b'\t'c'\n'd'\t'e'\t'f'\n" } 107 | let(:quote_character) { "'" } 108 | 109 | it "deduces the delimiter based on counts" do 110 | expect(subject.guess_delimeter(string, quote_character)).to eq("\t") 111 | end 112 | end 113 | end 114 | -------------------------------------------------------------------------------- /spec/fixtures/excel.csv: -------------------------------------------------------------------------------- 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 | ,,,,,,,,,,,,,,,,,,,,,,, 57 | -------------------------------------------------------------------------------- /spec/hippie_csv_spec.rb: -------------------------------------------------------------------------------- 1 | require "spec_helper" 2 | require "csv" 3 | 4 | describe HippieCSV do 5 | 6 | let(:string) { "test" } 7 | 8 | describe ".read" do 9 | 10 | it "reads and parses the file" do 11 | path = fixture_path(:normal) 12 | 13 | result = subject.read(path) 14 | expect(result.first[0..1]).to eq(["id", "email"]) 15 | end 16 | 17 | it "reads and parses the file with a byte order mark" do 18 | path = fixture_path(:with_byte_order_mark) 19 | 20 | result = subject.read(path) 21 | expect(result).to eq([["Name", "Email Address", "Date Added"]]) 22 | end 23 | 24 | end 25 | 26 | describe ".parse" do 27 | it "defers to support parse method" do 28 | result = double 29 | expect(subject::Support).to receive(:maybe_parse).with(string).and_return(result) 30 | 31 | expect(subject.parse(string)).to eq(result) 32 | end 33 | 34 | context "when unable to parse" do 35 | before do 36 | expect(subject::Support).to receive(:maybe_parse).and_return(nil) 37 | end 38 | 39 | it "raises an error" do 40 | expect { 41 | subject.parse(string) 42 | }.to raise_error( 43 | subject::UnableToParseError, 44 | "Something went wrong. Report this CSV: https://github.com/intercom/hippie_csv" 45 | ) 46 | end 47 | end 48 | end 49 | 50 | describe ".stream" do 51 | path = fixture_path(:normal) 52 | let(:proc) { Proc.new {} } 53 | 54 | it "encodes the string" do 55 | allow(subject::Support).to receive(:maybe_stream).and_return(double) 56 | 57 | subject.stream(path, &proc) 58 | end 59 | 60 | it "defers to support stream method" do 61 | result = double 62 | expect(subject::Support).to receive(:maybe_stream).with(path, &proc).and_return(result) 63 | expect(subject.stream(path, &proc)).to eq(result) 64 | end 65 | 66 | it "works" do 67 | path = fixture_path(:normal) 68 | 69 | result = [] 70 | subject.stream(path) { |row| result << row } 71 | expect(result[0]).to eq(["id", "email", "name", "country", "city", "created_at", "admin"]) 72 | end 73 | end 74 | 75 | context "integration cases: hard/encountered problems" do 76 | 77 | def read(path) 78 | subject.read(path) 79 | end 80 | 81 | def stream(path) 82 | [].tap do |rows| 83 | subject.stream(path) do |row| 84 | rows << row 85 | end 86 | end 87 | end 88 | 89 | def subject_call_method(method, path) 90 | send(method, path) 91 | end 92 | 93 | it "::read deals with a long, challenging file (and quickly)" do 94 | start_time = Time.now 95 | path = fixture_path(:never_ordered) 96 | 97 | import = subject.read(path) 98 | 99 | expect(import[0].count).to eq(10) 100 | expect(import.count).to eq(32803) 101 | expect(Time.now).to be_within(5).of(start_time) 102 | end 103 | 104 | %w[read stream].each do |method| 105 | it "::#{method} works when a BOM is present in the file" do 106 | path = fixture_path(:with_byte_order_mark) 107 | 108 | import = subject_call_method(method, path) 109 | expect(import[0]).to eq(["Name", "Email Address", "Date Added"]) 110 | end 111 | 112 | it "::#{method} works with a malformed CSV" do 113 | path = fixture_path(:malformed) 114 | expect { CSV.read(path) }.to raise_error(CSV::MalformedCSVError) 115 | 116 | import = subject_call_method(method, path) 117 | expect(import[0]).to eq(%w(site lon lat max min precip snow snowdepth)) 118 | end 119 | 120 | it "::#{method} works with odd encoding & emoji!" do 121 | path = fixture_path(:encoding) 122 | expect { CSV.read(path) }.to raise_error(ArgumentError) 123 | 124 | import = subject_call_method(method, path) 125 | expect(import[0].count).to eq(4) 126 | end 127 | 128 | it "::#{method} works with an excel export" do 129 | path = fixture_path(:excel) 130 | 131 | import = subject_call_method(method, path) 132 | expect(import[0].count).to eq(24) 133 | end 134 | 135 | it "::#{method} works with unescaped internal quotes" do 136 | path = fixture_path(:internal_quotes) 137 | 138 | import = subject_call_method(method, path) 139 | expect(import[1][1]).to eq("123") 140 | expect(import[1][2]).to eq("James Jimmy Doe") 141 | end 142 | 143 | it "::#{method} works with escaped quotes" do 144 | path = fixture_path(:escaped_quotes) 145 | 146 | import = subject_call_method(method, path) 147 | expect(import[0][1]).to eq("Lalo \"ElPapi\" Neymar") 148 | expect(import[0][2]).to eq("lalo@example.com") 149 | end 150 | 151 | it "::#{method} works with an invalid escaped quotes case" do 152 | path = fixture_path(:escaped_quotes_semicolons) 153 | 154 | import = subject_call_method(method, path) 155 | expect(import[0][0]).to eq("133") 156 | expect(import[0][1]).to eq("z3268856") 157 | expect(import[0][2]).to eq("stephen@example.com") 158 | end 159 | 160 | it "::#{method} works for a complicated case involving bad newlines and quote chars" do 161 | path = fixture_path(:dos_line_ending) 162 | 163 | import = subject_call_method(method, path) 164 | expect(import[0].count).to eq(9) 165 | end 166 | 167 | it "::#{method} works for a hard case" do 168 | path = fixture_path(:accents_semicolon_windows_1252) 169 | 170 | import = subject_call_method(method, path) 171 | expect(import[0][1]).to eq("Jérome") 172 | expect(import[1][0]).to eq("Héloise") 173 | end 174 | 175 | it "::#{method} works when many invalid quote types contained" do 176 | path = fixture_path(:bad_quoting) 177 | 178 | expect { 179 | import = subject_call_method(method, path) 180 | expect(import.map(&:count).uniq).to eq([11]) 181 | expect(import.count).to eq(8) 182 | }.not_to raise_error 183 | end 184 | 185 | it "::#{method} strips leading/trailing blank lines" do 186 | path = fixture_path(:trailing_leading_blank_lines) 187 | 188 | import = subject_call_method(method, path) 189 | expect(import.first).not_to be_empty 190 | expect(import.last).not_to be_empty 191 | end 192 | 193 | it "::#{method} maintains coherent column count when stripping blank lines" do 194 | [:blank_lines_crlf, :trailing_leading_blank_lines].each do |fixture_name| 195 | path = fixture_path(fixture_name) 196 | 197 | import = subject_call_method(method, path) 198 | expect(import.map(&:length).uniq.size).to eq(1) 199 | end 200 | end 201 | end 202 | end 203 | end 204 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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. 203 | --------------------------------------------------------------------------------