Commit d6a3678

mokha <mokha@cisco.com>
2019-02-27 18:03:43
publish emails to stdout and redis
1 parent f747bc8
lib/minbox/cli.rb
@@ -24,8 +24,9 @@ module Minbox
 
       desc 'server <HOST> <PORT>', 'SMTP server'
       def server(host = 'localhost', port = '25')
+        publisher = Publisher.new(LogPublisher.new, RedisPublisher.new)
         Server.new(host, port).listen! do |mail|
-          puts mail.to_s
+          publisher.publish(mail)
         end
       end
 
lib/minbox/publisher.rb
@@ -0,0 +1,35 @@
+require 'redis'
+
+module Minbox
+  class Publisher
+    attr_reader :publishers
+
+    def initialize(*publishers)
+      @publishers = Array(publishers)
+    end
+
+    def publish(mail)
+      publishers.each { |x| x.publish(mail) }
+    end
+  end
+
+  class LogPublisher
+    def initialize(logger = STDOUT)
+      @logger = logger
+    end
+
+    def publish(mail)
+      @logger.puts mail.to_s
+    end
+  end
+
+  class RedisPublisher
+    def initialize(redis = Redis.new)
+      @redis = redis
+    end
+
+    def publish(mail)
+      @redis.publish("minbox", mail.to_s)
+    end
+  end
+end
lib/minbox.rb
@@ -1,5 +1,6 @@
 require 'socket'
 
+require "minbox/publisher"
 require "minbox/server"
 require "minbox/version"
 
Gemfile.lock
@@ -3,6 +3,7 @@ PATH
   specs:
     minbox (0.1.0)
       mail (~> 2.7)
+      redis (~> 4.1)
       thor (~> 0.20)
 
 GEM
@@ -13,6 +14,7 @@ GEM
       mini_mime (>= 0.1.1)
     mini_mime (1.0.1)
     rake (10.5.0)
+    redis (4.1.0)
     rspec (3.8.0)
       rspec-core (~> 3.8.0)
       rspec-expectations (~> 3.8.0)
minbox.gemspec
@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
   spec.require_paths = ["lib"]
 
   spec.add_dependency "mail", "~> 2.7"
+  spec.add_dependency "redis", "~> 4.1"
   spec.add_dependency "thor", "~> 0.20"
   spec.add_development_dependency "bundler", "~> 2.0"
   spec.add_development_dependency "rake", "~> 10.0"