Class: Ftpmock::NetFtpProxy
- Inherits:
-
Object
- Object
- Ftpmock::NetFtpProxy
show all
- Includes:
- MethodMissingMixin
- Defined in:
- lib/ftpmock/proxies/net_ftp_proxy.rb
Constant Summary
collapse
- Real =
begin
Net::FTP
rescue NameError
nil
end
- PORT =
21
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#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)
login(@options[:username], @options[:password]) if @options[:username]
end
end
|
Instance Attribute Details
#cache ⇒ Object
136
137
138
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 136
def cache
@cache || _raise_not_connected
end
|
#configuration ⇒ Object
Returns the value of attribute configuration.
76
77
78
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76
def configuration
@configuration
end
|
#host ⇒ Object
Returns the value of attribute host.
76
77
78
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76
def host
@host
end
|
#password ⇒ Object
Returns the value of attribute password.
76
77
78
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76
def password
@password
end
|
#port ⇒ Object
Returns the value of attribute port.
76
77
78
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 76
def port
@port
end
|
#real ⇒ Object
71
72
73
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 71
def real
@real ||= Real.new
end
|
#username ⇒ Object
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_cache ⇒ Object
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_connected ⇒ Object
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_login ⇒ Object
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 _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 ⇒ Object
123
124
125
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 123
def _real_connect_args
[host, port]
end
|
#_real_login_args ⇒ Object
127
128
129
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 127
def _real_login_args
[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
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
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_connect_and_login
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_connect_and_login
real.getbinaryfile(remotefile, localfile)
end
true
end
|
#getdir ⇒ Object
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_connect_and_login
real.gettextfile(remotefile, localfile)
end
true
end
|
#list(*args) ⇒ Object
161
162
163
164
165
166
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 161
def list(*args)
cache.list(*args) do
_real_connect_and_login
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 login(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_connect_and_login
real.put(localfile, remotefile)
end
true
end
|
#putbinaryfile(localfile, remotefile = File.basename(localfile)) ⇒ Object
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_connect_and_login
real.putbinaryfile(localfile, remotefile)
end
true
end
|
#puttextfile(localfile, remotefile = File.basename(localfile)) ⇒ Object
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_connect_and_login
real.puttextfile(localfile, remotefile)
end
true
end
|
#pwd ⇒ Object
151
152
153
|
# File 'lib/ftpmock/proxies/net_ftp_proxy.rb', line 151
def pwd
chdir
end
|