class Ftpmock::NetFtpProxy

Constants

PORT
Real

Stubbers

Attributes

cache[W]
configuration[R]
host[R]
password[R]
port[R]
real[W]
username[R]

Public Class Methods

new(host = nil, *args) click to toggle source

inspired by apidock.com/ruby/v2_6_3/Net/FTP/new/class

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 55
def initialize(host = nil, *args)
  @options = args.last.is_a?(Hash) ? args.pop : {}
  @host = host
  @configuration = @options[:configuration] || Ftpmock.configuration

  if args.size == 2
    @options[:username] ||= args[0]
    @options[:password] ||= args[1]
  end

  if host
    connect(host, @options[:port] || PORT)
    login(@options[:username], @options[:password]) if @options[:username]
  end
end
off!() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 29
def self.off!
  return unless Real

  Net.send(:remove_const, :FTP)
  Net.const_set(:FTP, Real)
end
on!() { || ... } click to toggle source

inspired by github.com/bblimke/webmock/blob/master/lib/webmock/http_lib_caches/net_http.rb

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 15
def self.on!
  unless Real
    yield
    return
  end

  Net.send(:remove_const, :FTP)
  Net.const_set(:FTP, self)
  if block_given?
    yield
    off!
  end
end
open(host, *args) { |proxy| ... } click to toggle source

inspired by apidock.com/ruby/v2_6_3/Net/FTP/open/class

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 41
def self.open(host, *args)
  if block_given?
    proxy = new(host, *args)
    begin
      yield proxy
    ensure
      proxy.close
    end
  else
    new(host, *args)
  end
end

Public Instance Methods

_init_cache() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 137
def _init_cache
  credentials = [host, port, username, password]
  @cache = Cache.new(configuration, credentials)
end
_raise_code_error_connect_before_login() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 108
def _raise_code_error_connect_before_login
  raise CodeError,
        'please call ftp.connect(args) before calling ftp.login(args)'
end
_raise_not_connected() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 146
def _raise_not_connected
  raise(Net::FTPConnectionError, 'not connected')
end
_real_connect_and_login() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 113
def _real_connect_and_login
  @cache_connected || _raise_not_connected
  @cache_logged || _raise_not_connected

  @real_connected || real.connect(*_real_connect_args)
  @real_connected = true

  @real_logged || real.login(*_real_login_args)
  @real_logged = true

  if @chdir
    @real.chdir(@chdir)
    @chdir = nil
  end
end
_real_connect_args() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 129
def _real_connect_args
  [host, port]
end
_real_login_args() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 133
def _real_login_args
  [username, password].select { |string| StringUtils.present?(string) }
end
cache() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 142
def cache
  @cache || _raise_not_connected
end
chdir(dirname = nil) click to toggle source

docs.ruby-lang.org/en/2.0.0/Net/FTP.html#method-i-chdir

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 153
def chdir(dirname = nil)
  cache.chdir(dirname)
end
connect(host, port = PORT) click to toggle source

connection methods

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 84
def connect(host, port = PORT)
  @real_connected = false
  @host = host
  @port = port

  StringUtils.all_present?(host, port) || _raise_not_connected

  @cache_connected = true
  true
end
get(remotefile, localfile = File.basename(remotefile)) click to toggle source

get methods

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 176
def get(remotefile, localfile = File.basename(remotefile))
  cache.get(remotefile, localfile) do
    _real_connect_and_login
    real.get(remotefile, localfile)
  end

  true
end
getbinaryfile(remotefile, localfile = File.basename(remotefile)) click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 194
def getbinaryfile(remotefile, localfile = File.basename(remotefile))
  cache.get(remotefile, localfile) do
    _real_connect_and_login
    real.getbinaryfile(remotefile, localfile)
  end

  true
end
getdir() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 161
def getdir
  chdir
end
gettextfile(remotefile, localfile = File.basename(remotefile)) click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 185
def gettextfile(remotefile, localfile = File.basename(remotefile))
  cache.get(remotefile, localfile) do
    _real_connect_and_login
    real.gettextfile(remotefile, localfile)
  end

  true
end
list(*args) click to toggle source

list methods

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 167
def list(*args)
  cache.list(*args) do
    _real_connect_and_login
    real.list(*args)
  end
end
login(username = '', password = '') click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 95
def login(username = '', password = '')
  @cache_connected || _raise_code_error_connect_before_login
  @cache = nil
  @real_logged = false
  @username = username
  @password = password

  _init_cache

  @cache_logged = true
  true
end
put(localfile, remotefile = File.basename(localfile)) click to toggle source

TODO: block docs.ruby-lang.org/en/2.0.0/Net/FTP.html#method-i-put

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 207
def put(localfile, remotefile = File.basename(localfile))
  cache.put(localfile, remotefile) do
    _real_connect_and_login
    real.put(localfile, remotefile)
  end

  true
end
putbinaryfile(localfile, remotefile = File.basename(localfile)) click to toggle source

TODO: block

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 227
def putbinaryfile(localfile, remotefile = File.basename(localfile))
  cache.put(localfile, remotefile) do
    _real_connect_and_login
    real.putbinaryfile(localfile, remotefile)
  end

  true
end
puttextfile(localfile, remotefile = File.basename(localfile)) click to toggle source

TODO: block

# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 217
def puttextfile(localfile, remotefile = File.basename(localfile))
  cache.put(localfile, remotefile) do
    _real_connect_and_login
    real.puttextfile(localfile, remotefile)
  end

  true
end
pwd() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 157
def pwd
  chdir
end
real() click to toggle source
# File lib/ftpmock/proxies/net_ftp_proxy.rb, line 71
def real
  @real ||= Real.new
end