Class: Maileva::Batch
- Inherits:
-
Object
- Object
- Maileva::Batch
- Defined in:
- lib/maileva/batch.rb
Constant Summary collapse
- PART_SIZE =
30
Instance Attribute Summary collapse
-
#files ⇒ Object
readonly
Returns the value of attribute files.
-
#name ⇒ Object
Returns the value of attribute name.
-
#rule ⇒ Object
readonly
Returns the value of attribute rule.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #append_file(*files) ⇒ Object (also: #<<)
-
#initialize(type, name) ⇒ Batch
constructor
A new instance of Batch.
- #send ⇒ Object
- #send!(in_db: false) ⇒ Object
- #tmp_dir ⇒ Object
- #uploaded_count ⇒ Object
Constructor Details
#initialize(type, name) ⇒ Batch
Returns a new instance of Batch.
14 15 16 17 18 19 20 21 |
# File 'lib/maileva/batch.rb', line 14 def initialize(type, name) raise ArgumentError, "Invalid Maileva batch type '#{type}'" unless type.is_a?(Symbol) and Maileva.rules.key?(type) @type = type @name = name @rule = Maileva.rules[@type] @files = [] end |
Instance Attribute Details
#files ⇒ Object (readonly)
Returns the value of attribute files.
10 11 12 |
# File 'lib/maileva/batch.rb', line 10 def files @files end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/maileva/batch.rb', line 9 def name @name end |
#rule ⇒ Object (readonly)
Returns the value of attribute rule.
10 11 12 |
# File 'lib/maileva/batch.rb', line 10 def rule @rule end |
#type ⇒ Object
Returns the value of attribute type.
9 10 11 |
# File 'lib/maileva/batch.rb', line 9 def type @type end |
Instance Method Details
#append_file(*files) ⇒ Object Also known as: <<
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/maileva/batch.rb', line 23 def append_file(*files) files.each do |file| if file.is_a? Array file.each{|f| append_file(f)} return end raise ArgumentError, "Expecting string for 'file'" unless file.is_a?(String) raise "#{file}: no such file for batch '#{@name}' (#{@type})" unless File.file?(file) raise "#{file}: expecting .pdf" unless File.extname(file) == ".pdf" @files << file end end |
#send ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/maileva/batch.rb', line 47 def send if @files.size < Maileva.config.confirmation_threshold send! else FileUtils.mkdir_p tmp_dir unless File.directory?(tmp_dir) @files.each do |file| FileUtils.cp file, tmp_dir + File.basename(file) unless file.start_with? tmp_dir.to_s end Maileva.class_variable_get(:@@confirmation_callbacks).each{|proc| proc.call(self)} end end |
#send!(in_db: false) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/maileva/batch.rb', line 60 def send!(in_db: false) raise "Cannot send empty batch" if @files.size == 0 Maileva.class_variable_get(:@@processing_callbacks).each{|proc| proc.call(self, in_db)} @uploaded_remote_names = [] @uploaded_part_names = [] Maileva::BATCHES_IN_PROCESS_MUTEX.synchronize{ Maileva.batches_in_process << self } ftp = nil begin ftp = DoubleBagFTPS.new ftp.passive = true ftp.connect("ftps.maileva.com", 21) ftp.login(Maileva.config.ftp_login, Maileva.config.ftp_password) begin part_no = 1 @files.each_slice(PART_SIZE) do |part| send_part(ftp, part, part_no) part_no += 1 end @uploaded_part_names.each do |part| ftp.rename part + ".tmp", part + ".flw" end rescue Exception => e rollback!(ftp) raise e end Maileva.class_variable_get(:@@done_callbacks).each{|proc| proc.call(self)} rescue Exception => e Maileva.class_variable_get(:@@failure_callbacks).each{|proc| proc.call(self)} raise e ensure Maileva::BATCHES_IN_PROCESS_MUTEX.synchronize{ Maileva.batches_in_process.delete(self) } begin; ftp.close unless ftp.nil? or ftp.closed? rescue Exception; end end end |
#tmp_dir ⇒ Object
39 40 41 |
# File 'lib/maileva/batch.rb', line 39 def tmp_dir return Maileva.config.files_root + @type.to_s + @name end |
#uploaded_count ⇒ Object
43 44 45 |
# File 'lib/maileva/batch.rb', line 43 def uploaded_count return @uploaded_remote_names.size end |