lib/utils.rb in SimBot-0.1.19 vs lib/utils.rb in SimBot-0.1.20

- old
+ new

@@ -127,10 +127,35 @@ rescue ::Exception => e $stderr.puts "While signaling the PID, unexpected #{e.class}: #{e}" false end + def self.send_mail(data_file, template_file, email, password) + m = Mailer.new(email, password) + data = read_from_spreadsheet(data_file) + data.delete_at(0) + data.each do |row| + File.open(template_file, "r") do |f| + m.send(row[2], row[3], 'jon@ovex.io', 'COLLABORATION', find_and_replace(f, '{var}', row)) + puts "Sent mail to #{row[2]}" + end + end + end + + def self.find_and_replace(file, replce_string, data) + i = 0 + result = '' + file.each_line do |line| + if line.include? "{var}" + line = line.gsub(replce_string, data[i]) + i += 1 + end + result += line + end + result + end + # @return [Array] cmc data def self.cmc_data data = [] (0...16).each do |start| response = RestClient.get("https://api.coinmarketcap.com/v2/ticker/?start=#{start * 100 + 1}") @@ -161,7 +186,21 @@ rc += 1 end book.write(name) puts "Data written to #{name}" end + + # @param [String] name + # @return [Array] + def self.read_from_spreadsheet(name) + data = [] + book = Spreadsheet.open(name) + sheet1 = book.worksheet(0) # can use an index or worksheet name + sheet1.each do |row| + break if row[0].nil? # if first cell empty + data.push(row) + end + data + end + end