From 8acf4e5bb981df8484761763bec4090ef83bf7a2 Mon Sep 17 00:00:00 2001 From: rodley82 Date: Mon, 7 Mar 2022 16:15:11 -0300 Subject: [PATCH] Removed some unused gem dependencies as this will not be serving web content at all Implemented the MonitorInbox interactor that should detect and execute the actions A new rake task monitor:run has been implemented for execution from the console --- Gemfile | 12 ++++---- Gemfile.lock | 19 ------------- README.md | 10 ++++++- app/interactors/fetch_emails.rb | 3 +- app/interactors/monitor_inbox.rb | 39 ++++++++++++++++++++++++++ lib/tasks/monitor.rake | 6 ++++ spec/interactors/monitor_inbox_spec.rb | 7 +++++ 7 files changed, 69 insertions(+), 27 deletions(-) create mode 100644 app/interactors/monitor_inbox.rb create mode 100644 lib/tasks/monitor.rake create mode 100644 spec/interactors/monitor_inbox_spec.rb diff --git a/Gemfile b/Gemfile index c8b2130..e9e9588 100644 --- a/Gemfile +++ b/Gemfile @@ -13,19 +13,19 @@ gem "sprockets-rails" gem "sqlite3", "~> 1.4" # Use the Puma web server [https://github.com/puma/puma] -gem "puma", "~> 5.0" +# gem "puma", "~> 5.0" # Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails] -gem "importmap-rails" +# gem "importmap-rails" # Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev] -gem "turbo-rails" +# gem "turbo-rails" # Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev] -gem "stimulus-rails" +# gem "stimulus-rails" # Build JSON APIs with ease [https://github.com/rails/jbuilder] -gem "jbuilder" +# gem "jbuilder" # Use Redis adapter to run Action Cable in production # gem "redis", "~> 4.0" @@ -37,7 +37,7 @@ gem "jbuilder" # gem "bcrypt", "~> 3.1.7" # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] +# gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ] # Reduces boot times through caching; required in config/boot.rb gem "bootsnap", require: false diff --git a/Gemfile.lock b/Gemfile.lock index a75884f..7890b12 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -93,9 +93,6 @@ GEM activesupport (>= 5.0) i18n (1.10.0) concurrent-ruby (~> 1.0) - importmap-rails (1.0.3) - actionpack (>= 6.0.0) - railties (>= 6.0.0) interactor (3.1.2) interactor-rails (2.2.1) interactor (~> 3.0) @@ -104,9 +101,6 @@ GEM io-wait (0.2.1) irb (1.4.1) reline (>= 0.3.0) - jbuilder (2.11.5) - actionview (>= 5.0.0) - activesupport (>= 5.0.0) loofah (2.14.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) @@ -137,8 +131,6 @@ GEM nokogiri (1.13.3-x86_64-linux) racc (~> 1.4) public_suffix (4.0.6) - puma (5.6.2) - nio4r (~> 2.0) racc (1.6.0) rack (2.2.3) rack-test (1.1.0) @@ -187,14 +179,9 @@ GEM activesupport (>= 5.2) sprockets (>= 3.0.0) sqlite3 (1.4.2) - stimulus-rails (1.0.4) - railties (>= 6.0.0) strscan (3.0.1) thor (1.2.1) timeout (0.2.0) - turbo-rails (1.0.1) - actionpack (>= 6.0.0) - railties (>= 6.0.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) web-console (4.2.0) @@ -220,17 +207,11 @@ DEPENDENCIES bootsnap capybara debug - importmap-rails interactor-rails (~> 2.0) - jbuilder - puma (~> 5.0) rails (~> 7.0.2, >= 7.0.2.2) selenium-webdriver sprockets-rails sqlite3 (~> 1.4) - stimulus-rails - turbo-rails - tzinfo-data web-console webdrivers diff --git a/README.md b/README.md index 7db80e4..434fd4b 100644 --- a/README.md +++ b/README.md @@ -21,4 +21,12 @@ Things you may want to cover: * Deployment instructions -* ... +## How to test from the console + +``` +email = "" +password = "" +res = FetchEmails.call(email: email, password: password, query_from: Time.now - 30.days) + +res = MonitorInbox.call!(email: email,password: password) +``` \ No newline at end of file diff --git a/app/interactors/fetch_emails.rb b/app/interactors/fetch_emails.rb index df0a4dc..256cbfa 100644 --- a/app/interactors/fetch_emails.rb +++ b/app/interactors/fetch_emails.rb @@ -62,7 +62,8 @@ class FetchEmails message = OpenStruct.new( date: envelope.date.to_time.utc.iso8601, thread: mail.references, - from: "#{envelope.from&.dig(0)&.name} <#{envelope.from&.dig(0)&.mailbox}@#{envelope.from&.dig(0)&.host}>", + # from: "#{envelope.from&.dig(0)&.name} <#{envelope.from&.dig(0)&.mailbox}@#{envelope.from&.dig(0)&.host}>", + from: "#{envelope.from&.dig(0)&.mailbox}@#{envelope.from&.dig(0)&.host}", subject: envelope.subject, mail_object: mail, uid: uid, diff --git a/app/interactors/monitor_inbox.rb b/app/interactors/monitor_inbox.rb new file mode 100644 index 0000000..6441dd8 --- /dev/null +++ b/app/interactors/monitor_inbox.rb @@ -0,0 +1,39 @@ +class MonitorInbox + include Interactor + + AUTHORIZED_SENDERS = [ + "rodolfo.leyes@gmail.com" + ] + def call + email = context.email || ENV["EMAIL"] + password = context.password || ENV["PASSWORD"] + + 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: Time.now - 3.days + ) + + context.emails_arr = fetch_emails_res.messages + analyze_emails(context.emails_arr) + end + + 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) + end + end + 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) + end + + def ejecutar_accion(email) + + end +end diff --git a/lib/tasks/monitor.rake b/lib/tasks/monitor.rake new file mode 100644 index 0000000..6d77b93 --- /dev/null +++ b/lib/tasks/monitor.rake @@ -0,0 +1,6 @@ +namespace :monitor do + desc "executes the monitoring logic" + task :run => :environment do + MonitorInbox.call! + end +end \ No newline at end of file diff --git a/spec/interactors/monitor_inbox_spec.rb b/spec/interactors/monitor_inbox_spec.rb new file mode 100644 index 0000000..98734e5 --- /dev/null +++ b/spec/interactors/monitor_inbox_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +RSpec.describe MonitorInbox, type: :interactor do + describe '.call' do + pending "add some examples to (or delete) #{__FILE__}" + end +end