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.



92
93
94
95
# File 'lib/bbmb/util/polling_manager.rb', line 92

def initialize(*args)
  super
  @regexp = Regexp.new('.*')
end

Instance Attribute Details

#backup_dirObject

Returns the value of attribute backup_dir.



91
92
93
# File 'lib/bbmb/util/polling_manager.rb', line 91

def backup_dir
  @backup_dir
end

#deleteObject

Returns the value of attribute delete.



91
92
93
# File 'lib/bbmb/util/polling_manager.rb', line 91

def delete
  @delete
end

#directoryObject

Returns the value of attribute directory.



91
92
93
# File 'lib/bbmb/util/polling_manager.rb', line 91

def directory
  @directory
end

#patternObject

Returns the value of attribute pattern.



91
92
93
# File 'lib/bbmb/util/polling_manager.rb', line 91

def pattern
  @pattern
end

Instance Method Details

#poll(&block) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/bbmb/util/polling_manager.rb', line 96

def poll(&block)
  @backup_dir ||= Dir.tmpdir
  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



115
116
117
118
119
120
121
# File 'lib/bbmb/util/polling_manager.rb', line 115

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



122
123
124
125
126
127
128
129
# File 'lib/bbmb/util/polling_manager.rb', line 122

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



130
131
# File 'lib/bbmb/util/polling_manager.rb', line 130

def regexp
end