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 15 |
# File 'lib/bankserv/engine.rb', line 6 def initialize(service) @service = service @logs = { reply_files: [], output_files: [], input_files: [] } @success = true 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
187 188 189 |
# File 'lib/bankserv/engine.rb', line 187 def self.archive_directory config[:archive_directory] end |
.archive_directory=(dir) ⇒ Object
203 204 205 |
# File 'lib/bankserv/engine.rb', line 203 def self.archive_directory=(dir) EngineConfiguration.last.update_attributes!(archive_directory: dir) end |
.config ⇒ Object
171 172 173 |
# File 'lib/bankserv/engine.rb', line 171 def self.config EngineConfiguration.to_hash end |
.input_directory ⇒ Object
179 180 181 |
# File 'lib/bankserv/engine.rb', line 179 def self.input_directory config[:input_directory] end |
.input_directory=(dir) ⇒ Object
195 196 197 |
# File 'lib/bankserv/engine.rb', line 195 def self.input_directory=(dir) EngineConfiguration.last.update_attributes!(input_directory: dir) end |
.interval ⇒ Object
175 176 177 |
# File 'lib/bankserv/engine.rb', line 175 def self.interval config[:interval] end |
.interval=(interval) ⇒ Object
191 192 193 |
# File 'lib/bankserv/engine.rb', line 191 def self.interval=(interval) EngineConfiguration.last.update_attributes!(interval_in_minutes: interval) end |
.output_directory ⇒ Object
183 184 185 |
# File 'lib/bankserv/engine.rb', line 183 def self.output_directory config[:output_directory] end |
.output_directory=(dir) ⇒ Object
199 200 201 |
# File 'lib/bankserv/engine.rb', line 199 def self.output_directory=(dir) EngineConfiguration.last.update_attributes!(output_directory: dir) end |
.output_files(service) ⇒ Object
211 212 213 214 |
# File 'lib/bankserv/engine.rb', line 211 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
207 208 209 |
# File 'lib/bankserv/engine.rb', line 207 def self.reply_files(service) Dir.entries(service.config[:reply_directory]).select {|file| file.upcase.starts_with? "REPLY" } end |
.running? ⇒ Boolean
167 168 169 |
# File 'lib/bankserv/engine.rb', line 167 def self.running? EngineProcess.where(running: true).count > 0 end |
Instance Method Details
#archive_file!(file) ⇒ Object
136 137 138 139 140 141 142 |
# File 'lib/bankserv/engine.rb', line 136 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
144 145 146 |
# File 'lib/bankserv/engine.rb', line 144 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
148 149 150 |
# File 'lib/bankserv/engine.rb', line 148 def finish! @process.update_attributes!(running: false, completed_at: Time.now, success: @success, response: @logs) end |
#generate_input_file_name(document) ⇒ Object
132 133 134 |
# File 'lib/bankserv/engine.rb', line 132 def generate_input_file_name(document) "INPUT.#{document.id}.#{Time.now.strftime('%y%m%d%H%M%S')}.txt" end |
#input_services ⇒ Object
110 111 112 |
# File 'lib/bankserv/engine.rb', line 110 def input_services Bankserv::Service.active.select(&:can_transmit?) end |
#process! ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'lib/bankserv/engine.rb', line 17 def process! self.start! self.process_reply_files self.process_output_files self.process_input_files self.finish! # self.perform_post_checks! end |
#process_input_files ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/bankserv/engine.rb', line 85 def process_input_files # input_services.each do |bankserv_service| begin 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 rescue Exception => e @logs[:input_files] << "Error occured! #{e.}" @logs[:input_files] << "Backtrace: #{e.backtrace}" @success = false end # end end |
#process_output_files ⇒ Object
53 54 55 56 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 82 83 |
# File 'lib/bankserv/engine.rb', line 53 def process_output_files # ------------------------------------------------------------------------------------------------------------ # | NB: ABSA place output files in our incoming directory! They take our input files from our outgoing folder.| # ------------------------------------------------------------------------------------------------------------ begin 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 rescue Exception => e @logs[:output_files] << "Error occured! #{e.}" @logs[:output_files] << "Backtrace: #{e.backtrace}" @success = false end end |
#process_reply_files ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bankserv/engine.rb', line 30 def process_reply_files begin 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 rescue Exception => e @logs[:reply_files] << "Error occured! #{e.}" @logs[:reply_files] << "Backtrace: #{e.backtrace}" @success = false end end |
#running? ⇒ Boolean
152 153 154 |
# File 'lib/bankserv/engine.rb', line 152 def running? @process.running? end |
#start! ⇒ Object
26 27 28 |
# File 'lib/bankserv/engine.rb', line 26 def start! @process = EngineProcess.create!(running: true) end |
#write_file!(document) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/bankserv/engine.rb', line 114 def write_file!(document) begin 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}" rescue Exception => e @logs[:input_files] << "Error occured. #{e.}" @logs[:input_files] << "Backtrace: #{e.backtrace}" return false end true end |