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

Constant Summary collapse

VERSION =
'0.0.9'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, password) ⇒ Gmail

Gmail.new(username, password)


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

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 Attribute Details

#peekObject

don't mark emails as read on the server when downloading them


77
78
79
# File 'lib/gmail.rb', line 77

def peek
  @peek
end

Instance Method Details

#create_label(name) ⇒ Object


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

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

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


100
101
102
103
104
105
106
107
108
# File 'lib/gmail.rb', line 100

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

#destroy_label(name) ⇒ Object


41
42
43
# File 'lib/gmail.rb', line 41

def destroy_label(name)
  imap.delete(name)
end

#disconnectObject

Shutdown socket and disconnect


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

def disconnect
  logout if logged_in?
  @imap.disconnect unless @imap.disconnected?
end

#find_label_by_attribute(attribute) ⇒ Object


72
73
74
# File 'lib/gmail.rb', line 72

def find_label_by_attribute(attribute)
  mailbox_list.find{ |label| label.attr.include?(attribute.to_sym.capitalize) }
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)

91
92
93
94
95
96
97
98
# File 'lib/gmail.rb', line 91

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.


163
164
165
166
167
168
169
# File 'lib/gmail.rb', line 163

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


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/gmail.rb', line 134

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[mailbox] ||= Mailbox.new(self, mailbox)
  end
end

#inboxObject

READING EMAILS

gmail.inbox
gmail.label('News')

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

def inbox
  in_label('inbox')
end

#inspectObject

Other…


158
159
160
# File 'lib/gmail.rb', line 158

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)


67
68
69
# File 'lib/gmail.rb', line 67

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

#labelsObject

List the available labels


58
59
60
61
62
63
64
# File 'lib/gmail.rb', line 58

def labels
  mailbox_list.inject([]) do |labels,label|
    labels << label[:name] unless label.attr.include?(:Noselect)

    labels
  end
end

#logged_in?Boolean

Returns:

  • (Boolean)

117
118
119
# File 'lib/gmail.rb', line 117

def logged_in?
  !!@logged_in
end

#loginObject

LOGIN


113
114
115
116
# File 'lib/gmail.rb', line 113

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

#logoutObject

Log out of gmail


121
122
123
124
125
126
# File 'lib/gmail.rb', line 121

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

#mailbox_listObject


53
54
55
# File 'lib/gmail.rb', line 53

def mailbox_list
  imap.list("","*")
end

#rename_label(oldname, newname) ⇒ Object


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

def rename_label(oldname, newname)
  imap.rename(oldname, newname)
end