Class: BBMB::Util::FtpMission
- Inherits:
-
Object
- Object
- BBMB::Util::FtpMission
- Defined in:
- lib/bbmb/util/polling_manager.rb
Instance Attribute Summary collapse
-
#backup_dir ⇒ Object
Returns the value of attribute backup_dir.
-
#delete ⇒ Object
Returns the value of attribute delete.
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#pattern ⇒ Object
Returns the value of attribute pattern.
Instance Method Summary collapse
-
#initialize(*args) ⇒ FtpMission
constructor
A new instance of FtpMission.
- #poll(&block) ⇒ Object
- #poll_file(path, &block) ⇒ Object
- #poll_remote(ftp, file) ⇒ Object
- #regexp ⇒ Object
Constructor Details
#initialize(*args) ⇒ FtpMission
Returns a new instance of FtpMission.
97 98 99 100 101 102 |
# File 'lib/bbmb/util/polling_manager.rb', line 97 def initialize(*args) super @backup_dir = BBMB.config.backup_dir if BBMB.config.respond_to?(:backup_dir) @backup_dir ||= Dir.tmpdir @regexp = Regexp.new('.*') end |
Instance Attribute Details
#backup_dir ⇒ Object
Returns the value of attribute backup_dir.
96 97 98 |
# File 'lib/bbmb/util/polling_manager.rb', line 96 def backup_dir @backup_dir end |
#delete ⇒ Object
Returns the value of attribute delete.
96 97 98 |
# File 'lib/bbmb/util/polling_manager.rb', line 96 def delete @delete end |
#directory ⇒ Object
Returns the value of attribute directory.
96 97 98 |
# File 'lib/bbmb/util/polling_manager.rb', line 96 def directory @directory end |
#pattern ⇒ Object
Returns the value of attribute pattern.
96 97 98 |
# File 'lib/bbmb/util/polling_manager.rb', line 96 def pattern @pattern end |
Instance Method Details
#poll(&block) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/bbmb/util/polling_manager.rb', line 103 def poll(&block) FileUtils.mkdir_p(@backup_dir) @regexp = Regexp.new(@pattern || '.*') uri = URI.parse(@directory) locals = [] Net::FTP.open(uri.host) do |ftp| ftp.login uri.user, uri.password ftp.chdir uri.path ftp.nlst.each do |file| if(local = poll_remote ftp, file) locals.push local end end end locals.each do |path| poll_file path, &block end end |
#poll_file(path, &block) ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/bbmb/util/polling_manager.rb', line 121 def poll_file(path, &block) File.open(path) { |io| block.call(File.basename(path), io) } rescue StandardError => err BBMB::Util::Mail.notify_error(err) end |
#poll_remote(ftp, file) ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/bbmb/util/polling_manager.rb', line 128 def poll_remote(ftp, file) if(@regexp.match file) local = File.join(@backup_dir, file) ftp.get file, local ftp.delete file if @delete local end end |
#regexp ⇒ Object
136 137 |
# File 'lib/bbmb/util/polling_manager.rb', line 136 def regexp end |