Class: Gmail

Inherits:
Object show all
Defined in:
lib/gmail.rb,
lib/gmail/mailbox.rb,
lib/gmail/message.rb

Defined Under Namespace

Classes: Mailbox, Message, NoLabel

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Gmail

Gmail.new(username, password)



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

def initialize(username, password)
  # This is to hide the username and password, not like it REALLY needs hiding, but ... you know.
  # Could be helpful when demoing the gem in irb, these bits won't show up that way.
  class << self
    class << self
      attr_accessor :username, :password
    end
  end
  meta.username = username =~ /@/ ? username : username + '@gmail.com'
  meta.password = password
  @imap = Net::IMAP.new('imap.gmail.com',993,true,nil,false)
  if block_given?
     # This is here intentionally. Normally, we get auto logged-in when first needed.
    yield self
    logout
  end
end

Instance Method Details

#create_label(name) ⇒ Object



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

def create_label(name)
  imap.create(name)
end

#deliver(mail = nil, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
# File 'lib/gmail.rb', line 76

def deliver(mail=nil, &block)
  require 'net/smtp'
  require 'smtp_tls'
  require 'mail'
  mail = Mail.new(&block) if block_given?
  mail.delivery_method(*smtp_settings)
  mail.from = meta.username unless mail.from
  mail.deliver!
end

#generate_message(&block) ⇒ Object

MAKING EMAILS

gmail.generate_message do
  ...inside Mail context...
end

gmail.deliver do ... end

mail = Mail.new...
gmail.deliver!(mail)


67
68
69
70
71
72
73
74
# File 'lib/gmail.rb', line 67

def generate_message(&block)
  require 'net/smtp'
  require 'smtp_tls'
  require 'mail'
  mail = Mail.new(&block)
  mail.delivery_method(*smtp_settings)
  mail
end

#imapObject

Accessor for @imap, but ensures that it’s logged in first.



133
134
135
136
137
138
139
# File 'lib/gmail.rb', line 133

def imap
  unless logged_in?
    
    at_exit { logout } # Set up auto-logout for later.
  end
  @imap
end

#in_mailbox(mailbox, &block) ⇒ Object Also known as: in_label



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/gmail.rb', line 104

def in_mailbox(mailbox, &block)
  if block_given?
    mailbox_stack << mailbox
    unless @selected == mailbox.name
      imap.select(mailbox.name)
      @selected = mailbox.name
    end
    value = block.arity == 1 ? block.call(mailbox) : block.call
    mailbox_stack.pop
    # Select previously selected mailbox if there is one
    if mailbox_stack.last
      imap.select(mailbox_stack.last.name)
      @selected = mailbox.name
    end
    return value
  else
    mailboxes[name] ||= Mailbox.new(self, mailbox)
  end
end

#inboxObject

READING EMAILS

gmail.inbox
gmail.label('News')


35
36
37
# File 'lib/gmail.rb', line 35

def inbox
  in_label('inbox')
end

#inspectObject

Other…



128
129
130
# File 'lib/gmail.rb', line 128

def inspect
  "#<Gmail:#{'0x%x' % (object_id << 1)} (#{meta.username}) #{'dis' if !logged_in?}connected>"
end

#label(name) ⇒ Object Also known as: mailbox

gmail.label(name)



50
51
52
# File 'lib/gmail.rb', line 50

def label(name)
  mailboxes[name] ||= Mailbox.new(self, name)
end

#labelsObject

List the available labels



44
45
46
47
# File 'lib/gmail.rb', line 44

def labels
  (imap.list("", "%") + imap.list("[Gmail]/", "%")).inject([]) { |labels,label|
    label[:name].each_line { |l| labels << l }; labels }
end

#logged_in?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/gmail.rb', line 93

def logged_in?
  !!@logged_in
end

#loginObject

LOGIN



89
90
91
92
# File 'lib/gmail.rb', line 89

def 
  res = @imap.(meta.username, meta.password)
  @logged_in = true if res && res.name == 'OK'
end

#logoutObject

Log out of gmail



97
98
99
100
101
102
# File 'lib/gmail.rb', line 97

def logout
  if logged_in?
    res = @imap.logout
    @logged_in = false if res.name == 'OK'
  end
end