lib/client.rb in navi_client-1.0.1 vs lib/client.rb in navi_client-1.0.2

- old
+ new

@@ -7,11 +7,12 @@ # login # # login to the navi-cloud and get the authentication token # def login - provider_url = "http://localhost:3008/oauth/token" + 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" @@ -67,24 +68,20 @@ return else @logger.debug "\nProcessing #{message_ids.count} mails.\n" end end - message_ids.each_with_index do |message_id, i| # fetch all the email contents data = imap.fetch(message_id, "RFC822") - data.each do |d| msg = d.attr['RFC822'] # instantiate a Mail object to avoid further IMAP parameters nightmares mail = Mail.read_from_string msg # call the block with mail object as param - start = (i == 0) - last = (i == message_ids-1) - process_email_block.call mail, start, last + process_email_block.call mail # mark as read if @mark_as_read imap.store(message_id, "+FLAGS", [:Seen]) end @@ -117,11 +114,14 @@ end end # We're out, which means there are some emails ready for us. # Go do a search for UNSEEN and fetch them. - retrieve_emails(imap, search_condition, folder) { |mail| process_email mail } + filenames = [] + retrieve_emails(imap, search_condition, folder) { |mail| filenames << process_email mail } + self.send_request(filenames) + @logger.debug "Process Completed." if @debug rescue SignalException => e # http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby @logger.info "Signal received at #{time_now}: #{e.class}. #{e.message}" @@ -141,11 +141,22 @@ @logger.info "reconnected to server: #{server}" end end end - def process_email(mail, start, last) + def send_request(in_filenames = []) + download_path = config['download_path'] + filename = download_path + "inputs/" + (Time.now.to_f * 1000).to_s + + File.open(filename, 'w') do |f| + in_filenames.each { |element| f.puts(element) } + end + + HttpService::NaviAI.start(filename, @client_type, @token) + end + + def process_email(mail) meta = Hash.new custom_uid = (Time.now.to_f * 1000).to_s + "_" + mail.__id__.to_s meta["from"] = mail.from.first meta["to"] = mail.to.join(";") unless mail.to.nil? @@ -153,21 +164,18 @@ meta["subject"] = mail.subject meta["date"] = mail.date.to_s if mail.multipart? for i in 0...mail.parts.length - m = @local_flag ? download_local(mail, custom_uid) : download_s3(mail, custom_uid) + m = download(mail, custom_uid) meta.merge!(m) unless m.nil? end else - m = @local_flag ? download_local(mail, custom_uid) : download_s3(mail, custom_uid) + m = download(mail, custom_uid) meta.merge!(m) unless m.nil? end - meta_file_path = save(meta, "meta/#{custom_uid}") - pid = Process.spawn(@cmd+" -f=#{meta_file_path} -t=#{@token}") - - HTTPService::NaviAI.start(start, last) + save(meta, "meta/#{custom_uid}") end def save(data={}, filename) download_path = config['download_path'] filepath = download_path + filename + ".yml"