Class: Gmail

Inherits:
Object
  • Object
show all
Defined in:
lib/gmail.rb

Constant Summary collapse

OOB_URI =
'urn:ietf:wg:oauth:2.0:oob'
APPLICATION_NAME =
'Lita Kintai'
CLIENT_SECRETS_PATH =
'client_secret.json'
CREDENTIALS_PATH =
File.join(Dir.home, '.credentials',
"lita-kintai.yaml")
SCOPE =
[Google::Apis::GmailV1::AUTH_GMAIL_READONLY, Google::Apis::GmailV1::AUTH_GMAIL_SEND]
USER_ID =
'default'

Class Method Summary collapse

Class Method Details

.authorization_urlObject



25
26
27
# File 'lib/gmail.rb', line 25

def self.authorization_url
  authorizer.get_authorization_url(base_url: OOB_URI)
end

.authorized?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/gmail.rb', line 16

def self.authorized?
  !service.nil? && !service.authorization.nil?
end

.credentials_from_code(code) ⇒ Object



20
21
22
23
# File 'lib/gmail.rb', line 20

def self.credentials_from_code(code)
  authorizer.get_and_store_credentials_from_code(
  user_id: USER_ID, code: code, base_url: OOB_URI)
end

.find_mail(query) ⇒ Object



29
30
31
32
33
34
35
36
# File 'lib/gmail.rb', line 29

def self.find_mail(query)
  ids = service.list_user_messages('me', q: query)

  return [] unless ids.messages
  ids.messages.map do |message|
    find_mail_by_id(message.id)
  end
end

.send_message(mail) ⇒ Object



38
39
40
41
# File 'lib/gmail.rb', line 38

def self.send_message(mail)
  message = Google::Apis::GmailV1::Message.new(raw: mail.to_s )
  result = service.send_user_message('me', message)
end