Commit c840a42

mokha <mokha@cisco.com>
2019-03-15 00:43:30
move regex match into rash. still not O(1) lookup.
1 parent abf969f
lib/minbox/client.rb
@@ -133,19 +133,19 @@ 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
+    COMMANDS = Hashie::Rash.new(
+      /^AUTH LOGIN/i => AuthLogin.new,
+      /^AUTH PLAIN/i => AuthPlain.new,
+      /^DATA/i => Data.new,
+      /^EHLO/i => Ehlo.new,
+      /^HELO/i => Helo.new,
+      /^MAIL FROM/i => Noop.new(/^MAIL FROM/i),
+      /^NOOP/i => Noop.new(/^NOOP/i),
+      /^QUIT/i => Quit.new,
+      /^RCPT TO/i => Noop.new(/^RCPT TO/i),
+      /^RSET/i => Noop.new(/^RSET/i),
+      /^STARTTLS/i => StartTls.new,
+    )
     attr_reader :server, :socket, :logger
 
     def initialize(server, socket, logger)
@@ -157,7 +157,7 @@ module Minbox
     def handle(&block)
       write "220 #{server.host} ESMTP"
       while connected? && (line = read)
-        command = COMMANDS.find { |x| x.matches?(line) } || Unsupported.new
+        command = COMMANDS[line] || Unsupported.new
         command.run(self, line, &block)
       end
       close
lib/minbox.rb
@@ -1,6 +1,7 @@
 # frozen_string_literal: true
 
 require 'base64'
+require 'hashie'
 require 'logger'
 require 'socket'
 
Gemfile.lock
@@ -2,6 +2,7 @@ PATH
   remote: .
   specs:
     minbox (0.1.4)
+      hashie (~> 3.6)
       mail (~> 2.7)
       redis (~> 4.1)
       thor (~> 0.20)
@@ -17,6 +18,7 @@ GEM
     diff-lcs (1.3)
     faker (1.9.3)
       i18n (>= 0.7)
+    hashie (3.6.0)
     i18n (1.6.0)
       concurrent-ruby (~> 1.0)
     jaro_winkler (1.5.2)
minbox.gemspec
@@ -35,6 +35,7 @@ Gem::Specification.new do |spec|
   spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
   spec.require_paths = ['lib']
 
+  spec.add_dependency 'hashie', '~> 3.6'
   spec.add_dependency 'mail', '~> 2.7'
   spec.add_dependency 'redis', '~> 4.1'
   spec.add_dependency 'thor', '~> 0.20'