class ACSConverter def initialize(options) @options = options end def straight { "FamilyNumber" => "legacy_family_id", "LastName" => "last_name", "Suffix" => "suffix", "Address1" => "family_address1", "Address2" => "family_address2", "City" => "family_city", "State" => "family_state", "ZIPCode" => "family_zip", "DateOfBirth" => "birthday", "HomeEmailAddr" => "email", "FamilyName" => "family_name", "Gender" => "gender" } end def convert(records) create_family_names(records) create_family_sequences(records) records.map do |record| convert_record(record) end end def convert_record(record) returning({}) do |new_record| record.each do |key, value| if new_key = straight[key] new_record[new_key] = value end end new_record['legacy_id'] = new_record['legacy_family_id'] + record['IndividualNumber'] new_record['first_name'] = get_first_name(record) new_record['family_home_phone'] = get_phone(record, 'Home') new_record['work_phone'] = get_phone(record, 'Business') new_record['mobile_phone'] = get_phone(record, 'Cell') new_record['fax'] = get_phone(record, 'FAX') new_record['family_last_name'] = new_record['last_name'] new_record['family_name'] = @family_names[new_record['legacy_family_id']] new_record['visible_to_everyone'] = @options['visible'].include?(record['MemberStatus']) new_record['visible_on_printed_directory'] = @options['visible_on_printed_directory'].include?(record['MemberStatus']) new_record['full_access'] = @options['full_access'].include?(record['MemberStatus']) new_record['can_sign_in'] = @options['full_access'].include?(record['MemberStatus']) new_record['email'] = record['HomeEmailAddr'].to_s.any? ? record['HomeEmailAddr'] : record['BusinessEmailAddr'] new_record['child'] = new_record['birthday'].to_s.empty? ? record['FamilyPosition'] == 'Child' : nil new_record['sequence'] = @families_by_sequence[new_record['legacy_family_id']].index(record['IndividualNumber'].to_i) + 1 end end def create_family_names(records) # this could probably be less messy @families = {} records.each do |record| @families[record['FamilyNumber']] ||= {} @families[record['FamilyNumber']][record['FamilyPosition']] = record end @family_names = {} @families.each do |family_number, family| family['Head'] ||= family.delete('Spouse') || family.delete('Child') if family['Head'] if family['Spouse'] @family_names[family_number] = "#{get_first_name(family['Head'])} & #{get_first_name(family['Spouse'])} #{family['Head']['LastName']}" else @family_names[family_number] = "#{get_first_name(family['Head'])} #{family['Head']['LastName']}" end else @family_names[family_number] = "#{get_first_name(family['Other'])} #{family['Other']['LastName']}" end end end def create_family_sequences(records) @families_by_sequence = {} records.each do |record| @families_by_sequence[record['FamilyNumber']] ||= [] @families_by_sequence[record['FamilyNumber']] << record['IndividualNumber'].to_i end @families_by_sequence.each { |k, v| v.sort! } end def get_first_name(record) record['GoesByName'].to_s.any? ? record['GoesByName'] : record['FirstName'] end def get_phone(record, type) unless record[type + 'Unlisted'].to_s.downcase == 'true' phone = record[type + 'Phone'].to_s.scan(/\d/).join phone << ' ' + record[type + 'Extension'] if record[type + 'Extension'].to_s.any? phone end end end