|
|
|
@ -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! |
|
|
|
|