Browse Source

Added new column action, sender list is read from env var

master
rodley82 4 years ago
parent
commit
9ba310aeb4
  1. 4
      README.md
  2. 34
      app/interactors/monitor_inbox.rb
  3. 1
      db/migrate/20220307195441_add_action_logs_table.rb
  4. 1
      db/schema.rb

4
README.md

@ -26,7 +26,7 @@ Things you may want to cover:
### From the console ### 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 ### From the Rails console
@ -36,5 +36,5 @@ email = "email@outlook.com"
password = "micontrasenia" password = "micontrasenia"
res = FetchEmails.call(email: email, password: password, query_from: Time.now - 30.days) res = FetchEmails.call(email: email, password: password, query_from: Time.now - 30.days)
res = MonitorInbox.call!(email: email,password: password) res = MonitorInbox.call!
``` ```

34
app/interactors/monitor_inbox.rb

@ -1,17 +1,12 @@
class MonitorInbox class MonitorInbox
include Interactor include Interactor
AUTHORIZED_SENDERS = [ VALID_ACTIONS = [
"rodolfo.leyes@gmail.com",
"ing_rodolfo_utn@hotmail.com"
]
VALID_ACTION_TERMS = [
"encender", "encender",
"poweron",
] ]
def call def call
build_authorized_senders
context.actions_executed_count=0 context.actions_executed_count=0
email = context.email || ENV["EMAIL"] email = context.email || ENV["EMAIL"]
password = context.password || ENV["PASSWORD"] password = context.password || ENV["PASSWORD"]
@ -19,7 +14,6 @@ class MonitorInbox
fetch_emails_res = FetchEmails.call!( fetch_emails_res = FetchEmails.call!(
email: email, email: email,
password: password, password: password,
# TODO: We need the process to be intelligent and know when was it's last execution
query_from: calculate_from_time query_from: calculate_from_time
) )
@ -28,39 +22,51 @@ class MonitorInbox
context.analyzed_emails_count = emails_arr.count context.analyzed_emails_count = emails_arr.count
end 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 def calculate_from_time
latest_action_log = ActionLog.last latest_action_log = ActionLog.last
time = if latest_action_log time = if latest_action_log
latest_action_log.created_at latest_action_log.created_at
else else
Time.now Time.now - 5.minutes
end end
time time
end end
# Analiza uno a uno los elmentos del array de emails
def analyze_emails(emails_arr) def analyze_emails(emails_arr)
emails_arr.each do |email| emails_arr.each do |email|
puts "Analyzing email with subject: #{email.subject} From: #{email.from} On #{email.date}" puts "Analyzing email with subject: #{email.subject} From: #{email.from} On #{email.date}"
if is_action_email?(email) if is_action_email?(email)
ejecutar_accion(email) execute_action(email)
end end
end end
end end
# Verifica si un email es considerado basado en su subject y quien lo envio
def is_action_email?(email) 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 end
def ejecutar_accion(email) # Ejecuta la accion indicada en el email
def execute_action(email)
al=ActionLog.new( al=ActionLog.new(
from: email.from, from: email.from,
subject: email.subject, subject: email.subject,
created_at: email.date created_at: email.date,
action: "encender"
) )
if al.valid? if al.valid?
puts "Executing action..." puts "Executing action..."
command = ENV["COMANDO_ACCION"] command = ENV["COMMAND"]
system(command) system(command)
context.actions_executed_count+=1 context.actions_executed_count+=1
al.save! al.save!

1
db/migrate/20220307195441_add_action_logs_table.rb

@ -3,6 +3,7 @@ class AddActionLogsTable < ActiveRecord::Migration[7.0]
create_table :action_logs do |t| create_table :action_logs do |t|
t.string :from t.string :from
t.string :subject t.string :subject
t.string :action
t.timestamps t.timestamps
end end
end end

1
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| create_table "action_logs", force: :cascade do |t|
t.string "from" t.string "from"
t.string "subject" t.string "subject"
t.string "action"
t.datetime "created_at", null: false t.datetime "created_at", null: false
t.datetime "updated_at", null: false t.datetime "updated_at", null: false
end end

Loading…
Cancel
Save