Class: NaviClient::Cloud
- Inherits:
-
Object
- Object
- NaviClient::Cloud
- Includes:
- Client, Concurrent::Async
- Defined in:
- lib/cloud/navi_cloud_client.rb
Overview
This class represents the client for cloud version. In cloud version, all the content will be saved and also being used from the s3
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
Attributes included from Client
#downloaded, #message_ids, #total_emails
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(prefix) ⇒ Object
-
#initialize(sso_web_url = ENV['api_url'], env = Rails.env) ⇒ Cloud
constructor
A new instance of Cloud.
-
#login(session_token) ⇒ Object
login.
- #override_logger(logger) ⇒ Object
- #s3_resource_files(bucket_name, prefix) ⇒ Object
-
#save(data = {}, filename) ⇒ Object
save data to download_path with file named by filename params.
-
#send_request(in_filenames = [], is_last: false) ⇒ Object
send bulk request to navi-ai service with list of input files downloaded from imap server.
- #set_current_user_email(email) ⇒ Object
-
#upload_to_s3(file_path, content) ⇒ Object
Helper function to upload the content to the s3.
Methods included from Client
#depcrecated_encrypt, #encrypt, #errors, #fetch_emails, #imap_connection, #init_messages, #logToLoggly, #logger, #process_email, #setupLoggly, #shutdown, #time_now
Constructor Details
#initialize(sso_web_url = ENV['api_url'], env = Rails.env) ⇒ Cloud
Returns a new instance of Cloud.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/cloud/navi_cloud_client.rb', line 16 def initialize(sso_web_url = ENV['api_url'], env = Rails.env) super() # client-id used to track the process @id = SecureRandom.uuid # 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 = false @logger = nil # 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 # client_type @client_type = "cloud" @download_path = config[:s3_download_folder] + '/' # set email_address of current_user for s3 folder name @current_user_email = nil @env = env end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
14 15 16 |
# File 'lib/cloud/navi_cloud_client.rb', line 14 def id @id end |
Instance Method Details
#config ⇒ Object
136 137 138 |
# File 'lib/cloud/navi_cloud_client.rb', line 136 def config YAML.load_file(Rails.root.join("config/navi_client.yml")).with_indifferent_access end |
#download(message, custom_uid) ⇒ Object
Downloads the email content from imap-server and save it to the download_path
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/cloud/navi_cloud_client.rb', line 81 def download(, custom_uid) download_path = config[:s3_download_folder] + "/" + @current_user_email if ['text/plain', 'text/html'].include? .mime_type h = Hash.new out_file = download_path + "/" + .mime_type + "/"+custom_uid s3_filepath = upload_to_s3(out_file, encrypt(.decoded)) key = .mime_type.split("/").join("_") h[key] = s3_filepath return h end end |
#getMessageUUIds(prefix) ⇒ Object
131 132 133 134 |
# File 'lib/cloud/navi_cloud_client.rb', line 131 def getMessageUUIds(prefix) files = s3_resource_files(config[:s3_bucket], prefix) files.empty? ? [] : files.map { |i| i.empty? ? 0 : i.split('/').last.split("_").first.to_i } end |
#login(session_token) ⇒ Object
login
login to the navi-cloud and get the authentication token
56 57 58 |
# File 'lib/cloud/navi_cloud_client.rb', line 56 def login(session_token) @token = session_token end |
#override_logger(logger) ⇒ Object
47 48 49 |
# File 'lib/cloud/navi_cloud_client.rb', line 47 def override_logger(logger) @logger = logger end |
#s3_resource_files(bucket_name, prefix) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/cloud/navi_cloud_client.rb', line 121 def s3_resource_files(bucket_name, prefix) files = [] credentials = Aws::Credentials.new(config[:aws_key], config[:aws_secret]) s3 = Aws::S3::Resource.new(credentials: credentials, region: config[:aws_region]) s3.bucket(bucket_name).objects(prefix: prefix).each do |obj| files << obj.key end return files end |
#save(data = {}, filename) ⇒ Object
save data to download_path with file named by filename params. Input is hashmap, and it save the hashmap as yml format.
100 101 102 103 104 105 |
# File 'lib/cloud/navi_cloud_client.rb', line 100 def save(data={}, filename) download_path = config[:s3_download_folder] + "/" + @current_user_email filepath = download_path + "/" + filename + ".yml" return upload_to_s3(filepath, data.to_yaml) end |
#send_request(in_filenames = [], is_last: false) ⇒ Object
send bulk request to navi-ai service with list of input files downloaded from imap server.
68 69 70 71 72 73 74 75 76 |
# File 'lib/cloud/navi_cloud_client.rb', line 68 def send_request(in_filenames = [], is_last: false) unless in_filenames.blank? download_path = config['s3_download_folder'] + "/" + @current_user_email filepath = download_path + "/inputs/" + (Time.now.to_f * 1000).to_s filename = upload_to_s3(filepath, in_filenames.join("\n")) HTTPService::NaviAI.start(filepath: filename, client_id: @id, client_type: @client_type, token: @token, email: @current_user_email, is_last: is_last) end end |
#set_current_user_email(email) ⇒ Object
60 61 62 63 |
# File 'lib/cloud/navi_cloud_client.rb', line 60 def set_current_user_email(email) @current_user_email = email @download_path = config[:s3_download_folder] + '/' + email + "/" end |
#upload_to_s3(file_path, content) ⇒ Object
Helper function to upload the content to the s3
109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/cloud/navi_cloud_client.rb', line 109 def upload_to_s3(file_path, content) credentials = Aws::Credentials.new(config[:aws_key], config[:aws_secret]) s3 = Aws::S3::Client.new(credentials: credentials, region: config[:aws_region]) obj = s3.put_object({ body: content, bucket: config[:s3_bucket], key: file_path }) return file_path if obj.successful? return "" end |