Browse Source

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
master
rodley82 4 years ago
parent
commit
8acf4e5bb9
  1. 12
      Gemfile
  2. 19
      Gemfile.lock
  3. 10
      README.md
  4. 3
      app/interactors/fetch_emails.rb
  5. 39
      app/interactors/monitor_inbox.rb
  6. 6
      lib/tasks/monitor.rake
  7. 7
      spec/interactors/monitor_inbox_spec.rb

12
Gemfile

@ -13,19 +13,19 @@ gem "sprockets-rails"
gem "sqlite3", "~> 1.4" gem "sqlite3", "~> 1.4"
# Use the Puma web server [https://github.com/puma/puma] # 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] # 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] # 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] # 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] # Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder" # gem "jbuilder"
# Use Redis adapter to run Action Cable in production # Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0" # gem "redis", "~> 4.0"
@ -37,7 +37,7 @@ gem "jbuilder"
# gem "bcrypt", "~> 3.1.7" # gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem # 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 # Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false gem "bootsnap", require: false

19
Gemfile.lock

@ -93,9 +93,6 @@ GEM
activesupport (>= 5.0) activesupport (>= 5.0)
i18n (1.10.0) i18n (1.10.0)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
importmap-rails (1.0.3)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
interactor (3.1.2) interactor (3.1.2)
interactor-rails (2.2.1) interactor-rails (2.2.1)
interactor (~> 3.0) interactor (~> 3.0)
@ -104,9 +101,6 @@ GEM
io-wait (0.2.1) io-wait (0.2.1)
irb (1.4.1) irb (1.4.1)
reline (>= 0.3.0) reline (>= 0.3.0)
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
loofah (2.14.0) loofah (2.14.0)
crass (~> 1.0.2) crass (~> 1.0.2)
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)
@ -137,8 +131,6 @@ GEM
nokogiri (1.13.3-x86_64-linux) nokogiri (1.13.3-x86_64-linux)
racc (~> 1.4) racc (~> 1.4)
public_suffix (4.0.6) public_suffix (4.0.6)
puma (5.6.2)
nio4r (~> 2.0)
racc (1.6.0) racc (1.6.0)
rack (2.2.3) rack (2.2.3)
rack-test (1.1.0) rack-test (1.1.0)
@ -187,14 +179,9 @@ GEM
activesupport (>= 5.2) activesupport (>= 5.2)
sprockets (>= 3.0.0) sprockets (>= 3.0.0)
sqlite3 (1.4.2) sqlite3 (1.4.2)
stimulus-rails (1.0.4)
railties (>= 6.0.0)
strscan (3.0.1) strscan (3.0.1)
thor (1.2.1) thor (1.2.1)
timeout (0.2.0) timeout (0.2.0)
turbo-rails (1.0.1)
actionpack (>= 6.0.0)
railties (>= 6.0.0)
tzinfo (2.0.4) tzinfo (2.0.4)
concurrent-ruby (~> 1.0) concurrent-ruby (~> 1.0)
web-console (4.2.0) web-console (4.2.0)
@ -220,17 +207,11 @@ DEPENDENCIES
bootsnap bootsnap
capybara capybara
debug debug
importmap-rails
interactor-rails (~> 2.0) interactor-rails (~> 2.0)
jbuilder
puma (~> 5.0)
rails (~> 7.0.2, >= 7.0.2.2) rails (~> 7.0.2, >= 7.0.2.2)
selenium-webdriver selenium-webdriver
sprockets-rails sprockets-rails
sqlite3 (~> 1.4) sqlite3 (~> 1.4)
stimulus-rails
turbo-rails
tzinfo-data
web-console web-console
webdrivers webdrivers

10
README.md

@ -21,4 +21,12 @@ Things you may want to cover:
* Deployment instructions * 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)
```

3
app/interactors/fetch_emails.rb

@ -62,7 +62,8 @@ class FetchEmails
message = OpenStruct.new( message = OpenStruct.new(
date: envelope.date.to_time.utc.iso8601, date: envelope.date.to_time.utc.iso8601,
thread: mail.references, 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, subject: envelope.subject,
mail_object: mail, mail_object: mail,
uid: uid, uid: uid,

39
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

6
lib/tasks/monitor.rake

@ -0,0 +1,6 @@
namespace :monitor do
desc "executes the monitoring logic"
task :run => :environment do
MonitorInbox.call!
end
end

7
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
Loading…
Cancel
Save