Commit abf969f

mokha <mokha@cisco.com>
2019-03-15 00:28:59
extract constant list of commands.
1 parent 56287b0
Changed files (3)
lib/minbox/cli.rb
@@ -14,12 +14,7 @@ module Minbox
 
       desc 'client <HOST> <PORT>', 'SMTP client'
       def client(host = 'localhost', port = 25)
-        mail = Mail.new do
-          from 'Your Name <me@example.org>'
-          to 'Destination Address <them@example.com>'
-          subject 'test message'
-          body "#{Time.now} This is a test message."
-        end
+        mail = create_mail
         Net::SMTP.start(host, port) do |smtp|
           smtp.debug_output = Minbox.logger
           smtp.send_message(mail.to_s, 'me+1@example.org', 'them+1@example.com')
@@ -41,6 +36,17 @@ module Minbox
       def version
         say Minbox::VERSION
       end
+
+      private
+
+      def create_mail
+        Mail.new do
+          from 'Your Name <me@example.org>'
+          to 'Destination Address <them@example.com>'
+          subject 'test message'
+          body "#{Time.now} This is a test message."
+        end
+      end
     end
   end
 end
lib/minbox/client.rb
@@ -41,7 +41,7 @@ module Minbox
   end
 
   class Noop < Command
-    def run(client, line)
+    def run(client, _line)
       client.write '250 OK'
     end
   end
@@ -51,7 +51,7 @@ module Minbox
       super(/^QUIT/i)
     end
 
-    def run(client, line)
+    def run(client, _line)
       client.write '221 Bye'
       client.close
     end
@@ -62,7 +62,7 @@ module Minbox
       super(/^DATA/i)
     end
 
-    def run(client, line, &block)
+    def run(client, _line)
       client.write '354 End data with <CR><LF>.<CR><LF>'
       body = []
       line = client.read
@@ -71,7 +71,7 @@ module Minbox
         line = client.read
       end
       client.write '250 OK'
-      block.call(Mail.new(body.join)) unless body.empty?
+      yield(Mail.new(body.join)) unless body.empty?
     end
   end
 
@@ -80,7 +80,7 @@ module Minbox
       super(/^STARTTLS/i)
     end
 
-    def run(client, line)
+    def run(client, _line)
       client.write '220 Ready to start TLS'
       client.secure_socket!
     end
@@ -122,7 +122,7 @@ module Minbox
   end
 
   class Unsupported
-    def matches?(line)
+    def matches?(_line)
       true
     end
 
@@ -133,32 +133,31 @@ module Minbox
   end
 
   class Client
+    COMMANDS = [
+      Ehlo.new,
+      Helo.new,
+      Noop.new(/^MAIL FROM/i),
+      Noop.new(/^RCPT TO/i),
+      Noop.new(/^RSET/i),
+      Noop.new(/^NOOP/i),
+      Quit.new,
+      Data.new,
+      StartTls.new,
+      AuthPlain.new,
+      AuthLogin.new
+    ].freeze
     attr_reader :server, :socket, :logger
-    attr_reader :commands
 
     def initialize(server, socket, logger)
       @server = server
       @logger = logger
       @socket = socket
-      @commands = [
-        Ehlo.new,
-        Helo.new,
-        Noop.new(/^MAIL FROM/i),
-        Noop.new(/^RCPT TO/i),
-        Noop.new(/^RSET/i),
-        Noop.new(/^NOOP/i),
-        Quit.new,
-        Data.new,
-        StartTls.new,
-        AuthPlain.new,
-        AuthLogin.new
-      ]
     end
 
     def handle(&block)
       write "220 #{server.host} ESMTP"
       while connected? && (line = read)
-        command = commands.find { |x| x.matches?(line) } || Unsupported.new
+        command = COMMANDS.find { |x| x.matches?(line) } || Unsupported.new
         command.run(self, line, &block)
       end
       close
spec/minbox/server_spec.rb
@@ -1,5 +1,4 @@
 # frozen_string_literal: true
-
 require 'spec_helper'
 
 RSpec.describe Minbox::Server do