Module: EM::FTPD::FSD::Base

Defined in:
lib/em-ftpd-fsd/base.rb

Overview

Base module to include in custom drivers

Constant Summary collapse

COMMANDS =

Supported FTP commands.

{
  bytes:        { on_error_value: nil   },
  dir_contents: { on_error_value: nil   },
  get_file:     { on_error_value: nil   },
  change_dir:   { on_error_value: false },
  delete_dir:   { on_error_value: false },
  delete_file:  { on_error_value: false },
  rename:       { on_error_value: false },
  make_dir:     { on_error_value: false },
  put_file:     { on_error_value: false },
}

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) {|Object| ... } ⇒ Object

Metaprogrammed redirection to FTP commands defined by FileOperation class.

Parameters:

  • method (Symbol)

    Method called

  • args (Array)

    All arguments received by called method

  • block (Block)

    Block to be yield by called method

Yields:

  • (Object)

    Return value from file specific operation

Raises:

  • (NoMethodError)

    if method is not supported by driver

  • (ArgumentError)

    if no block is passed to yield value



86
87
88
89
90
91
# File 'lib/em-ftpd-fsd/base.rb', line 86

def method_missing( method, *args, &block )
  raise NoMethodError, "#{method}"    unless COMMANDS.include?( method )
  raise ArgumentError, "Block needed" unless block_given?

  yield ftp_methods( method, args )
end

Class Method Details

.included(klass) ⇒ Object

Extend hooks module to load ‘before’ and ‘after’ class functions



42
43
44
# File 'lib/em-ftpd-fsd/base.rb', line 42

def self.included( klass )
  klass.extend( EM::FTPD::FSD::Hooks )
end

Instance Method Details

#base_pathString

Absolute path to FTP base directory.

Returns:

  • (String)

    Full path to FTP base folder.



75
76
77
# File 'lib/em-ftpd-fsd/base.rb', line 75

def base_path
  File.expand_path( @base_path || "" )
end

#initialize(options = {}) ⇒ Object

Initiliaze the driver with basic options

Examples:

Default options for authentication

{
  plain: {
    user:     "admin",
    password: "root"
  }
}

Parameters:

  • options (Hash) (defaults to: {})

    Options

Options Hash (options):

  • base_path (Object)

    Base directory for FTP server

  • authentication (Object)

    Authentication module to load



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/em-ftpd-fsd/base.rb', line 57

def initialize( options = {} )
  opts = {
    base_path: Dir.pwd,
    authentication: {
      plain: {
        user:     "admin",
        password: "root"
      }
    }
  }.merge( options )

  load_auth_module( opts[:authentication] )

  @base_path = opts[:base_path]
end

#respond_to?(method, include_private = false) ⇒ Boolean

Return true if obj respond to given method or method correspond to an implemented ftp command.

Parameters:

  • method (Symbol)

    Given method

Returns:

  • (Boolean)

    Return true if obj respond to given method.



97
98
99
# File 'lib/em-ftpd-fsd/base.rb', line 97

def respond_to?( method, include_private = false )
  super( method, include_private ) || COMMANDS.include?( method )
end