Class: Smartcsv::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/smartcsv/parser.rb

Class Method Summary collapse

Class Method Details

.get_encoding(file) ⇒ Object



7
8
9
# File 'lib/smartcsv/parser.rb', line 7

def self.get_encoding(file)
  @encoding ||= File.read(file).encoding.name
end

.get_separator(file) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/smartcsv/parser.rb', line 11

def self.get_separator(file)
  line = IO.readlines(file, encoding: get_encoding(file))[0]

  h = {}
  h["comma"] = line.split(',').count
  h["semicolon"] = line.split(';').count
  h["tab"] = line.split("\t").count

  case h.max{|a,b| a[1] <=> b[1]}[0]
  when "semicolon" then ';'
  when "tab" then "\t"
  else ','
  end
end

.read(file) ⇒ Object



3
4
5
# File 'lib/smartcsv/parser.rb', line 3

def self.read(file)
  CSV.read(file,{encoding: get_encoding(file), col_sep: get_separator(file)})
end