Class: NaviClient::Local

Inherits:
Object
  • Object
show all
Includes:
Client
Defined in:
lib/local/navi_local_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') ⇒ Local

Returns a new instance of Local.



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

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

  # 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

  # client_type
  @client_type = "local"
end

Instance Method Details

#configObject



147
148
149
# File 'lib/local/navi_local_client.rb', line 147

def config
  YAML.load_file(ENV['HOME'] + '/.navi/config.yml')
end

#download(message, custom_uid) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/local/navi_local_client.rb', line 45

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

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

    File.open(out_file, 'w') { |file| file.write(encrypt(message.decoded)) }
    key = message.mime_type.split("/").join("_")

    h[key] = out_file
    return h
  end
end

#idle_loop(imap, search_condition, folder, server, username, password) ⇒ 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



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/local/navi_local_client.rb', line 100

def idle_loop(imap, search_condition, folder, server, username, password)

  @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
          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 = []
      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}"
      shutdown imap

    rescue Net::IMAP::Error => e
      @logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.message}"

      # timeout ? reopen connection
      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.message}"

      imap = imap_connection(server, username, password)
      @logger.info "reconnected to server: #{server}"
    end
  end
end

#loginObject

login

login to the navi-cloud and get the authentication token



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/local/navi_local_client.rb', line 33

def 
  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']
end

#mkdir_if_not_exist(filepath) ⇒ Object



86
87
88
89
90
91
# File 'lib/local/navi_local_client.rb', line 86

def mkdir_if_not_exist(filepath)
  dirname = File.dirname(filepath)
  unless File.directory?(dirname)
    FileUtils.mkdir_p(dirname)
  end
end

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



61
62
63
64
65
66
67
68
69
# File 'lib/local/navi_local_client.rb', line 61

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

  mkdir_if_not_exist(filepath)

  File.write(filepath, data.to_yaml)
  return filepath
end

#send_request(in_filenames = []) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/local/navi_local_client.rb', line 71

def send_request(in_filenames = [])
  unless in_filenames.blank?
    download_path = config['download_path']
    filename = download_path + "inputs/" + (Time.now.to_f * 1000).to_s

    mkdir_if_not_exist(filename)

    File.open(filename, 'w') do |f|
      in_filenames.each { |element| f.puts(element) }
    end

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