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

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

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|
t.string :from
t.string :subject
t.string :action
t.timestamps
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|
t.string "from"
t.string "subject"
t.string "action"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

Loading…
Cancel
Save