Class: Merger
Instance Method Summary collapse
- #construct_emails_hash ⇒ Object
-
#initialize(input_dir, output_path, mapping = nil) ⇒ Merger
constructor
A new instance of Merger.
- #merge ⇒ Object
Methods included from CSVHandlers
#append_to_csv, #create_file, #create_file_with_headers, #create_row, #get_headers
Constructor Details
#initialize(input_dir, output_path, mapping = nil) ⇒ Merger
Returns a new instance of Merger.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/linsc/merger.rb', line 7 def initialize(input_dir, output_path, mapping = nil) @input_dir, @output_path, @mapping = input_dir, output_path, mapping recruiter_file = Pathname.new(File.dirname __dir__).realdirpath + '../data/recruiters.txt' @recruiters = recruiter_file.read.split(",").collect{|r| r.strip} unless @recruiters && @recruiters.length > 0 puts "recruiters.txt is empty! refer to the docs for instructions on filling it" exit end @lin_files = @input_dir.children.select{|fn| fn.basename.to_s.match(/^LIN.+\.csv/)} if mapping @headers = mapping.values else @headers = get_headers(@lin_files.first) end if File.exist?(@output_path) File.delete(@output_path) end create_file(@output_path) end |
Instance Method Details
#construct_emails_hash ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/linsc/merger.rb', line 27 def construct_emails_hash emails = {} @lin_files.each do |pn| lin_file = pn.to_s recruiter_name = pn.basename.to_s.match(/LIN[^.]+/)[0] puts "merging #{recruiter_name}" clean_file = File.read(lin_file, encoding: 'windows-1252').strip CSV.parse(clean_file, headers: true, encoding: 'windows-1252') do |row| row["Recruiter"] = recruiter_name email = row['EmailAddress'].downcase if row['EmailAddress'] if emails.has_key?(email) emails[email] << row else emails[email] = [row] end end end emails end |
#merge ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/linsc/merger.rb', line 47 def merge emails = construct_emails_hash i = 0 j = emails.length emails.each do |ek, ev| i += 1 puts "merging - row #{i}/#{j}" correct_row = ev.find do |row| row['Recruiter'] == @recruiters.find do |rec| ev.collect {|row| row['Recruiter']}.include?(rec) end end if @mapping output_row = CSV::Row.new(@headers, []) correct_row.each do |key, value| if @mapping[key] output_row[@mapping[key]] = value.encode('utf-8', invalid: :replace, undef: :replace, replace: '?') if value end end output_row['Email'] = output_row['Email'].downcase if output_row['Email'] else output_row = create_row(correct_row, @headers, 'utf-8') end append_to_csv(@output_path, output_row) end @output_path end |