Class: Ftpd::CommandHandlers

Inherits:
Object
  • Object
show all
Defined in:
lib/ftpd/command_handlers.rb

Overview

All FTP commands which the server supports are dispatched by this class.

Instance Method Summary collapse

Constructor Details

#initializeCommandHandlers

Returns a new instance of CommandHandlers.



8
9
10
# File 'lib/ftpd/command_handlers.rb', line 8

def initialize
  @commands = {}
end

Instance Method Details

#<<(command_handler) ⇒ Object

Add a command handler

Parameters:

  • command_handler (Command)


16
17
18
19
20
# File 'lib/ftpd/command_handlers.rb', line 16

def <<(command_handler)
  command_handler.commands.each do |command|
    @commands[command] = command_handler
  end
end

#commandsArray<String>

Return the sorted list of commands supported by this handler

Returns:

  • (Array<String>)

    Lowercase command



48
49
50
# File 'lib/ftpd/command_handlers.rb', line 48

def commands
  @commands.keys.sort
end

#execute(command, argument) ⇒ Object

Dispatch a command to the appropriate command handler.

Parameters:

  • command (String)

    the command (e.g. “STOR”). Case insensitive.

  • argument (String)

    The argument, or nil if there isn’t one.



38
39
40
41
42
# File 'lib/ftpd/command_handlers.rb', line 38

def execute(command, argument)
  command = canonical_command(command)
  method = "cmd_#{command}"
  @commands[command.downcase].send(method, argument)
end

#has?(command) ⇒ Boolean

Returns truthy if the server supports the command.

Parameters:

  • command (String)

    the command (e.g. “STOR”). Case insensitive.

Returns:

  • (Boolean)

    truthy if the server supports the command.



26
27
28
29
# File 'lib/ftpd/command_handlers.rb', line 26

def has?(command)
  command = canonical_command(command)
  @commands.has_key?(command)
end