Commit 7145e0f

mo <mo.khan@gmail.com>
2018-05-12 03:35:23
add client commands for querying users.
1 parent 60d01ec
Changed files (4)
lib/del/cli.rb
@@ -33,8 +33,34 @@ module Del
 
     desc 'message <jid> <message>', 'send a message to the Jabber ID'
     def message(jid, message)
-      SendMessage.new(self, socket_file: options[:socket_file])
-        .run(jid, message)
+      SendMessage.new(self, socket_file: options[:socket_file]).run(jid, message)
+    end
+
+    desc 'whoami', 'send a whoami message to the local del server'
+    def whoami
+      socket = SocketMessage.new(self, socket_file: options[:socket_file])
+      socket.deliver(command: :whoami)
+      say(socket.listen, :green)
+    ensure
+      socket.close
+    end
+
+    desc 'whois <jid>', 'whois a specific user'
+    def whois(jid)
+      socket = SocketMessage.new(self, socket_file: options[:socket_file])
+      socket.deliver(command: :whois, q: jid)
+      say(socket.listen, :green)
+    ensure
+      socket.close
+    end
+
+    desc 'users', 'list all users'
+    def users
+      socket = SocketMessage.new(self, socket_file: options[:socket_file])
+      socket.deliver(command: :users)
+      say(socket.listen, :green)
+    ensure
+      socket.close
     end
 
     desc 'setup', 'setup your $HOME/.delrc'
lib/del/robot.rb
@@ -35,10 +35,16 @@ module Del
     end
 
     def execute(request)
-      case request['command']
-      when 'send_message'
+      case request['command'].to_sym
+      when :send_message
         send_message(request['jid'], request['message'])
         'Sent!'
+      when :whoami
+        JSON.generate(whois(jid))
+      when :whois
+        JSON.generate(whois(request['q']))
+      when :users
+        JSON.generate(configuration.users.all.map(&:attributes))
       else
         'Unknown'
       end
@@ -60,5 +66,9 @@ module Del
     def user?(jid)
       configuration.users[jid]
     end
+
+    def whois(jid)
+      configuration.users[jid]&.attributes || {}
+    end
   end
 end
lib/del/user.rb
@@ -19,6 +19,7 @@ module Del
     end
 
     def self.map_from(attributes)
+      return nil if attributes.nil?
       new(attributes['jid'], attributes)
     end
   end
spec/user_spec.rb
@@ -13,5 +13,7 @@ RSpec.describe Del::User do
       expect(result.jid).to eql(attributes['jid'])
       expect(result.attributes).to eql(attributes)
     end
+
+    specify { expect(subject.map_from(nil)).to be_nil }
   end
 end