Browse Source

Improved fully working version

master
rodley82 4 years ago
parent
commit
fe420af44c
  1. 3
      README.md
  2. 48
      app/interactors/monitor_inbox.rb
  3. 3
      app/models/action_log.rb
  4. 10
      db/migrate/20220307195441_add_action_logs_table.rb
  5. 21
      db/schema.rb
  6. 7
      lib/tasks/monitor.rake
  7. 11
      test/fixtures/action_logs.yml
  8. 7
      test/models/action_log_test.rb

3
README.md

@ -25,7 +25,10 @@ Things you may want to cover:
### From the console ### From the console
```
EMAIL=email@outlook.com PASSWORD=micontrasenia bundle exec rake monitor:run EMAIL=email@outlook.com PASSWORD=micontrasenia bundle exec rake monitor:run
```
### From the Rails console ### From the Rails console
``` ```

48
app/interactors/monitor_inbox.rb

@ -2,9 +2,17 @@ class MonitorInbox
include Interactor include Interactor
AUTHORIZED_SENDERS = [ AUTHORIZED_SENDERS = [
"rodolfo.leyes@gmail.com" "rodolfo.leyes@gmail.com",
"ing_rodolfo_utn@hotmail.com"
] ]
VALID_ACTION_TERMS = [
"encender",
"poweron",
]
def call def call
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"]
@ -12,11 +20,22 @@ class MonitorInbox
email: email, email: email,
password: password, password: password,
# TODO: We need the process to be intelligent and know when was it's last execution # TODO: We need the process to be intelligent and know when was it's last execution
query_from: Time.now - 3.days query_from: calculate_from_time
) )
context.emails_arr = fetch_emails_res.messages emails_arr = fetch_emails_res.messages
analyze_emails(context.emails_arr) analyze_emails(emails_arr)
context.analyzed_emails_count = emails_arr.count
end
def calculate_from_time
latest_action_log = ActionLog.last
time = if latest_action_log
latest_action_log.created_at
else
Time.now
end
time
end end
def analyze_emails(emails_arr) def analyze_emails(emails_arr)
@ -29,11 +48,26 @@ class MonitorInbox
end end
def is_action_email?(email) def is_action_email?(email)
# For now we only recognize the encender action VALID_ACTION_TERMS.include?(email.subject.downcase) && AUTHORIZED_SENDERS.include?(email.from.downcase)
email.subject.downcase.include?("encender") && AUTHORIZED_SENDERS.include?(email.from.downcase)
end end
def ejecutar_accion(email) def ejecutar_accion(email)
al=ActionLog.new(
from: email.from,
subject: email.subject,
created_at: email.date
)
if al.valid?
puts "Executing action..."
command = ENV["COMANDO_ACCION"]
system(command)
context.actions_executed_count+=1
al.save!
else
# We're using the validate uniqueness of ActionLog to detect duplicate actions
# we have already processed if we get this exception we should not execute it
puts "We have already executed this action"
end
end end
end end

3
app/models/action_log.rb

@ -0,0 +1,3 @@
class ActionLog < ApplicationRecord
validates :created_at, uniqueness: true
end

10
db/migrate/20220307195441_add_action_logs_table.rb

@ -0,0 +1,10 @@
class CreateActionLogs < ActiveRecord::Migration[7.0]
def change
create_table :action_logs do |t|
t.string :from
t.string :subject
t.timestamps
end
end
end

21
db/schema.rb

@ -0,0 +1,21 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.
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.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end

7
lib/tasks/monitor.rake

@ -1,6 +1,9 @@
namespace :monitor do namespace :monitor do
desc "executes the monitoring logic" desc "executes the monitoring logic"
task :run => :environment do task :run => :environment do
MonitorInbox.call! puts "About to check #{ENV["EMAIL"]} inbox..."
res = MonitorInbox.call!
puts "A total of #{res.analyzed_emails_count} new emails were analyzed"
puts "A total of #{res.actions_executed_count} actions were triggered"
end end
end end

11
test/fixtures/action_logs.yml

@ -0,0 +1,11 @@
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
# This model initially had no columns defined. If you add columns to the
# model remove the "{}" from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value

7
test/models/action_log_test.rb

@ -0,0 +1,7 @@
require "test_helper"
class ActionLogTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
Loading…
Cancel
Save