navi_client 1.0.7 → 1.0.8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3bc08ae5670fdb26dae21794e6d7afc9016a6913
4
- data.tar.gz: d7627f78cb23e0713e95596de03ca64423a8d79d
3
+ metadata.gz: 6dc4c7acfc106147f55f221429c73ee8065fee41
4
+ data.tar.gz: dc617dcb96936756409e2158f6bcd96ccf65d965
5
5
  SHA512:
6
- metadata.gz: cdd9fda78d43de15ea21a1c383a300b795722f3703305201e9bf461507cb237fb9af313d780d1affca1405abee9d49e29b2e7feac8cca3562614e9b5509fa3be
7
- data.tar.gz: b018001f65761d913b58816eb58473a789a289a8ba44c8b2f3a834756beaa40ed522de7cc06dfec057232483ff28351a4e29afcee73d5ac1e3c79e7a416f6292
6
+ metadata.gz: 152cec5e2561c48b29b04b2462d376865ccf2c85f85ec3499a92f5e79e6e0793aa75e809d7568ebaba8561ce694fb9a81b80c0815a9ded534b848da673451160
7
+ data.tar.gz: 6af82c926c26ce5dd8158d0efea07efc56d4387170200e5e052e7c8be040e2649cf3114b042925f24cbd0e47220a65c13fb6160eee5c5d89fd095dfbc7c1393d
data/lib/client.rb CHANGED
@@ -17,23 +17,6 @@ module Client
17
17
  @logger
18
18
  end
19
19
 
20
- #
21
- # login
22
- #
23
- # login to the navi-cloud and get the authentication token
24
- #
25
- def login
26
- url = "#{@sso_web_url}/oauth/token"
27
- provider_url = url
28
- @token = HTTParty.post(provider_url,
29
- body: {
30
- client_id: config["uid"], # get from sso_web application
31
- client_secret: config["secret_key"],
32
- grant_type: "client_credentials"
33
- }
34
- )['access_token']
35
- end
36
-
37
20
  #
38
21
  # imap_connection
39
22
  #
@@ -103,60 +86,6 @@ module Client
103
86
  end
104
87
  end
105
88
 
106
- #
107
- # idle_loop
108
- #
109
- # check for any further mail with "real-time" responsiveness.
110
- # retrieve any mail from a folder, following specified search condition
111
- # for any mail retrieved call a specified block
112
- #
113
- def idle_loop(imap, search_condition, folder, server, username, password)
114
-
115
- @logger.info "\nwaiting new mails (IDLE loop)..."
116
-
117
- loop do
118
- begin
119
- imap.select folder
120
- imap.idle do |resp|
121
-
122
- # You'll get all the things from the server. For new emails (EXISTS)
123
- if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
124
-
125
- @logger.debug resp.inspect if @debug
126
- # Got something. Send DONE. This breaks you out of the blocking call
127
- imap.idle_done
128
- end
129
- end
130
-
131
- # We're out, which means there are some emails ready for us.
132
- # Go do a search for UNSEEN and fetch them.
133
- filenames = []
134
- retrieve_emails(imap, search_condition, folder) { |mail| filenames << process_email(mail)}
135
- self.send_request(filenames)
136
-
137
- @logger.debug "Process Completed." if @debug
138
-
139
- rescue SignalException => e
140
- # http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby
141
- @logger.info "Signal received at #{time_now}: #{e.class}. #{e.message}"
142
- shutdown imap
143
-
144
- rescue Net::IMAP::Error => e
145
- @logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.message}"
146
-
147
- # timeout ? reopen connection
148
- imap = imap_connection(server, username, password) #if e.message == 'connection closed'
149
- @logger.info "reconnected to server: #{server}"
150
-
151
- rescue Exception => e
152
- @logger.error "Something went wrong at #{time_now}: #{e.class}. #{e.message}"
153
-
154
- imap = imap_connection(server, username, password)
155
- @logger.info "reconnected to server: #{server}"
156
- end
157
- end
158
- end
159
-
160
89
  def send_request(in_filenames = [])
161
90
  download_path = config['download_path']
162
91
  filename = download_path + "inputs/" + (Time.now.to_f * 1000).to_s
@@ -167,7 +96,7 @@ module Client
167
96
  in_filenames.each { |element| f.puts(element) }
168
97
  end
169
98
 
170
- HttpService::NaviAI.start(filename, @client_type, @token)
99
+ HTTPService::NaviAI.start(filename, @client_type, @token)
171
100
  end
172
101
 
173
102
  def process_email(mail)
@@ -193,27 +122,10 @@ module Client
193
122
  save(meta, "meta/#{custom_uid}")
194
123
  end
195
124
 
196
- def save(data={}, filename)
197
- download_path = config['download_path']
198
- filepath = download_path + filename + ".yml"
199
-
200
- mkdir_if_not_exist(filepath)
201
-
202
- File.write(filepath, data.to_yaml)
203
- return filepath
204
- end
205
-
206
125
  def encrypt(data)
