Class: MeshChat::CLI::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/meshchat/cli/input.rb

Direct Known Subclasses

MeshChat::Command::Base

Constant Summary collapse

WHISPER =
'@'
COMMAND =
'/'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Input

Returns a new instance of Input.



33
34
35
# File 'lib/meshchat/cli/input.rb', line 33

def initialize(input)
  self._input = input.chomp
end

Instance Attribute Details

#_inputObject

Returns the value of attribute _input.



6
7
8
# File 'lib/meshchat/cli/input.rb', line 6

def _input
  @_input
end

Class Method Details

.create(input) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/meshchat/cli/input.rb', line 17

def create(input)
  klass =
    if is_command?(input)
      Command::Base
    elsif is_whisper?(input)
      Command::Whisper
    else
      # TODO: maybe change this to a chat command?
      CLI::Input
    end

  klass.new(input)
end

.is_command?(input) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/meshchat/cli/input.rb', line 9

def is_command?(input)
  input[0, 1] == COMMAND
end

.is_whisper?(input) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/meshchat/cli/input.rb', line 13

def is_whisper?(input)
  input[0, 1] == WHISPER
end

Instance Method Details

#handleObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/meshchat/cli/input.rb', line 37

def handle
  return if _input.empty?

  servers = Node.online
  if !servers.empty?
    m = Message::Chat.new(
      message: _input
    )

    Display.chat m.display

    # if sending to all, iterate thorugh list of
    # servers, and send to each one
    # TODO: do this async so that one server doesn't block
    # the rest of the servers from receiving the messages
    servers.each do |entry|
      Net::Client.send(node: entry, message: m)
    end
  else
    Display.warning 'you have no servers'
  end
end