Class: NaviClient::Local
- Inherits:
-
Object
- Object
- NaviClient::Local
- Includes:
- Client
- Defined in:
- lib/local/navi_local_client.rb
Overview
This class represents the client for local lockbox mode. In local-lockbox mode, all the content will be saved and also being used from the local file system
Constant Summary collapse
- CONFIG_PATH =
File.join(ENV['HOME'], '/.navi/config.yml')
Instance Method Summary collapse
- #config ⇒ Object
-
#download(message, custom_uid) ⇒ Object
Downloads the email content from imap-server and save it to the download_path.
- #getMessageUUIds(path) ⇒ Object
-
#idle_loop(imap, search_condition, folder, server, username, password, callback = nil, navi_control = nil) ⇒ Object
idle_loop.
- #initate_csv_generation(username) ⇒ Object
-
#initialize(sso_web_url = ENV['api_url'], client_type = "local", gen_csv_url = "#{ENV['api_url']}/v2/generate_csv_input", debug = true, env = ENV['env']?ENV['env']:Rails.env) ⇒ Local
constructor
A new instance of Local.
-
#login ⇒ Object
login.
- #mkdir_if_not_exist(filepath) ⇒ Object
- #override_logger(logger) ⇒ Object
- #parse_prev_failed_emails ⇒ Object
-
#save(data = {}, filename, stamp) ⇒ Object
save data to download_path with file named by filename params.
- #save_aes_key_iv(key, iv) ⇒ Object
-
#send_request(in_filenames = [], stamp, is_last: false) ⇒ Object
send bulk request to navi-ai service with list of input files downloaded from imap server.
- #set_token(token) ⇒ Object
- #update_config(data) ⇒ Object
Methods included from Client
#depcrecated_encrypt, #encrypt, #errors, #imap_connection, #logToLoggly, #logger, #process_email, #retrieve_emails, #setupLoggly, #shutdown, #time_now
Constructor Details
#initialize(sso_web_url = ENV['api_url'], client_type = "local", gen_csv_url = "#{ENV['api_url']}/v2/generate_csv_input", debug = true, env = ENV['env']?ENV['env']:Rails.env) ⇒ Local
Returns a new instance of Local.
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/local/navi_local_client.rb', line 13 def initialize(sso_web_url = ENV['api_url'], client_type = "local", gen_csv_url = "#{ENV['api_url']}/v2/generate_csv_input", debug = true, env = ENV['env']?ENV['env']:Rails.env ) # flag to print Ruby library debug info (very detailed) @net_imap_debug = false # flag to mark email as read after gets downloaded. @mark_as_read = false # flag to turn on/off debug mode. @debug = debug # override the log file mkdir_if_not_exist(config['client_log_file']) @logger = Logger.new(config['client_log_file']) # sso_web (authentication) config. @sso_web_url = sso_web_url # authentication token received from sso_web used to authenticate the request to database_api @token = nil @download_path = config['download_path'] + config['username'] + '/' @current_user_email = config['username'] # client_type @client_type = client_type @gen_csv_url = gen_csv_url @env = env end |
Instance Method Details
#config ⇒ Object
203 204 205 |
# File 'lib/local/navi_local_client.rb', line 203 def config YAML.load_file(CONFIG_PATH) end |
#download(message, custom_uid) ⇒ Object
Downloads the email content from imap-server and save it to the download_path
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/local/navi_local_client.rb', line 70 def download(, custom_uid) download_path = config['download_path'] + config['username'] + '/' if ['text/plain', 'text/html'].include? .mime_type h = Hash.new out_file = download_path + .mime_type + "/"+custom_uid mkdir_if_not_exist(out_file) File.open(out_file, 'w') { |file| file.write(encrypt(.decoded)) } key = .mime_type.split("/").join("_") h[key] = out_file return h end end |
#getMessageUUIds(path) ⇒ Object
216 217 218 |
# File 'lib/local/navi_local_client.rb', line 216 def getMessageUUIds(path) File.directory?(path) ? (Dir.entries path).map { |i| i.split("_").first.to_i } : [] end |
#idle_loop(imap, search_condition, folder, server, username, password, callback = nil, navi_control = nil) ⇒ Object
idle_loop
check for any further mail with “real-time” responsiveness. retrieve any mail from a folder, following specified search condition for any mail retrieved call a specified block
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/local/navi_local_client.rb', line 141 def idle_loop(imap, search_condition, folder, server, username, password, callback = nil, navi_control = nil) @logger.info "\nwaiting new mails (IDLE loop)..." loop do begin imap.select folder imap.idle do |resp| # You'll get all the things from the server. For new emails (EXISTS) if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS" # @logger.debug resp.inspect if @debug # Got something. Send DONE. This breaks you out of the blocking call @logger.debug "New mail received" if @debug imap.idle_done end end # We're out, which means there are some emails ready for us. # Go do a search for UNSEEN and fetch them. filenames = [] stamp = (Time.now.to_f * 1000).to_s retrieve_emails(imap, search_condition, folder) { |mail, i, isLast, id| callback.call(mail, i, id, false) unless callback.nil? filenames << process_email(mail, id, stamp) callback.call(mail, i, id, true) unless callback.nil? } @logger.info "Sending Request for #{filenames.size} emails to Navi AI." navi_control.call(true) unless navi_control.nil? stamp = (Time.now.to_f * 1000).to_s self.send_request(filenames, stamp, is_last: true) # HTTPService::NaviAI.generate_csv("#{@gen_csv_url}?email=#{username}", @token) @logger.debug "Process Completed." if @debug navi_control.call(false) unless navi_control.nil? rescue SignalException => e # http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby @logger.info "Signal received at #{time_now}: #{e.class}. #{e.}" shutdown imap rescue Net::IMAP::Error => e @logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.}" imap = imap_connection(server, username, password) #if e.message == 'connection closed' @logger.info "reconnected to server: #{server}" rescue Exception => e @logger.error "Something went wrong at #{time_now}: #{e.class}. #{e.}" imap = imap_connection(server, username, password) @logger.info "reconnected to server: #{server}" end end end |
#initate_csv_generation(username) ⇒ Object
199 200 201 |
# File 'lib/local/navi_local_client.rb', line 199 def initate_csv_generation(username) HTTPService::NaviAI.generate_csv("#{@gen_csv_url}?email=#{username}", @token) end |
#login ⇒ Object
login
login to the navi-cloud and get the authentication token
46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/local/navi_local_client.rb', line 46 def login url = "#{@sso_web_url}/oauth/token" provider_url = url token = HTTParty.post(provider_url, body: { client_id: config["uid"], # get from sso_web application client_secret: config["secret_key"], grant_type: "client_credentials" } )['access_token'] @token = "Token: #{token}##{config['username']}" end |
#mkdir_if_not_exist(filepath) ⇒ Object
127 128 129 130 131 132 |
# File 'lib/local/navi_local_client.rb', line 127 def mkdir_if_not_exist(filepath) dirname = File.dirname(filepath) unless File.directory?(dirname) FileUtils.mkdir_p(dirname) end end |
#override_logger(logger) ⇒ Object
59 60 61 |
# File 'lib/local/navi_local_client.rb', line 59 def override_logger(logger) @logger = logger end |
#parse_prev_failed_emails ⇒ Object
224 225 226 227 228 229 230 231 232 233 |
# File 'lib/local/navi_local_client.rb', line 224 def parse_prev_failed_emails changes = false Dir[@download_path + 'inputs/temp*'].each do |file_name| if File.file? file_name HTTPService::NaviAI.start(filepath: file_name, client_type: @client_type, token: @token, email: @current_user_email ) changes = true end end return changes end |
#save(data = {}, filename, stamp) ⇒ Object
save data to download_path with file named by filename params. Input is hashmap, and it save the hashmap as yml format.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/local/navi_local_client.rb', line 90 def save(data={}, filename, stamp) download_path = config['download_path'] + config['username'] + '/' temp_path = config['download_path'] + config['username'] + "/inputs/temp-#{stamp}" filepath = download_path + filename + ".yml" mkdir_if_not_exist(filepath) mkdir_if_not_exist(temp_path) File.open(filepath, 'w') do |f| f.puts(data.to_yaml) end File.open(temp_path, 'a') do |f| f.puts(filepath) end return filepath end |
#save_aes_key_iv(key, iv) ⇒ Object
207 208 209 210 211 212 213 214 |
# File 'lib/local/navi_local_client.rb', line 207 def save_aes_key_iv(key, iv) aes_key_path = File.join(ENV['HOME'] + '/.navi/.aes.key') iv_path = File.join(ENV['HOME'] + '/.navi/.aes.iv') File.open(aes_key_path, "w") { |f| f.write(Base64.encode64(key)) } File.open(iv_path, "w") { |f| f.write(Base64.encode64(iv)) } return aes_key_path, iv_path end |
#send_request(in_filenames = [], stamp, is_last: false) ⇒ Object
send bulk request to navi-ai service with list of input files downloaded from imap server.
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/local/navi_local_client.rb', line 111 def send_request(in_filenames = [], stamp, is_last: false) unless in_filenames.empty? download_path = config['download_path'] filename = download_path + config['username'] + "/inputs/#{stamp}" mkdir_if_not_exist(filename) File.open(filename, 'w') do |f| in_filenames.each { |element| f.puts(element) } end response = HTTPService::NaviAI.start(filepath: filename, client_type: @client_type, token: @token, email: config['username'], is_last: is_last) return response end end |
#set_token(token) ⇒ Object
63 64 65 |
# File 'lib/local/navi_local_client.rb', line 63 def set_token(token) @token = token end |
#update_config(data) ⇒ Object
220 221 222 |
# File 'lib/local/navi_local_client.rb', line 220 def update_config(data) File.write(CONFIG_PATH, YAML.dump(data)) end |