Class: MeshChat::Command::Ping

Inherits:
Base show all
Defined in:
lib/meshchat/command/ping.rb

Constant Summary

Constants inherited from Base

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

Constants inherited from MeshChat::CLI::Input

MeshChat::CLI::Input::COMMAND, MeshChat::CLI::Input::WHISPER

Instance Attribute Summary

Attributes inherited from Base

#_input

Attributes inherited from MeshChat::CLI::Input

#_input

Instance Method Summary collapse

Methods inherited from MeshChat::CLI::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/command/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/command/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/command/ping.rb', line 36

def lookup_field
  sub_command
end

#lookup_valueObject



40
41
42
43
# File 'lib/meshchat/command/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/command/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/command/ping.rb', line 32

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