Class: MeshChat::CLI::Ping

Inherits:
Command show all
Defined in:
lib/meshchat/cli/ping.rb

Constant Summary

Constants inherited from Command

Command::ADD, Command::CHAT, Command::CONFIG, Command::CONNECT, Command::DISPLAY, Command::EXIT, Command::EXPORT, Command::IDENTITY, Command::IMPORT, Command::INIT, Command::IRB, Command::LISTEN, Command::PING, Command::PING_ALL, Command::QUIT, Command::REMOVE, Command::RM, Command::SERVER, Command::SERVERS, Command::SET, Command::SHARE, Command::STOP_LISTENING, Command::WHO

Constants inherited from Input

Input::COMMAND, Input::WHISPER

Instance Attribute Summary

Attributes inherited from Command

#_input

Attributes inherited from Input

#_input

Instance Method Summary collapse

Methods inherited from Input

create, #initialize, is_command, is_whisper?

Constructor Details

This class inherits a constructor from MeshChat::CLI::Input

Instance Method Details

#command_valid?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/meshchat/cli/ping.rb', line 45

def command_valid?
  parse_ping_command.compact.length == 2
end

#handleObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/meshchat/cli/ping.rb', line 4

def handle
  if command_valid?
    msg = Message::Ping.new

    field, value = parse_ping_command

    node =
      if field == 'location'
        Node.find_by_location(lookup_value)
      else
        Node.find_by_alias_name(lookup_value)
      end

    location = node.try(:location)

    unless location
      return Display.alert "#{lookup_value} could not be found"
    end

    Net::Client.send(
      node: node,
      message: msg
    )
  else
    Display.alert usage
  end
end

#lookup_fieldObject



36
37
38
# File 'lib/meshchat/cli/ping.rb', line 36

def lookup_field
  sub_command
end

#lookup_valueObject



40
41
42
43
# File 'lib/meshchat/cli/ping.rb', line 40

def lookup_value
  value = command_args.last
  value if value != sub_command
end

#parse_ping_commandObject



49
50
51
52
53
54
55
56
57
58
# File 'lib/meshchat/cli/ping.rb', line 49

def parse_ping_command
  @parsed_args ||=
    if lookup_field == 'location' || lookup_field == 'alias'
      [lookup_field, lookup_value]
    elsif lookup_field =~ /(?:[0-9]{1,3}\.){3}[0-9]{1,3}/
      ['location', lookup_field]
    else
      ['alias', lookup_field]
    end
end

#usageObject



32
33
34
# File 'lib/meshchat/cli/ping.rb', line 32

def usage
  'Usage: /ping {field} {value}  e.g.: /ping alias neurotek or /ping location 10.10.10.10:8080'
end