diff --git a/README.md b/README.md index 69b6f87..c7c2a47 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,10 @@ Things you may want to cover: ### From the console +``` EMAIL=email@outlook.com PASSWORD=micontrasenia bundle exec rake monitor:run +``` + ### From the Rails console ``` diff --git a/app/interactors/monitor_inbox.rb b/app/interactors/monitor_inbox.rb index 6441dd8..83dde42 100644 --- a/app/interactors/monitor_inbox.rb +++ b/app/interactors/monitor_inbox.rb @@ -2,9 +2,17 @@ class MonitorInbox include Interactor AUTHORIZED_SENDERS = [ - "rodolfo.leyes@gmail.com" + "rodolfo.leyes@gmail.com", + "ing_rodolfo_utn@hotmail.com" ] + + VALID_ACTION_TERMS = [ + "encender", + "poweron", + ] + def call + context.actions_executed_count=0 email = context.email || ENV["EMAIL"] password = context.password || ENV["PASSWORD"] @@ -12,11 +20,22 @@ class MonitorInbox email: email, password: password, # 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 - analyze_emails(context.emails_arr) + emails_arr = fetch_emails_res.messages + 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 def analyze_emails(emails_arr) @@ -29,11 +48,26 @@ class MonitorInbox end def is_action_email?(email) - # For now we only recognize the encender action - email.subject.downcase.include?("encender") && AUTHORIZED_SENDERS.include?(email.from.downcase) + VALID_ACTION_TERMS.include?(email.subject.downcase) && AUTHORIZED_SENDERS.include?(email.from.downcase) end 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 diff --git a/app/models/action_log.rb b/app/models/action_log.rb new file mode 100644 index 0000000..e5953b6 --- /dev/null +++ b/app/models/action_log.rb @@ -0,0 +1,3 @@ +class ActionLog < ApplicationRecord + validates :created_at, uniqueness: true +end diff --git a/db/migrate/20220307195441_add_action_logs_table.rb b/db/migrate/20220307195441_add_action_logs_table.rb new file mode 100644 index 0000000..cf4f9a2 --- /dev/null +++ b/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 + diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..1163184 --- /dev/null +++ b/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 diff --git a/lib/tasks/monitor.rake b/lib/tasks/monitor.rake index 6d77b93..18ba898 100644 --- a/lib/tasks/monitor.rake +++ b/lib/tasks/monitor.rake @@ -1,6 +1,9 @@ namespace :monitor do desc "executes the monitoring logic" 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 \ No newline at end of file +end diff --git a/test/fixtures/action_logs.yml b/test/fixtures/action_logs.yml new file mode 100644 index 0000000..d7a3329 --- /dev/null +++ b/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 diff --git a/test/models/action_log_test.rb b/test/models/action_log_test.rb new file mode 100644 index 0000000..4fd1024 --- /dev/null +++ b/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