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

Returns a new instance of Gmail.



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

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.
  meta = class << self
    class << self
      attr_accessor :username, :password
    end
    self
  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?
    @imap.(username, password)
    yield self
    logout
  end
end

Instance Method Details

#create_label(name) ⇒ Object



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

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

#imapObject

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



39
40
41
42
43
44
45
46
# File 'lib/gmail.rb', line 39

def imap
  if @imap.disconnected?
    meta = class << self; self end
    @imap.(meta.username, meta.password)
    at_exit { logout } # Set up auto-logout for later.
  end
  @imap
end

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

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gmail.rb', line 64

def in_mailbox(mailbox, &block)
  raise ArgumentError, "Must provide a code block" unless 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
end

#inboxObject

Accessors for Gmail things



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

def inbox
  mailbox('inbox')
end

#labelsObject

List the available labels



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

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

#logoutObject

Log out of gmail



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

def logout
  @imap.logout unless @imap.disconnected?
end

#mailbox(name) ⇒ Object

Accessors for IMAP things



30
31
32
# File 'lib/gmail.rb', line 30

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

#new_messageObject



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

def new_message
  MIME::Message.generate
end

#open_smtp(&block) ⇒ Object

Raises:

  • (ArgumentError)


82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/gmail.rb', line 82

def open_smtp(&block)
  raise ArgumentError, "This method is to be used with a block." unless block_given?
  meta = class << self; self end
  puts "Opening SMTP..."
  Net::SMTP.start('smtp.gmail.com', 587, 'localhost.localdomain', meta.username, meta.password, 'plain', true) do |smtp|
    puts "SMTP open."
    block.call(lambda {|to, body|
      from = meta.username
      puts "Sending from #{from} to #{to}:\n#{body}"
      smtp.send_message(body.to_s, from, to)
    })
    puts "SMTP closing."
  end
  puts "SMTP closed."
end

#send_email(to, body = nil) ⇒ Object

Raises:

  • (ArgumentError)


102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/gmail.rb', line 102

def send_email(to, body=nil)
  meta = class << self; self end
  if to.is_a?(MIME::Message)
    to.headers['from'] = meta.username
    to.headers['date'] = Time.now.strftime("%a, %d %b %Y %H:%M:%S %z")
    body = to.to_s
    to = to.to
  end
  raise ArgumentError, "Please supply (to, body) to Gmail#send_email" if body.nil?
  open_smtp do |smtp|
    smtp.call to, body
  end
end