From c6b0d4ee7496a0c2ae6b696af3c1ebf1c491eac4 Mon Sep 17 00:00:00 2001 From: bitsapien Date: Fri, 27 Nov 2015 22:39:24 +0530 Subject: [PATCH] fixed ruby script * corrected `email.body` to `email.body.raw_source` * moved script to `main` method * defined global variables(KUMARS_EMAIL and GMAIL) which need to be available across methods --- kumar_asshole.rb | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/kumar_asshole.rb b/kumar_asshole.rb index ca5b9f6..89bd6ca 100755 --- a/kumar_asshole.rb +++ b/kumar_asshole.rb @@ -8,32 +8,36 @@ Dotenv.load GMAIL_USERNAME = ENV['GMAIL_USERNAME'] GMAIL_PASSWORD = ENV['GMAIL_PASSWORD'] -gmail = Gmail.connect(GMAIL_USERNAME, GMAIL_PASSWORD) -kumars_email = 'kumar.a@example.com' +GMAIL = Gmail.connect(GMAIL_USERNAME, GMAIL_PASSWORD) +KUMARS_EMAIL = 'kumar.a@example.com' DB_NAME_REGEX = /\S+_staging/ KEYWORDS_REGEX = /sorry|help|wrong/i -gmail.inbox.find(:unread, from: kumars_email).each do |email| - if email.body[KEYWORDS_REGEX] && (db_name = email.body[DB_NAME_REGEX]) - backup_file = "/home/backups/databases/#{db_name}-" + (Date.today - 1).strftime('%Y%m%d') + '.gz' - abort 'ERROR: Backup file not found' unless File.exist?(backup_file) +def main + GMAIL.inbox.find(:unread, from: KUMARS_EMAIL).each do |email| + if email.body.raw_source[KEYWORDS_REGEX] && (db_name = email.body.raw_source[DB_NAME_REGEX]) + backup_file = "/home/backups/databases/#{db_name}-" + (Date.today - 1).strftime('%Y%m%d') + '.gz' + abort 'ERROR: Backup file not found' unless File.exist?(backup_file) - # Restore DB - `gunzip -c #{backup_file} | psql #{db_name}` + # Restore DB + `gunzip -c #{backup_file} | psql #{db_name}` - # Mark as read, add label and reply - email.read! - email.label('Database fixes') - reply = create_reply(email.subject) - gmail.deliver(reply) - end + # Mark as read, add label and reply + email.read! + email.label('Database fixes') + reply = create_reply(email.subject) + GMAIL.deliver(reply) + end + end end def create_reply(subject) - gmail.compose do - to kumars_email + GMAIL.compose do + to KUMARS_EMAIL subject "RE: #{subject}" body "No problem. I've fixed it. \n\n Please be careful next time." end end + +main # executes main method \ No newline at end of file