Sha256: c8784d75b9a2752d400bc7e8c254a72f5fad4eed7189ffd6f71f4f52a368115d

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Ftpd

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

  class CommandHandlers

    def initialize
      @commands = {}
    end

    # Add a command handler
    #
    # @param command_handler [Command]

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

    # @param command [String] the command (e.g. "STOR").  Case
    #   insensitive.
    # @return truthy if the server supports the command.

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

    # Dispatch a command to the appropriate command handler.
    #
    # @param command [String] the command (e.g. "STOR").  Case
    #   insensitive.
    # @param argument [String] The argument, or nil if there isn't
    #   one.

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

    # Return the sorted list of commands supported by this handler
    #
    # @return [Array<String>] Lowercase command

    def commands
      @commands.keys.sort
    end

    private

    def canonical_command(command)
      command.downcase
    end

  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
ftpd-2.1.0 lib/ftpd/command_handlers.rb
ftpd-2.0.5 lib/ftpd/command_handlers.rb
ftpd-2.0.4 lib/ftpd/command_handlers.rb
ftpd-2.0.3 lib/ftpd/command_handlers.rb
ftpd-2.0.2 lib/ftpd/command_handlers.rb
ftpd-2.0.1 lib/ftpd/command_handlers.rb
ftpd-2.0.0 lib/ftpd/command_handlers.rb