207
126
  Base64.encode64(data)
208
127
  end
209
128
 
210
- def mkdir_if_not_exist(filepath)
211
- dirname = File.dirname(filepath)
212
- unless File.directory?(dirname)
213
- FileUtils.mkdir_p(dirname)
214
- end
215
- end
216
-
217
129
  def time_now
218
130
  Time.now.utc.iso8601(3)
219
131
  end
@@ -226,8 +138,4 @@ module Client
226
138
  @logger.info "#{$0} has ended (crowd applauds)"
227
139
  exit 0
228
140
  end
229
-
230
- def config
231
- YAML.load_file(ENV['HOME'] + '/.navi/config.yml')
232
- end
233
141
  end
@@ -4,24 +4,34 @@ module NaviClient
4
4
  class Cloud
5
5
  include Client
6
6
  def initialize(sso_web_url = "http://localhost:3008/")
7
- # flag to print Ruby library debug info (very detailed)
8
- @net_imap_debug = false
7
+ # flag to print Ruby library debug info (very detailed)
8
+ @net_imap_debug = false
9
9
 
10
- # flag to mark email as read after gets downloaded.
11
- @mark_as_read = false
10
+ # flag to mark email as read after gets downloaded.
11
+ @mark_as_read = false
12
12
 
13
- # flag to turn on/off debug mode.
14
- @debug = false
13
+ # flag to turn on/off debug mode.
14
+ @debug = false
15
15
 
16
- # override the log file
17
- mkdir_if_not_exist(config['client_log_file'])
18
- @logger = Logger.new(config['client_log_file'])
16
+ @logger = Logger.new
19
17
 
20
- # naviai command
21
- @cmd = 'naviai'
22
- # authentication token received from sso_web used to authenticate the request to database_api
23
- @token = nil
24
- end
18
+ # sso_web (authentication) config.
19
+ @sso_web_url = sso_web_url
20
+ # authentication token received from sso_web used to authenticate the request to database_api
21
+ @token = nil
22
+
23
+ # client_type
24
+ @client_type = "cloud"
25
+ end
26
+
27
+ #
28
+ # login
29
+ #
30
+ # login to the navi-cloud and get the authentication token
31
+ #
32
+ def login(session_token)
33
+ @token = session_token
34
+ end
25
35
 
26
36
  def download(message, custom_uid)
27
37
  download_path = config[:s3_download_folder]
@@ -37,5 +47,28 @@ module NaviClient
37
47
  return h
38
48
  end
39
49
  end
50
+
51
+ def save(data={}, filename)
52
+ download_path = config[:s3_download_folder]
53
+ filepath = download_path + "/" + filename + ".yml"
54
+
55
+ return upload_to_s3(filepath, data.to_yaml)
56
+ end
57
+
58
+ def upload_to_s3(file_path, content)
59
+ credentials = Aws::Credentials.new(config[:aws_key], config[:aws_secret])
60
+ s3 = Aws::S3::Client.new(credentials: credentials, region: config[:aws_region])
61
+ obj = s3.put_object({
62
+ body: content,
63
+ bucket: config[:s3_bucket],
64
+ key: file_path
65
+ })
66
+ return "https://s3.amazonaws.com/#{config[:s3_bucket]}/#{file_path}" if obj.successful?
67
+ return ""
68
+ end
69
+
70
+ def config
71
+ YAML.load_file(Rails.root.join("config/navi_client.yml")).with_indifferent_access
72
+ end
40
73
  end
41
74
  end
@@ -1,6 +1,6 @@
1
1
  require "httparty"
2
2
 
3
- GO_SERVER_URL = 'http://localhost:9090'
3
+ GO_SERVER_URL = 'http://localhost:9090/v2/metas'
4
4
 
5
5
  module HTTPService
6
6
  class NaviAI
@@ -11,11 +11,7 @@ module HTTPService
11
11
  else
12
12
  go_url = ''
13
13
  end
14
- HTTParty.post(GO_SERVER_URL, body: {
15
- client_type: client_type,
16
- list_meta_path: file_path,
17
- token: token
18
- })
14
+ HTTParty.post(GO_SERVER_URL, body: { client_type: client_type, list_meta_path: file_path, token: token })
19
15
  end
20
16
  end
21
17
  end
@@ -3,25 +3,44 @@ module NaviClient
3
3
  class Local
4
4
  include Client
5
5
  def initialize(sso_web_url = 'http://localhost:3008')
6
- # flag to print Ruby library debug info (very detailed)
7
- @net_imap_debug = false
6
+ # flag to print Ruby library debug info (very detailed)
7
+ @net_imap_debug = false
8
8
 
9
- # flag to mark email as read after gets downloaded.
10
- @mark_as_read = false
9
+ # flag to mark email as read after gets downloaded.
10
+ @mark_as_read = false
11
11
 
