Class: Bankserv::Engine
- Inherits:
-
Object
- Object
- Bankserv::Engine
- Defined in:
- lib/bankserv/engine.rb
Instance Attribute Summary collapse
-
#logs ⇒ Object
Returns the value of attribute logs.
-
#process ⇒ Object
Returns the value of attribute process.
-
#service ⇒ Object
Returns the value of attribute service.
-
#success ⇒ Object
Returns the value of attribute success.
Class Method Summary collapse
- .archive_directory ⇒ Object
- .archive_directory=(dir) ⇒ Object
- .config ⇒ Object
- .input_directory ⇒ Object
- .input_directory=(dir) ⇒ Object
- .interval ⇒ Object
- .interval=(interval) ⇒ Object
- .output_directory ⇒ Object
- .output_directory=(dir) ⇒ Object
- .output_files(service) ⇒ Object
- .reply_files(service) ⇒ Object
- .running? ⇒ Boolean
- .start ⇒ Object
Instance Method Summary collapse
- #archive_file!(file) ⇒ Object
- #expecting_reply_file?(bankserv_service) ⇒ Boolean
- #finish! ⇒ Object
- #generate_input_file_name(document) ⇒ Object
-
#initialize(service) ⇒ Engine
constructor
A new instance of Engine.
- #input_services ⇒ Object
- #process! ⇒ Object
- #process_input_files ⇒ Object
- #process_output_files ⇒ Object
- #process_reply_files ⇒ Object
- #running? ⇒ Boolean
- #start! ⇒ Object
- #write_file!(document) ⇒ Object
Constructor Details
#initialize(service) ⇒ Engine
Returns a new instance of Engine.
6 7 8 9 10 11 12 13 14 |
# File 'lib/bankserv/engine.rb', line 6 def initialize(service) @service = service @success = true @logs = { reply_files: [], output_files: [], input_files: [] } end |
Instance Attribute Details
#logs ⇒ Object
Returns the value of attribute logs.
4 5 6 |
# File 'lib/bankserv/engine.rb', line 4 def logs @logs end |
#process ⇒ Object
Returns the value of attribute process.
4 5 6 |
# File 'lib/bankserv/engine.rb', line 4 def process @process end |
#service ⇒ Object
Returns the value of attribute service.
4 5 6 |
# File 'lib/bankserv/engine.rb', line 4 def service @service end |
#success ⇒ Object
Returns the value of attribute success.
4 5 6 |
# File 'lib/bankserv/engine.rb', line 4 def success @success end |
Class Method Details
.archive_directory ⇒ Object
170 171 172 |
# File 'lib/bankserv/engine.rb', line 170 def self.archive_directory config[:archive_directory] end |
.archive_directory=(dir) ⇒ Object
186 187 188 |
# File 'lib/bankserv/engine.rb', line 186 def self.archive_directory=(dir) EngineConfiguration.last.update_attributes!(archive_directory: dir) end |
.config ⇒ Object
154 155 156 |
# File 'lib/bankserv/engine.rb', line 154 def self.config EngineConfiguration.to_hash end |
.input_directory ⇒ Object
162 163 164 |
# File 'lib/bankserv/engine.rb', line 162 def self.input_directory config[:input_directory] end |
.input_directory=(dir) ⇒ Object
178 179 180 |
# File 'lib/bankserv/engine.rb', line 178 def self.input_directory=(dir) EngineConfiguration.last.update_attributes!(input_directory: dir) end |
.interval ⇒ Object
158 159 160 |
# File 'lib/bankserv/engine.rb', line 158 def self.interval config[:interval] end |
.interval=(interval) ⇒ Object
174 175 176 |
# File 'lib/bankserv/engine.rb', line 174 def self.interval=(interval) EngineConfiguration.last.update_attributes!(interval_in_minutes: interval) end |
.output_directory ⇒ Object
166 167 168 |
# File 'lib/bankserv/engine.rb', line 166 def self.output_directory config[:output_directory] end |
.output_directory=(dir) ⇒ Object
182 183 184 |
# File 'lib/bankserv/engine.rb', line 182 def self.output_directory=(dir) EngineConfiguration.last.update_attributes!(output_directory: dir) end |
.output_files(service) ⇒ Object
194 195 196 197 |
# File 'lib/bankserv/engine.rb', line 194 def self.output_files(service) return Dir.entries(service.config[:incoming_directory]).select {|file| file.upcase.starts_with? "OUTPUT" } unless ((service.is_a? Bankserv::StatementService) or (service.is_a? Bankserv::NotifyMeStatementService)) Dir.entries(service.config[:incoming_directory]).delete_if {|element| File.directory?(element)} end |
.reply_files(service) ⇒ Object
190 191 192 |
# File 'lib/bankserv/engine.rb', line 190 def self.reply_files(service) Dir.entries(service.config[:reply_directory]).select {|file| file.upcase.starts_with? "REPLY" } end |
.running? ⇒ Boolean
150 151 152 |
# File 'lib/bankserv/engine.rb', line 150 def self.running? EngineProcess.where(running: true).count > 0 end |
Instance Method Details
#archive_file!(file) ⇒ Object
119 120 121 122 123 124 125 |
# File 'lib/bankserv/engine.rb', line 119 def archive_file!(file) year, month = Date.today.year, Date.today.month Dir::mkdir("#{@service.config[:archive_directory]}/#{year}") unless File.directory?("#{@service.config[:archive_directory]}/#{year}") Dir::mkdir("#{@service.config[:archive_directory]}/#{year}/#{month}") unless File.directory?("#{@service.config[:archive_directory]}/#{year}/#{month}") FileUtils.mv(file, "#{@service.config[:archive_directory]}/#{year}/#{month}/") end |
#expecting_reply_file?(bankserv_service) ⇒ Boolean
127 128 129 |
# File 'lib/bankserv/engine.rb', line 127 def expecting_reply_file?(bankserv_service) Bankserv::Document.where(type: 'input', reply_status: nil, processed: true, client_code: bankserv_service.client_code).count > 0 end |
#finish! ⇒ Object
131 132 133 |
# File 'lib/bankserv/engine.rb', line 131 def finish! @process.update_attributes!(running: false, completed_at: Time.now, success: @success, response: @logs) end |
#generate_input_file_name(document) ⇒ Object
115 116 117 |
# File 'lib/bankserv/engine.rb', line 115 def generate_input_file_name(document) "INPUT.#{document.id}.#{Time.now.strftime('%y%m%d%H%M%S')}.txt" end |
#input_services ⇒ Object
100 101 102 |
# File 'lib/bankserv/engine.rb', line 100 def input_services Bankserv::Service.active.select(&:can_transmit?) end |
#process! ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bankserv/engine.rb', line 16 def process! begin self.start! EngineProcess.transaction do self.process_reply_files end EngineProcess.transaction do self.process_output_files end EngineProcess.transaction do self.process_input_files end ensure self.finish! end end |
#process_input_files ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bankserv/engine.rb', line 83 def process_input_files return if self.expecting_reply_file?(@service) document = nil Bankserv::Service.transaction do document = Bankserv::InputDocument.generate!(@service) if document @logs[:input_files] << "Input Document created with id: #{document.id}" if self.write_file!(document) document.mark_processed! end end end end |
#process_output_files ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/bankserv/engine.rb', line 57 def process_output_files # ------------------------------------------------------------------------------------------------------------ # | NB: ABSA place output files in our incoming directory! They take our input files from our outgoing folder.| # ------------------------------------------------------------------------------------------------------------ Engine.output_files(@service).each do |file| @logs[:output_files] << "Processing #{file}." contents = File.open("#{@service.config[:incoming_directory]}/#{file}", "rb").read if @service.is_a? Bankserv::StatementService document = Bankserv::Statement.store(contents) elsif @service.is_a? Bankserv::NotifyMeStatementService document = Bankserv::NotifyMeStatement.store("#{@service.config[:incoming_directory]}/#{file}") else document = Bankserv::OutputDocument.store(contents) end document.process! @logs[:output_files] << "Processing #{file}. Complete." self.archive_file!("#{@service.config[:incoming_directory]}/#{file}") @logs[:output_files] << "#{file} Archived." end end |
#process_reply_files ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/bankserv/engine.rb', line 40 def process_reply_files Engine.reply_files(@service).each do |file| @logs[:reply_files] << "Processing #{file}." # contents = File.open("#{Bankserv::Engine.output_directory}/#{file}", "rb").read contents = File.open("#{@service.config[:reply_directory]}/#{file}", "rb").read document = Bankserv::ReplyDocument.store(contents) document.process! @logs[:reply_files] << "Processing #{file}. Complete." self.archive_file!("#{@service.config[:incoming_directory]}/#{file}") @logs[:reply_files] << "#{file} Archived." end end |
#running? ⇒ Boolean
135 136 137 |
# File 'lib/bankserv/engine.rb', line 135 def running? @process.running? end |
#start! ⇒ Object
36 37 38 |
# File 'lib/bankserv/engine.rb', line 36 def start! @process = EngineProcess.create!(running: true) end |
#write_file!(document) ⇒ Object
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bankserv/engine.rb', line 104 def write_file!(document) transmission = Absa::H2h::Transmission::Document.build([document.to_hash]) file_name = generate_input_file_name(document) File.open("#{@service.config[:outgoing_directory]}/#{file_name}", 'w') { |f| f.write transmission } @logs[:input_files] << "Input Document File created. File name: #{file_name}" true end |