Skip to content

Commit

Permalink
Deliver notification emails
Browse files Browse the repository at this point in the history
  • Loading branch information
ramil350 committed Jan 26, 2016
1 parent 5d8d6bd commit f44dd29
Show file tree
Hide file tree
Showing 10 changed files with 1,129 additions and 2 deletions.
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 2,7 @@ PATH
remote: .
specs:
ssl_reminder (0.0.1)
active_model_serializers
faraday (~> 0.9.1)
faraday_middleware (~> 0.9.0)
rails (~> 4.2.1)
Expand All @@ -28,6 29,8 @@ GEM
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
active_model_serializers (0.8.3)
activemodel (>= 3.0)
activejob (4.2.4)
activesupport (= 4.2.4)
globalid (>= 0.3.0)
Expand Down
46 changes: 46 additions & 0 deletions app/jobs/ssl_reminder/scan_certificates.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,46 @@
module Jobs
module SslReminder
class ScanCertificates < Jobs::Scheduled
every 1.day

DEFAULT_BATCH_SIZE = 100
DAILY_THRESHOLD_DAYS = 7
WEEKLY_THRESHOLD_DAYS = 31
WEEKLY_DAY_TO_SEND = 1

def execute(_args)
::SslReminder::Domain.find_in_batches(batch_size: DEFAULT_BATCH_SIZE) do |domains|
domains.each { |domain| process_domain(domain) }
end
end

private

def process_domain(domain)
update_certificate_info(domain)
send_report(domain) if valid_to_send?(domain)
end

def update_certificate_info(domain)
scanner = ::SslReminder::Certificates::Scanner.new(domain.url)
new_expiration_date = scanner.scan
return if new_expiration_date.blank?

domain.expiration_date = new_expiration_date
domain.save! if domain.changed?
end

def send_report(domain)
report = ::SslReminderReportMailer.report(domain)
Email::Sender.new(report, :ssl_reminder).send
end

def valid_to_send?(domain)
return false unless domain.notification_enabled? && domain.days_remaining.present?

domain.days_remaining <= DAILY_THRESHOLD_DAYS ||
domain.days_remaining <= WEEKLY_THRESHOLD_DAYS && Date.current.wday == WEEKLY_DAY_TO_SEND
end
end
end
end
28 changes: 28 additions & 0 deletions app/mailers/ssl_reminder_report_mailer.rb
Original file line number Diff line number Diff line change
@@ -0,0 1,28 @@
require_dependency "email/message_builder"

class SslReminderReportMailer < ActionMailer::Base
include Email::BuildEmailHelper

def report(domain)
email_opts = {
template: "ssl_reminder",
html_override: html(domain)
}

build_email(domain.user.email, email_opts)
end

private

def html(domain)
ActionView::Base.new("plugins/ssl-reminder/app/views").render(
template: "email/ssl_reminder",
format: :html,
locals: {
domain_name: domain.name,
domain_url: domain.url,
days_remaining: domain.days_remaining
}
)
end
end
8 changes: 8 additions & 0 deletions app/models/ssl_reminder/domain.rb
Original file line number Diff line number Diff line change
@@ -1,5 1,13 @@
module SslReminder
class Domain < ActiveRecord::Base
belongs_to :user

def days_remaining
if expiration_date.blank?
nil
else
[(expiration_date - Date.current).to_i, 0].max
end
end
end
end
Loading

0 comments on commit f44dd29

Please sign in to comment.