12
- # flag to turn on/off debug mode.
13
- @debug = false
12
+ # flag to turn on/off debug mode.
13
+ @debug = false
14
14
 
15
- # override the log file
16
- mkdir_if_not_exist(config['client_log_file'])
17
- @logger = Logger.new(config['client_log_file'])
15
+ # override the log file
16
+ mkdir_if_not_exist(config['client_log_file'])
17
+ @logger = Logger.new(config['client_log_file'])
18
18
 
19
- @sso_web_url = sso_web_url
20
- # naviai command
21
- @cmd = 'naviai'
22
- # authentication token received from sso_web used to authenticate the request to database_api
23
- @token = nil
24
- end
19
+ # sso_web (authentication) config.
20
+ @sso_web_url = sso_web_url
21
+ # authentication token received from sso_web used to authenticate the request to database_api
22
+ @token = nil
23
+
24
+ # client_type
25
+ @client_type = "local"
26
+ end
27
+
28
+ #
29
+ # login
30
+ #
31
+ # login to the navi-cloud and get the authentication token
32
+ #
33
+ def login
34
+ url = "#{@sso_web_url}/oauth/token"
35
+ provider_url = url
36
+ @token = HTTParty.post(provider_url,
37
+ body: {
38
+ client_id: config["uid"], # get from sso_web application
39
+ client_secret: config["secret_key"],
40
+ grant_type: "client_credentials"
41
+ }
42
+ )['access_token']
43
+ end
25
44
 
26
45
  def download(message, custom_uid)
27
46
  download_path = config['download_path']
@@ -38,5 +57,80 @@ module NaviClient
38
57
  return h
39
58
  end
40
59
  end
60
+
61
+ def save(data={}, filename)
62
+ download_path = config['download_path']
63
+ filepath = download_path + filename + ".yml"
64
+
65
+ mkdir_if_not_exist(filepath)
66
+
67
+ File.write(filepath, data.to_yaml)
68
+ return filepath
69
+ end
70
+
71
+ def mkdir_if_not_exist(filepath)
72
+ dirname = File.dirname(filepath)
73
+ unless File.directory?(dirname)
74
+ FileUtils.mkdir_p(dirname)
75
+ end
76
+ end
77
+
78
+ #
79
+ # idle_loop
80
+ #
81
+ # check for any further mail with "real-time" responsiveness.
82
+ # retrieve any mail from a folder, following specified search condition
83
+ # for any mail retrieved call a specified block
84
+ #
85
+ def idle_loop(imap, search_condition, folder, server, username, password)
86
+
87
+ @logger.info "\nwaiting new mails (IDLE loop)..."
88
+
89
+ loop do
90
+ begin
91
+ imap.select folder
92
+ imap.idle do |resp|
93
+
94
+ # You'll get all the things from the server. For new emails (EXISTS)
95
+ if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
96
+
97
+ @logger.debug resp.inspect if @debug
98
+ # Got something. Send DONE. This breaks you out of the blocking call
99
+ imap.idle_done
100
+ end
101
+ end
102
+
103
+ # We're out, which means there are some emails ready for us.
104
+ # Go do a search for UNSEEN and fetch them.
105
+ filenames = []
106
+ retrieve_emails(imap, search_condition, folder) { |mail| filenames << process_email(mail)}
107
+ self.send_request(filenames)
108
+
109
+ @logger.debug "Process Completed." if @debug
110
+
111
+ rescue SignalException => e
112
+ # http://stackoverflow.com/questions/2089421/capturing-ctrl-c-in-ruby
113
+ @logger.info "Signal received at #{time_now}: #{e.class}. #{e.message}"
114
+ shutdown imap
115
+
116
+ rescue Net::IMAP::Error => e
117
+ @logger.error "Net::IMAP::Error at #{time_now}: #{e.class}. #{e.message}"
118
+
119
+ # timeout ? reopen connection
120
+ imap = imap_connection(server, username, password) #if e.message == 'connection closed'
121
+ @logger.info "reconnected to server: #{server}"
122
+
123
+ rescue Exception => e
124
+ @logger.error "Something went wrong at #{time_now}: #{e.class}. #{e.message}"
125
+
126
+ imap = imap_connection(server, username, password)
127
+ @logger.info "reconnected to server: #{server}"
128
+ end
129
+ end
130
+ end
131
+
132
+ def config
133
+ YAML.load_file(ENV['HOME'] + '/.navi/config.yml').with_indifferent_access
134
+ end
41
135
  end
42
136
  end
@@ -1,3 +1,3 @@
1
1
  module NaviClient
2
- VERSION = "1.0.7"
2
+ VERSION = "1.0.8"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: navi_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.7
4
+ version: 1.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Surya
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-12-08 00:00:00.000000000 Z
11
+ date: 2017-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler