Class: ContactSport::OutlookReader
- Inherits:
-
Object
- Object
- ContactSport::OutlookReader
- Defined in:
- lib/contact_sport/outlook_reader.rb
Overview
To read an Outlook “Comma Separated Values (Windows)” file we need to guess:
-
the file’s encoding
-
the column separator
-
the column headings
These all vary by Outlook version, platform, and phase of the moon.
Some example contact headings are given here:
Instance Attribute Summary collapse
-
#column_separator ⇒ Object
readonly
Returns the value of attribute column_separator.
-
#file ⇒ Object
readonly
Returns the value of attribute file.
-
#file_encoding ⇒ Object
readonly
Returns the value of attribute file_encoding.
Instance Method Summary collapse
- #contacts ⇒ Object
-
#initialize(file) ⇒ OutlookReader
constructor
A new instance of OutlookReader.
Constructor Details
#initialize(file) ⇒ OutlookReader
Returns a new instance of OutlookReader.
22 23 24 25 26 |
# File 'lib/contact_sport/outlook_reader.rb', line 22 def initialize(file) @file = file @file_encoding = ContactSport::Encoding.guess_encoding file @column_separator = guess_column_separator end |
Instance Attribute Details
#column_separator ⇒ Object (readonly)
Returns the value of attribute column_separator.
20 21 22 |
# File 'lib/contact_sport/outlook_reader.rb', line 20 def column_separator @column_separator end |
#file ⇒ Object (readonly)
Returns the value of attribute file.
20 21 22 |
# File 'lib/contact_sport/outlook_reader.rb', line 20 def file @file end |
#file_encoding ⇒ Object (readonly)
Returns the value of attribute file_encoding.
20 21 22 |
# File 'lib/contact_sport/outlook_reader.rb', line 20 def file_encoding @file_encoding end |
Instance Method Details
#contacts ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/contact_sport/outlook_reader.rb', line 28 def contacts begin @contacts ||= begin results = [] CSV.foreach(file, headers: true, col_sep: column_separator, encoding: "#{file_encoding.to_s}:UTF-8") do |row| # Headers: results << Contact.new( first_name: first_name(row), last_name: last_name(row), name: name(row), email: email(row), url: web_page(row), company: company(row), office_phone: office_phone(row), mobile_phone: mobile_phone(row), fax: fax(row), address1: street(row), address2: street2(row), city: city(row), region: state(row), postcode: postcode(row), country: country(row) ) end results end rescue EncodingError => e raise ContactSport::EncodingError, e. rescue StandardError => e raise ContactSport::FormatError, e. end end |