Module: GmailClient
- Included in:
- NaviEmailSync::GmailApi, NaviEmailSync::GmailCloudApi
- Defined in:
- lib/gmail_client.rb
Overview
This module provides the common functionality that will be needed for local and cloud module.
Constant Summary collapse
- OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'.freeze
- APPLICATION_NAME =
'Gmail Api Sync'.freeze
Instance Attribute Summary collapse
-
#auth_client ⇒ Object
Returns the value of attribute auth_client.
-
#downloaded ⇒ Object
Returns the value of attribute downloaded.
-
#message_ids ⇒ Object
Returns the value of attribute message_ids.
-
#total_emails ⇒ Object
Returns the value of attribute total_emails.
Instance Method Summary collapse
- #all_message_ids(label) ⇒ Object
- #all_messages(label) ⇒ Object
- #authorization_url ⇒ Object
- #authorize(code, user_id) ⇒ Object
- #encrypt(data) ⇒ Object
- #errors ⇒ Object
-
#fetch_emails(&process_email_block) ⇒ Object
retrieve_emails.
- #get_user_info(user_id) ⇒ Object
- #init(client_path, user_cred_path, user_id) ⇒ Object
- #init_apis(cred) ⇒ Object
- #init_apis_cloud(client_path, scope, token, type = "code") ⇒ Object
- #init_messages(label) ⇒ Object
- #logger ⇒ Object
- #logToLoggly(messageBody) ⇒ Object
-
#process_email(mail, uid, stamp = nil) ⇒ Object
Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service.
- #setupLoggly(tag) ⇒ Object
Instance Attribute Details
#auth_client ⇒ Object
Returns the value of attribute auth_client.
32 33 34 |
# File 'lib/gmail_client.rb', line 32 def auth_client @auth_client end |
#downloaded ⇒ Object
Returns the value of attribute downloaded.
26 27 28 |
# File 'lib/gmail_client.rb', line 26 def downloaded @downloaded end |
#message_ids ⇒ Object
Returns the value of attribute message_ids.
28 29 30 |
# File 'lib/gmail_client.rb', line 28 def @message_ids end |
#total_emails ⇒ Object
Returns the value of attribute total_emails.
30 31 32 |
# File 'lib/gmail_client.rb', line 30 def total_emails @total_emails end |
Instance Method Details
#all_message_ids(label) ⇒ Object
191 192 193 194 195 196 |
# File 'lib/gmail_client.rb', line 191 def label mids = [] = label .each { |m| mids << m.id } mids end |
#all_messages(label) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/gmail_client.rb', line 173 def label = [] next_page = nil begin result = @service.('me', max_results: 1000, page_token: next_page, label_ids: [label]) += result. # break if messages.size >= 1 next_page = result.next_page_token end while next_page end |
#authorization_url ⇒ Object
93 94 95 |
# File 'lib/gmail_client.rb', line 93 def @authorizer.(base_url: OOB_URI) end |
#authorize(code, user_id) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/gmail_client.rb', line 58 def code, user_id credentials = @authorizer.get_and_store_credentials_from_code( user_id: user_id, code: code, base_url: OOB_URI ) unless credentials.nil? init_apis credentials end credentials end |
#encrypt(data) ⇒ Object
247 248 249 |
# File 'lib/gmail_client.rb', line 247 def encrypt(data) Base64.encode64(data) end |
#errors ⇒ Object
40 41 42 |
# File 'lib/gmail_client.rb', line 40 def errors @errors end |
#fetch_emails(&process_email_block) ⇒ Object
retrieve_emails
retrieve any mail from a folder, followin specified serach condition for any mail retrieved call a specified block
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 |
# File 'lib/gmail_client.rb', line 103 def fetch_emails(&process_email_block) @message_ids.each_with_index do |, i| begin # fetch all the email contents # data = imap.uid_fetch(message_id, "RFC822") if @auth_client.expired? @auth_client.grant_type = "refresh_token" @auth_client.refresh! @service. = Signet::OAuth2::Client.new(JSON.parse(@auth_client.to_json)) end result = @service.('me', ) process_email_block.call result, i, i == @message_ids.length-1, rescue => e unless logger.nil? logger.info "Issue processing email for uuid ##{}, #{e.}" end logToLoggly({event:"EMAIL_SYNC_FAILED", env: @env, storage: @client_type, email: @current_user_email, uuid: , error: e.}) raise e end end end |
#get_user_info(user_id) ⇒ Object
187 188 189 |
# File 'lib/gmail_client.rb', line 187 def get_user_info user_id @service.get_user_profile user_id end |
#init(client_path, user_cred_path, user_id) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/gmail_client.rb', line 44 def init client_path, user_cred_path, user_id client_id = Google::Auth::ClientId.from_file(client_path) token_store = Google::Auth::Stores::FileTokenStore.new(file: user_cred_path) @authorizer = Google::Auth::UserAuthorizer.new(client_id, Google::Apis::GmailV1::AUTH_GMAIL_READONLY, token_store) credentials = @authorizer.get_credentials(user_id) unless credentials.nil? init_apis credentials end credentials end |
#init_apis(cred) ⇒ Object
68 69 70 71 72 |
# File 'lib/gmail_client.rb', line 68 def init_apis cred @service = Google::Apis::GmailV1::GmailService.new @service..application_name = APPLICATION_NAME @service. = cred end |
#init_apis_cloud(client_path, scope, token, type = "code") ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/gmail_client.rb', line 74 def init_apis_cloud client_path, scope, token, type = "code" client_secrets = Google::APIClient::ClientSecrets.load client_path @auth_client = client_secrets. @auth_client.update!(:scope => scope, :redirect_uri => "postmessage") if(type == "code") @auth_client.code = token else @auth_client.grant_type = "refresh_token" @auth_client.refresh_token = token end @auth_client.refresh! # @auth_client.client_secret = nil @service = Google::Apis::GmailV1::GmailService.new @service..application_name = APPLICATION_NAME @service. = Signet::OAuth2::Client.new(JSON.parse(@auth_client.to_json)) end |
#init_messages(label) ⇒ Object
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/gmail_client.rb', line 198 def label @message_ids = label @total_emails = @message_ids.length downloaded_email = getMessageUUIds("#{@download_path}meta/") downloaded_email = @message_ids & downloaded_email @downloaded = downloaded_email.length @downloaded = [@downloaded, @total_emails].min @message_ids = @message_ids - downloaded_email if @debug if @message_ids.empty? @logger.info "No new emails found..." elsif downloaded_email.any? @logger.info "Found emails saved in your #{@client_type}. Downloading only #{@message_ids.count} new emails..." else @logger.info "Downloading #{@message_ids.count} emails." end end end |
#logger ⇒ Object
36 37 38 |
# File 'lib/gmail_client.rb', line 36 def logger @logger end |
#logToLoggly(messageBody) ⇒ Object
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/gmail_client.rb', line 223 def logToLoggly() if(@logglyTag) begin HTTParty.post("http://logs-01.loggly.com/bulk/0d67f93b-6568-4b00-9eca-97d0ea0bd5a1/tag/#{@logglyTag}/", body: .to_json, headers: { 'Content-Type' => 'application/json' } ) rescue true end else if @debug && !@logger.nil? @logger.info "Logging to Loggly disabled" @logger.info end end end |
#process_email(mail, uid, stamp = nil) ⇒ Object
Process each email downloaded from imap-server and creates meta file that will be sent over to the navi-ai service. Meta will content information like: [‘from’, ‘to’, ‘cc’, …, ‘body_url’]. This information is then used by navi-ai to parse the body content.
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/gmail_client.rb', line 136 def process_email(mail, uid, stamp = nil) = Hash.new custom_uid = (Time.now.to_f * 1000).to_s + "_" + uid payload = mail.payload headers = payload.headers date = headers.any? { |h| h.name == 'Date' } ? headers.find { |h| h.name == 'Date' }.value : '' from = headers.any? { |h| h.name == 'From' } ? headers.find { |h| h.name == 'From' }.value : '' to = headers.any? { |h| h.name == 'To' } ? headers.find { |h| h.name == 'To' }.value : '' subject = headers.any? { |h| h.name == 'Subject' } ? headers.find { |h| h.name == 'Subject' }.value : '' cc = headers.any? { |h| h.name == 'Cc' } ? headers.find { |h| h.name == 'Cc' }.value : '' ["from"] = from ["to"] = to ["cc"] = cc ["subject"] = subject ["date"] = date if payload.parts&.any? payload.parts.map { |part| m = download({"decoded" => part.body.data.nil? ? "" : part.body.data, "mime_type" => part.mime_type.nil? ? "text/html" : part.mime_type}, custom_uid) .merge!(m) unless m.nil? } else m = download({"decoded" => payload.body.data.nil? ? "" : payload.body.data, "mime_type" => "text/html"}, custom_uid) .merge!(m) unless m.nil? end if stamp.nil? save(, "meta/#{uid.to_s + '_' + custom_uid}") else save(, "meta/#{uid.to_s + '_' + custom_uid}", stamp) end end |
#setupLoggly(tag) ⇒ Object
243 244 245 |
# File 'lib/gmail_client.rb', line 243 def setupLoggly(tag) @logglyTag = tag end |