From 9ba310aeb474185b44333b9fde26176113d284e7 Mon Sep 17 00:00:00 2001 From: rodley82 Date: Tue, 8 Mar 2022 00:13:15 -0300 Subject: [PATCH] Added new column action, sender list is read from env var --- README.md | 4 +-- app/interactors/monitor_inbox.rb | 34 +++++++++++-------- .../20220307195441_add_action_logs_table.rb | 1 + db/schema.rb | 1 + 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index c7c2a47..ae36996 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ Things you may want to cover: ### From the console ``` -EMAIL=email@outlook.com PASSWORD=micontrasenia bundle exec rake monitor:run +COMMAND="echo ejecutada" AUTHORIZED_SENDERS="sender1@email.com,sender2@email.com" EMAIL=email@outlook.com PASSWORD=micontrasenia bundle exec rake monitor:run ``` ### From the Rails console @@ -36,5 +36,5 @@ email = "email@outlook.com" password = "micontrasenia" res = FetchEmails.call(email: email, password: password, query_from: Time.now - 30.days) -res = MonitorInbox.call!(email: email,password: password) +res = MonitorInbox.call! ``` \ No newline at end of file diff --git a/app/interactors/monitor_inbox.rb b/app/interactors/monitor_inbox.rb index 83dde42..7c370e9 100644 --- a/app/interactors/monitor_inbox.rb +++ b/app/interactors/monitor_inbox.rb @@ -1,17 +1,12 @@ class MonitorInbox include Interactor - AUTHORIZED_SENDERS = [ - "rodolfo.leyes@gmail.com", - "ing_rodolfo_utn@hotmail.com" - ] - - VALID_ACTION_TERMS = [ + VALID_ACTIONS = [ "encender", - "poweron", ] def call + build_authorized_senders context.actions_executed_count=0 email = context.email || ENV["EMAIL"] password = context.password || ENV["PASSWORD"] @@ -19,7 +14,6 @@ class MonitorInbox fetch_emails_res = FetchEmails.call!( email: email, password: password, - # TODO: We need the process to be intelligent and know when was it's last execution query_from: calculate_from_time ) @@ -28,39 +22,51 @@ class MonitorInbox context.analyzed_emails_count = emails_arr.count end + # Construye un array de direcciones de email a partir del contenido de la variable + # de entorno AUTHORIZED_SENDERS que contendra una lista de emails separados por coma + def build_authorized_senders + context.authorized_senders = ENV["AUTHORIZED_SENDERS"].split(",") + end + + # Si existe un registro de ActionLog entonces usar el created_at time + # sino utilizar ahora def calculate_from_time latest_action_log = ActionLog.last time = if latest_action_log latest_action_log.created_at else - Time.now + Time.now - 5.minutes end time end + # Analiza uno a uno los elmentos del array de emails def analyze_emails(emails_arr) emails_arr.each do |email| puts "Analyzing email with subject: #{email.subject} From: #{email.from} On #{email.date}" if is_action_email?(email) - ejecutar_accion(email) + execute_action(email) end end end + # Verifica si un email es considerado basado en su subject y quien lo envio def is_action_email?(email) - VALID_ACTION_TERMS.include?(email.subject.downcase) && AUTHORIZED_SENDERS.include?(email.from.downcase) + VALID_ACTIONS.include?(email.subject.downcase) && context.authorized_senders.include?(email.from.downcase) end - def ejecutar_accion(email) + # Ejecuta la accion indicada en el email + def execute_action(email) al=ActionLog.new( from: email.from, subject: email.subject, - created_at: email.date + created_at: email.date, + action: "encender" ) if al.valid? puts "Executing action..." - command = ENV["COMANDO_ACCION"] + command = ENV["COMMAND"] system(command) context.actions_executed_count+=1 al.save! diff --git a/db/migrate/20220307195441_add_action_logs_table.rb b/db/migrate/20220307195441_add_action_logs_table.rb index 4b23315..91edd78 100644 --- a/db/migrate/20220307195441_add_action_logs_table.rb +++ b/db/migrate/20220307195441_add_action_logs_table.rb @@ -3,6 +3,7 @@ class AddActionLogsTable < ActiveRecord::Migration[7.0] create_table :action_logs do |t| t.string :from t.string :subject + t.string :action t.timestamps end end diff --git a/db/schema.rb b/db/schema.rb index 1163184..0d7c5f7 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -14,6 +14,7 @@ ActiveRecord::Schema[7.0].define(version: 2022_03_07_195441) do create_table "action_logs", force: :cascade do |t| t.string "from" t.string "subject" + t.string "action" t.datetime "created_at", null: false t.datetime "updated_at", null: false end