Class: NaviClient::Cloud

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/cloud/navi_cloud_client.rb

Instance Method Summary collapse

Methods included from Client

#encrypt, #imap_connection, #logger, #process_email, #retrieve_emails, #shutdown, #time_now

Constructor Details

#initialize(sso_web_url = "http://localhost:3008/") ⇒ Cloud

Returns a new instance of Cloud.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/cloud/navi_cloud_client.rb', line 6

def initialize(sso_web_url = "http://localhost:3008/")
  # 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"
end

Instance Method Details

#configObject



84
85
86
# File 'lib/cloud/navi_cloud_client.rb', line 84

def config
  YAML.load_file(Rails.root.join("config/navi_client.yml")).with_indifferent_access
end

#download(message, custom_uid) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/cloud/navi_cloud_client.rb', line 50

def download(message, custom_uid)
  download_path = config[:s3_download_folder]
  if ['text/plain', 'text/html'].include? message.mime_type

    h = Hash.new
    out_file = download_path + "/" + message.mime_type + "/"+custom_uid

    s3_filepath = upload_to_s3(out_file, encrypt(message.decoded))
    key = message.mime_type.split("/").join("_")

    h[key] = s3_filepath
    return h
  end
end

#login(session_token) ⇒ Object

login

login to the navi-cloud and get the authentication token



36
37
38
# File 'lib/cloud/navi_cloud_client.rb', line 36

def (session_token)
  @token = session_token
end

#override_logger(logger) ⇒ Object



27
28
29
# File 'lib/cloud/navi_cloud_client.rb', line 27

def override_logger(logger)
  @logger = logger
end

#save(data = {}, filename) ⇒ Object



65
66
67
68
69
70
# File 'lib/cloud/navi_cloud_client.rb', line 65

def save(data={}, filename)
  download_path = config[:s3_download_folder]
  filepath = download_path + "/" + filename + ".yml"

  return upload_to_s3(filepath, data.to_yaml)
end

#send_request(in_filenames = []) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/cloud/navi_cloud_client.rb', line 40

def send_request(in_filenames = [])
  unless in_filenames.blank?
    download_path = config['s3_download_folder']
    filepath = download_path + "/inputs/" + (Time.now.to_f * 1000).to_s
    filename = upload_to_s3(filepath, in_filenames.join("\n"))

    HTTPService::NaviAI.start(filename, @client_type, @token)
  end
end

#upload_to_s3(file_path, content) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
# File 'lib/cloud/navi_cloud_client.rb', line 72

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