Class: OutlookImporter

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

Instance Method Summary collapse

Constructor Details

#initialize(mapping = nil) ⇒ OutlookImporter

Returns a new instance of OutlookImporter.



5
6
7
8
9
10
# File 'lib/outlook_importer.rb', line 5

def initialize(mapping = nil)
  if mapping
    @mapping = mapping.invert
    raise ArgumentError unless valid_arguments?
  end
end

Instance Method Details

#assign_header_columns(headers) ⇒ Object



20
21
22
23
24
25
# File 'lib/outlook_importer.rb', line 20

def assign_header_columns(headers)
  @header_column_indices = {}
  @mapping.keys.each do |key|
    @header_column_indices[@mapping[key]] = headers.index(key)
  end  
end

#contact_entry(row) ⇒ Object



35
36
37
38
39
40
# File 'lib/outlook_importer.rb', line 35

def contact_entry(row)
  [
    contact_entry_name(row),
    row[@header_column_indices[:email]]
  ]
end

#contact_entry_name(row) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/outlook_importer.rb', line 42

def contact_entry_name(row)
  if joined_arguments?
    row[@header_column_indices[:name]]
  else
    "#{row[@header_column_indices[:firstname]]} #{row[@header_column_indices[:lastname]]}"
  end
end

#contactsObject



27
28
29
30
31
32
33
# File 'lib/outlook_importer.rb', line 27

def contacts
  _contacts = []
  @csv.each do |row|
    _contacts << contact_entry(row)
  end
  _contacts
end

#find_lookup_tableObject



76
77
78
79
80
# File 'lib/outlook_importer.rb', line 76

def find_lookup_table
  lookup_tables.each do |key, mapping|
    return mapping if mapping.values - @csv.headers == []        
  end
end

#joined_arguments?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/outlook_importer.rb', line 72

def joined_arguments?
  @joined_arguments ||= @header_column_indices.include?(:name)
end

#lookup_tablesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/outlook_importer.rb', line 82

def lookup_tables
  {
    :livemail =>
      {
        :name => 'Name',
        :email => 'E-Mail-Adresse'
      },
    :outlook =>
      {
        :firstname => 'Vorname',
        :lastname => 'Nachname',
        :email => 'E-Mail-Adresse'
      }
  }
end

#read(file_or_path) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/outlook_importer.rb', line 12

def read(file_or_path)
  file = file_or_path.respond_to?(:path) ? file_or_path : File.new(file_or_path)
  separator = file.gets.match(/([,;])[^,;]+\n/)[1]
  @csv = FasterCSV.read(file.path, :headers => true, :col_sep => separator)
  @mapping = find_lookup_table.invert unless @mapping
  assign_header_columns(@csv.headers)
end

#required_keys_joinedObject



68
69
70
# File 'lib/outlook_importer.rb', line 68

def required_keys_joined
  [:name, :email]
end

#required_keys_present?Boolean

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/outlook_importer.rb', line 59

def required_keys_present?
  values = @mapping.keys
  values.select{|val| val && val.length > 0}.length == values.length
end

#required_keys_split_upObject



64
65
66
# File 'lib/outlook_importer.rb', line 64

def required_keys_split_up
  [:firstname, :lastname, :email]
end

#required_values_present?Boolean

Returns:

  • (Boolean)


54
55
56
57
# File 'lib/outlook_importer.rb', line 54

def required_values_present?
  required_keys_split_up - @mapping.values == [] ||
    required_keys_joined - @mapping.values == []
end

#valid_arguments?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/outlook_importer.rb', line 50

def valid_arguments?
  required_values_present? && required_keys_present?
end