Sha256: d0268424accd4b8a0948d55db0b25dd29241e9d9764a91312c36b3ad9e9c1b88
Contents?: true
Size: 1.77 KB
Versions: 3
Compression:
Stored size: 1.77 KB
Contents
require 'fastercsv' ## # Imports contacts from a CSV file class Blackbook::Importer::Csv < Blackbook::Importer::Base DEFAULT_COLUMNS = [:name,:email,:misc] ## # Matches this importer to a file that contains CSV values def =~(options) options && options[:file].respond_to?(:open) ? true : false end ## # fetch_contacts! implementation for this importer def fetch_contacts! a = open(options[:file].base_uri.to_s) lines_ = Array.new lines = a.readlines first_line = lines.first if first_line.is_binary_data? lines.each { |e| s = '' unless e == lines.first u = "\377\376N" << e.strip e = u s << e.toutf8 s.slice!(0) else s << e.toutf8 end lines_ << s } lines = lines_ end # lines.each { |e| # if e.is_binary_data? # e = e.toutf8 # end # } columns = to_columns(lines.first) lines.shift if columns.first == :name columns = DEFAULT_COLUMNS.dup unless columns.first == :name contacts = Array.new lines.each do |l| vals = l.split(',') next if vals.empty? contacts << to_hash(columns, vals) end contacts end def to_hash(cols, vals) # :nodoc: h = Hash.new cols.each do |c| h[c] = (c == cols.last) ? vals.join(',') : vals.shift end h end def to_columns(line) # :nodoc: if line columns = Array.new tags = line.split(',') # deal with "Name,E-mail..." oddity up front if tags.first =~ /^name$/i tags.shift columns << :name if tags.first =~ /^e.?mail/i # E-mail or Email tags.shift columns << :email end end tags.each{|v| columns << v.strip.to_sym} columns end end Blackbook.register(:csv, self) end
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
alexjp-blackbook_csv-1.0.5 | lib/blackbook/importer/csv.rb |
alexjp-blackbook_csv-3.0.1 | lib/blackbook/importer/csv.rb |
alexjp-blackbook_csv-3.0.2 | lib/blackbook/importer/csv.rb |