Class: Gmail
- 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
- #create_label(name) ⇒ Object
-
#imap ⇒ Object
Accessor for @imap, but ensures that it’s logged in first.
- #in_mailbox(mailbox, &block) ⇒ Object (also: #in_label)
-
#inbox ⇒ Object
Accessors for Gmail things.
-
#initialize(username, password) ⇒ Gmail
constructor
A new instance of Gmail.
-
#labels ⇒ Object
List the available labels.
-
#logout ⇒ Object
Log out of gmail.
-
#mailbox(name) ⇒ Object
Accessors for IMAP things.
- #new_message ⇒ Object
- #open_smtp(&block) ⇒ Object
- #send_email(to, body = nil) ⇒ Object
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. = class << self class << self attr_accessor :username, :password end self end .username = username =~ /@/ ? username : username + '@gmail.com' .password = password @imap = Net::IMAP.new('imap.gmail.com',993,true,nil,false) if block_given? @imap.login(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 |
#imap ⇒ Object
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? = class << self; self end @imap.login(.username, .password) at_exit { logout } # Set up auto-logout for later. end @imap end |
#in_mailbox(mailbox, &block) ⇒ Object Also known as: in_label
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 |
#inbox ⇒ Object
Accessors for Gmail things
35 36 37 |
# File 'lib/gmail.rb', line 35 def inbox mailbox('inbox') end |
#labels ⇒ Object
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 |
#logout ⇒ Object
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 |
#open_smtp(&block) ⇒ Object
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? = class << self; self end puts "Opening SMTP..." Net::SMTP.start('smtp.gmail.com', 587, 'localhost.localdomain', .username, .password, 'plain', true) do |smtp| puts "SMTP open." block.call(lambda {|to, body| from = .username puts "Sending from #{from} to #{to}:\n#{body}" smtp.(body.to_s, from, to) }) puts "SMTP closing." end puts "SMTP closed." end |
#send_email(to, body = nil) ⇒ Object
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) = class << self; self end if to.is_a?(MIME::Message) to.headers['from'] = .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 |