Skip to content

Instantly share code, notes, and snippets.

@isaldin
Last active August 29, 2015 14:15
Show Gist options
  • Save isaldin/c62b1d31fce11b9da25f to your computer and use it in GitHub Desktop.
Save isaldin/c62b1d31fce11b9da25f to your computer and use it in GitHub Desktop.
require 'csv'
def parse(filename)
result_array = []
current_record = {}
File.open(filename).each do |line|
# линия с '# name' в начале считается началом новой записи
if line.index('# name') == 0
result_array << current_record unless current_record.empty?
current_record = {}
end
# для строки 'zimbraAdminAuthTokenLifetime: 12h' key => 'zimbraAdminAuthTokenLifetime', value > '12h'
# последовательность ': ' взятa за делиметер
key, value = line.split(': ')
if %w(cn displayName mail title zimbraAccountStatus zimbraLastLogonTimestamp).include? key
# если у объекта уже есть mail, просто дописываем новый через запятую
if key == 'mail' && current_record['mail']
current_record[key] = "#{current_record[key]},#{value}"
elsif key == 'zimbraLastLogonTimestamp'
if value.length > 5
current_record[key] = "#{value[0..3]} #{value[4..5]} #{value[6..7]}"
end
else
current_record[key] = value
end
end
end
result_array
end
#entry point
%w(medicare-pm.kz medicare.kz mediker.kz meiyrim.kz).each do |filename|
File.open("#{filename}.csv", 'w') do |file|
file << "#{'ФИО;Отображаемое имя;mail;должность;статус;последний_вход;'}\n"
parse(filename).each do |rec|
new_row = "#{rec['cn']};#{rec['displayName']};#{rec['mail']};#{rec['title']};#{rec['zimbraAccountStatus']};#{rec['zimbraLastLogonTimestamp']};".gsub(/\n/, '').gsub(/\. /, ' ').gsub("\"", "'")
file << "#{new_row}\n"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment