Class: Meshchat::Ui::Command::Ping

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

Constant Summary

Constants inherited from Base

Base::ADD, Base::ALL_CHAT_LOCK, Base::BIND, Base::CHAT, Base::CONFIG, Base::DISPLAY, Base::EMOTE, Base::EXIT, Base::EXPORT, Base::HELP, Base::IDENTITY, Base::IMPORT, Base::IRB, Base::OFFLINE, Base::ONLINE, Base::PING, Base::PING_ALL, Base::QUIT, Base::REMOVE, Base::RM, Base::ROLL, Base::SEND_DISCONNECT, Base::SERVER, Base::SERVERS, Base::SET, Base::SHARE, Base::WHISPER_LOCK, Base::WHO

Instance Attribute Summary

Attributes inherited from Base

#_input, #_input_factory, #_message_dispatcher, #_message_factory

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#initialize

Constructor Details

This class inherits a constructor from Meshchat::Ui::Command::Base

Class Method Details

.descriptionObject



6
7
8
# File 'lib/meshchat/ui/command/ping.rb', line 6

def self.description
  'pings a particular user'
end

Instance Method Details

#command_valid?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/meshchat/ui/command/ping.rb', line 48

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

#handleObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/meshchat/ui/command/ping.rb', line 10

def handle
  if command_valid?
    msg = _message_factory.create(Network::Message::PING)

    field, value = parse_ping_command

    node =
      if field == 'location'
        Node.find_by_location_on_network(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

    _message_dispatcher.send_message(node: node, message: msg)
  else
    Display.alert usage
  end
end

#lookup_fieldObject



39
40
41
# File 'lib/meshchat/ui/command/ping.rb', line 39

def lookup_field
  sub_command
end

#lookup_valueObject



43
44
45
46
# File 'lib/meshchat/ui/command/ping.rb', line 43

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

#parse_ping_commandObject



52
53
54
55
56
57
58
59
60
61
# File 'lib/meshchat/ui/command/ping.rb', line 52

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



35
36
37
# File 'lib/meshchat/ui/command/ping.rb', line 35

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