Class: BBMB::Util::FtpMission

Inherits:
Object
  • Object
show all
Defined in:
lib/bbmb/util/polling_manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_dirObject

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

#deleteObject

Returns the value of attribute delete.



96
97
98
# File 'lib/bbmb/util/polling_manager.rb', line 96

def delete
  @delete
end

#directoryObject

Returns the value of attribute directory.



96
97
98
# File 'lib/bbmb/util/polling_manager.rb', line 96

def directory
  @directory
end

#patternObject

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. 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

#regexpObject



136
137
# File 'lib/bbmb/util/polling_manager.rb', line 136

def regexp
end