Commit 3e1b7a0

mo <mo.khan@gmail.com>
2018-04-28 04:52:36
add default route to log all messages.
1 parent 6bf1f28
lib/del/connection.rb
@@ -4,8 +4,8 @@ module Del
 
     def initialize(configuration:)
       @configuration = configuration
-      @rooms = configuration[:rooms]
-      @users = configuration[:users]
+      @rooms = configuration.rooms
+      @users = configuration.users
       @mucs = {}
     end
 
@@ -14,9 +14,9 @@ module Del
         Del.logger.error(error)
         disconnect
       end
-      client.connect(configuration[:host])
+      client.connect(configuration.host)
       sleep 0.0001 until client.is_connected?
-      client.auth(configuration[:password])
+      client.auth(configuration.password)
       roster = Jabber::Roster::Helper.new(client, false)
       roster.add_update_callback do |old_item, item|
         users.upsert(item['jid'], User.new(item['jid'], item)) if item
@@ -25,13 +25,13 @@ module Del
       roster.wait_for_roster
       client.add_message_callback do |message|
         next if message.type == :error || message.body.nil?
-        user = configuration[:users].find_by(message.from.strip)
+        user = configuration.users.find_by(message.from.strip)
         robot.receive(message.body, source: Source.new(user: user))
       end
       client.send(Jabber::Presence.new(:chat))
-      configuration[:default_rooms].each do |room|
+      configuration.default_rooms.each do |room|
         Del.logger.debug("Joining #{room} as #{robot.name}")
-        room_jid = jid_for(room, configuration[:muc_domain].dup, robot.name)
+        room_jid = jid_for(room, configuration.muc_domain.dup, robot.name)
         stripped_jid = room_jid.strip.to_s
         next if @mucs[stripped_jid]
 
@@ -44,7 +44,7 @@ module Del
         end
         muc.join(room_jid)
       end
-      list_rooms(configuration[:muc_domain]).each do |room|
+      list_rooms(configuration.muc_domain).each do |room|
         rooms.upsert(room)
       end
     end
@@ -74,7 +74,7 @@ module Del
     end
 
     def jid
-      @jid ||= jid_for(configuration[:jid], "chat.hipchat.com", "bot")
+      @jid ||= jid_for(configuration.jid, "chat.hipchat.com", "bot")
     end
 
     def list_rooms(muc_domain)
lib/del/default_router.rb
@@ -1,10 +1,7 @@
 module Del
   class DefaultRouter
-    attr_reader :logger
-
-    def initialize(logger = Del.logger)
-      @logger = logger
-      @routes = []
+    def initialize(routes = [])
+      @routes = routes
     end
 
     def register(pattern, &block)
@@ -12,7 +9,6 @@ module Del
     end
 
     def route(message)
-      logger.info(message.to_s)
       @routes.each do |route|
         if route[:pattern].match(message.text)
           route[:command].call(message)
lib/del/robot.rb
@@ -6,11 +6,11 @@ module Del
 
     def initialize(configuration:)
       @connection = Connection.new(configuration: configuration)
-      @jid = configuration[:jid]
-      @name = configuration[:name]
-      @router = configuration[:router]
-      @users = configuration[:users]
-      @rooms = configuration[:rooms]
+      @jid = configuration.jid
+      @name = configuration.name
+      @router = configuration.router
+      @users = configuration.users
+      @rooms = configuration.rooms
     end
 
     def get_funky!
lib/del.rb
@@ -21,6 +21,12 @@ module Del
     Dotenv.load(dotenv_file.to_s)
     Del.logger.level = Logger::INFO
     del = Robot.new(configuration: configuration)
+    Del.configure do |config|
+      config.router.register(/.*/) do |message|
+        logger.info(message.to_s)
+        message.reply(message.text.reverse)
+      end
+    end
     del.get_funky!
   end
 
@@ -33,6 +39,6 @@ module Del
   end
 
   def self.logger
-    @logger ||= configuration[:logger]
+    @logger ||= configuration.logger
   end
 end
spec/default_router_spec.rb
@@ -1,5 +1,5 @@
 RSpec.describe Del::DefaultRouter do
-  subject { described_class.new(Logger.new(STDOUT)) }
+  subject { described_class.new }
 
   describe "#route" do
     let(:recorder) { [] }