Commit 4289c40

mo <mo.khan@gmail.com>
2018-04-27 21:03:31
move classes to separate files.
1 parent 9e70ce5
lib/del/robot.rb
@@ -1,35 +1,4 @@
-require 'xmpp4r'
-require 'xmpp4r/roster/helper/roster'
-require 'xmpp4r/muc/helper/simplemucclient'
-require 'xmpp4r/muc/helper/mucbrowser'
-
 module Del
-  class User
-    def initialize(attributes)
-      @attributes = attributes
-    end
-  end
-
-  class UserRepository
-    def initialize
-      @users = Set.new
-    end
-
-    def create(item)
-      @users << User.new(item.attributes)
-    end
-  end
-
-  class RoomRepository
-    def initialize
-      @rooms = Set.new
-    end
-
-    def upsert(room)
-      @rooms << room
-    end
-  end
-
   class Robot
     attr_reader :configuration, :users, :rooms
 
@@ -53,11 +22,9 @@ module Del
       end
       roster.get_roster
       roster.wait_for_roster
-      @mention_name = roster[jid].attributes['mention_name']
-
+      #@mention_name = roster[jid].attributes["mention_name"]
       client.add_message_callback do |message|
         next if message.type == :error || message.body.nil?
-        puts message.inspect
         send_message(message.from, message.body)
       end
       client.send(Jabber::Presence.new(:chat))
@@ -99,7 +66,7 @@ module Del
     end
 
     def encode_string(s)
-      s.encode('UTF-8', invalid: :replace, undef: :replace)
+      s.encode("UTF-8", invalid: :replace, undef: :replace)
     end
   end
 end
lib/del/room_repository.rb
@@ -0,0 +1,9 @@
+  class RoomRepository
+    def initialize
+      @rooms = Set.new
+    end
+
+    def upsert(room)
+      @rooms << room
+    end
+  end
lib/del/user.rb
@@ -0,0 +1,5 @@
+  class User
+    def initialize(attributes)
+      @attributes = attributes
+    end
+  end
lib/del/user_repository.rb
@@ -0,0 +1,9 @@
+  class UserRepository
+    def initialize
+      @users = Set.new
+    end
+
+    def create(item)
+      @users << User.new(item.attributes)
+    end
+  end
lib/del.rb
@@ -1,16 +1,24 @@
+require "dotenv"
+require "xmpp4r"
+require "xmpp4r/muc/helper/mucbrowser"
+require "xmpp4r/muc/helper/simplemucclient"
+require "xmpp4r/roster/helper/roster"
+
 require "del/robot"
 require "del/version"
-require 'dotenv'
+require "del/room_repository"
+require "del/user"
+require "del/user_repository"
 
 module Del
   def self.start
     Dotenv.load(".env.local", Pathname.new(Dir.home).join(".delrc").to_s)
 
     del = Robot.new(configuration: {
-      host: ENV['DEL_HOST'],
-      jid: ENV['DEL_JID'],
-      muc_domain: ENV['DEL_MUC_DOMAIN'],
-      password: ENV['DEL_PASSWORD'],
+      host: ENV["DEL_HOST"],
+      jid: ENV["DEL_JID"],
+      muc_domain: ENV["DEL_MUC_DOMAIN"],
+      password: ENV["DEL_PASSWORD"],
     })
     del.run
   end