lib/sapos/print/printer.rb in sapos-print-1.0.0 vs lib/sapos/print/printer.rb in sapos-print-1.0.1
- old
+ new
@@ -1,28 +1,49 @@
module Sapos
module Print
class Printer
+
def initialize(config)
@config = config
end
- def print(raw_message)
- puts "Print =>"
- filename = SecureRandom.hex
+ def print(args = {})
+ raw_message = args[:document]
+
+ log_text = "Print #{args[:document_number]},#{args[:print_control]}"
+ filename = "#{args[:document_number]}.print" if args[:document_number]
+ filename = SecureRandom.hex if filename.nil?
result = false
raw = raw_message.force_encoding("UTF-8")
begin
- printable = data(raw_message)
- File.open(filename, 'w'){|f| f.write(printable)}
- result = send_to_printer({file: filename})
- puts "Result: #{result}"
+ if args[:print_control] && args[:document_number]
+ print_dir = File.join(Sapos::Print.app_directory, 'print')
+ FileUtils.mkdir_p print_dir unless File.exists?(print_dir)
+
+ filename = File.join(print_dir, filename)
+ if !File.exists?(filename)
+ printable = data(raw_message)
+ File.open(filename, 'w'){|f| f.write(printable)}
+ result = send_to_printer({file: filename})
+ #puts "Result: #{result}"
+ log_text += " => OK"
+ end
+ else
+ printable = data(raw_message)
+ File.open(filename, 'w'){|f| f.write(printable)}
+ result = send_to_printer({file: filename})
+ File.delete(filename) if File.exists?(filename)
+ #puts "Result: #{result}"
+ log_text += " => OK"
+ end
+ puts log_text
rescue => e
puts "#{e.message}"
self.errors = e.message
result = false
ensure
- File.delete(filename) if File.exists?(filename)
+ #File.delete(filename) if File.exists?(filename)
return result
end
end
\ No newline at end of file