Class: Ftpmock::NetFtpProxy

Inherits:
Object
  • Object
show all
Includes:
MethodMissingMixin
Defined in:
lib/ftpmock/proxies/net_ftp_proxy.rb

Constant Summary collapse

Real =

Stubbers

begin
  Net::FTP
rescue NameError
  nil
end
PORT =
21

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MethodMissingMixin

#method_missing

Constructor Details

#initialize(host = nil, *args) ⇒ NetFtpProxy



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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)
    (@options[:username], @options[:password]) if @options[:username]
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Ftpmock::MethodMissingMixin

Instance Attribute Details

#cacheObject



136
137
138
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 136

def cache
  @cache || _raise_not_connected
end

#configurationObject (readonly)

Returns the value of attribute configuration.



76
77
78
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76

def configuration
  @configuration
end

#hostObject (readonly)

Returns the value of attribute host.



76
77
78
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



76
77
78
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



76
77
78
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76

def port
  @port
end

#realObject



71
72
73
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 71

def real
  @real ||= Real.new
end

#usernameObject (readonly)

Returns the value of attribute username.



76
77
78
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76

def username
  @username
end

Class Method Details

.off!Object



29
30
31
32
33
34
# 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!Object



15
16
17
18
19
20
21
22
23
24
25
26
27
# 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) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# 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

Instance Method Details

#_init_cacheObject



131
132
133
134
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 131

def _init_cache
  credentials = [host, port, username, password]
  @cache = Cache.new(configuration, credentials)
end

#_raise_not_connectedObject

Raises:

  • (Net::FTPConnectionError)


140
141
142
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 140

def _raise_not_connected
  raise(Net::FTPConnectionError, 'not connected')
end

#_real_connect_and_loginObject



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 107

def 
  @cache_connected || _raise_not_connected
  @cache_logged || _raise_not_connected

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

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

  if @chdir
    @real.chdir(@chdir)
    @chdir = nil
  end
end

#_real_connect_argsObject



123
124
125
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 123

def _real_connect_args
  [host, port]
end

#_real_login_argsObject



127
128
129
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 127

def 
  [username, password].select { |string| StringUtils.present?(string) }
end

#chdir(dirname = nil) ⇒ Object



147
148
149
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 147

def chdir(dirname = nil)
  cache.chdir(dirname)
end

#connect(host, port = PORT) ⇒ Object

connection methods



84
85
86
87
88
89
90
91
92
93
# 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)) ⇒ Object

get methods



170
171
172
173
174
175
176
177
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 170

def get(remotefile, localfile = File.basename(remotefile))
  cache.get(remotefile, localfile) do
    
    real.get(remotefile, localfile)
  end

  true
end

#getbinaryfile(remotefile, localfile = File.basename(remotefile)) ⇒ Object



188
189
190
191
192
193
194
195
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 188

def getbinaryfile(remotefile, localfile = File.basename(remotefile))
  cache.get(remotefile, localfile) do
    
    real.getbinaryfile(remotefile, localfile)
  end

  true
end

#getdirObject



155
156
157
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 155

def getdir
  chdir
end

#gettextfile(remotefile, localfile = File.basename(remotefile)) ⇒ Object



179
180
181
182
183
184
185
186
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 179

def gettextfile(remotefile, localfile = File.basename(remotefile))
  cache.get(remotefile, localfile) do
    
    real.gettextfile(remotefile, localfile)
  end

  true
end

#list(*args) ⇒ Object

list methods



161
162
163
164
165
166
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 161

def list(*args)
  cache.list(*args) do
    
    real.list(*args)
  end
end

#login(username, password) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 95

def (username, password)
  @cache = nil
  @real_logged = false
  @username = username
  @password = password

  _init_cache

  @cache_logged = true
  true
end

#put(localfile, remotefile = File.basename(localfile)) ⇒ Object



201
202
203
204
205
206
207
208
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 201

def put(localfile, remotefile = File.basename(localfile))
  cache.put(localfile, remotefile) do
    
    real.put(localfile, remotefile)
  end

  true
end

#putbinaryfile(localfile, remotefile = File.basename(localfile)) ⇒ Object

TODO: block



221
222
223
224
225
226
227
228
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 221

def putbinaryfile(localfile, remotefile = File.basename(localfile))
  cache.put(localfile, remotefile) do
    
    real.putbinaryfile(localfile, remotefile)
  end

  true
end

#puttextfile(localfile, remotefile = File.basename(localfile)) ⇒ Object

TODO: block



211
212
213
214
215
216
217
218
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 211

def puttextfile(localfile, remotefile = File.basename(localfile))
  cache.put(localfile, remotefile) do
    
    real.puttextfile(localfile, remotefile)
  end

  true
end

#pwdObject



151
152
153
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 151

def pwd
  chdir
end