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

#create_label(name) ⇒ Object



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

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

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



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

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)


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

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.



135
136
137
138
139
140
141
# File 'lib/gmail.rb', line 135

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



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

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
    label(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…



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

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)



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

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

#labelsObject

List the available labels



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

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

#logged_in?Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/gmail.rb', line 95

def logged_in?
  !!@logged_in
end

#loginObject

LOGIN



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

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

#logoutObject

Log out of gmail



99
100
101
102
103
104
# File 'lib/gmail.rb', line 99

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