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 Attribute Summary collapse
-
#peek ⇒ Object
don’t mark emails as read on the server when downloading them.
Instance Method Summary collapse
- #create_label(name) ⇒ Object
- #deliver(mail = nil, &block) ⇒ Object
- #destroy_label(name) ⇒ Object
-
#disconnect ⇒ Object
Shutdown socket and disconnect.
- #find_label_by_attribute(attribute) ⇒ Object
-
#generate_message(&block) ⇒ Object
MAKING EMAILS.
-
#imap ⇒ Object
Accessor for @imap, but ensures that it’s logged in first.
- #in_mailbox(mailbox, &block) ⇒ Object (also: #in_label)
-
#inbox ⇒ Object
READING EMAILS.
-
#initialize(username, password) ⇒ Gmail
constructor
Gmail.new(username, password).
-
#inspect ⇒ Object
Other…
-
#label(name) ⇒ Object
(also: #mailbox)
gmail.label(name).
-
#labels ⇒ Object
List the available labels.
- #logged_in? ⇒ Boolean
-
#login ⇒ Object
LOGIN.
-
#logout ⇒ Object
Log out of gmail.
- #mailbox_list ⇒ Object
- #rename_label(oldname, newname) ⇒ Object
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 .username = username =~ /@/ ? username : username + '@gmail.com' .password = password @imap = Net::IMAP.new('imap.gmail.com',993,true,nil,false) if block_given? login # This is here intentionally. Normally, we get auto logged-in when first needed. yield self logout end end |
Instance Attribute Details
#peek ⇒ Object
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 = .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 |
#disconnect ⇒ Object
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 (&block) require 'net/smtp' require 'smtp_tls' require 'mail' mail = Mail.new(&block) mail.delivery_method(*smtp_settings) mail end |
#imap ⇒ Object
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? login 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 |
#inbox ⇒ Object
READING EMAILS
gmail.inbox
gmail.label('News')
37 38 39 |
# File 'lib/gmail.rb', line 37 def inbox in_label('inbox') end |
#inspect ⇒ Object
Other…
158 159 160 |
# File 'lib/gmail.rb', line 158 def inspect "#<Gmail:#{'0x%x' % (object_id << 1)} (#{.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 |
#labels ⇒ Object
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
117 118 119 |
# File 'lib/gmail.rb', line 117 def logged_in? !!@logged_in end |
#login ⇒ Object
LOGIN
113 114 115 116 |
# File 'lib/gmail.rb', line 113 def login res = @imap.login(.username, .password) @logged_in = true if res && res.name == 'OK' end |
#logout ⇒ Object
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_list ⇒ Object
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 |