Class: Gmail::Client::Base
Constant Summary
- GMAIL_IMAP_HOST =
GMail IMAP defaults
'imap.gmail.com'- GMAIL_IMAP_PORT =
993- GMAIL_SMTP_HOST =
GMail SMTP defaults
"smtp.gmail.com"- GMAIL_SMTP_PORT =
587
Instance Attribute Summary (collapse)
-
- (Object) options
readonly
Returns the value of attribute options.
-
- (Object) username
readonly
Returns the value of attribute username.
Instance Method Summary (collapse)
-
- (Object) compose(mail = nil, &block)
(also: #message)
Compose new e-mail.
-
- (Object) connect(raise_errors = false)
Connect to gmail service.
-
- (Object) connect!
This version of connect will raise error on failure...
-
- (Object) connection
(also: #conn)
Return current connection.
-
- (Object) deliver(mail = nil, raise_errors = false, &block)
Compose (optionaly) and send given email.
-
- (Object) deliver!(mail = nil, &block)
This version of deliver will raise error on failure...
- - (Object) fill_username(username)
-
- (Object) inbox
Alias for mailbox("INBOX").
-
- (Base) initialize(username, options = {})
constructor
A new instance of Base.
- - (Object) inspect
-
- (Object) labels
Return labels object, which helps you with managing your GMail labels.
-
- (Boolean) logged_in?
(also: #signed_in?)
Returns true when you are logged in to specified account.
-
- (Object) login(*args)
(also: #sign_in)
Login to specified account.
-
- (Object) login!
(also: #sign_in!)
This version of login will raise error on failure...
-
- (Object) logout
(also: #sign_out)
Logout from GMail service.
- - (Object) mail_domain
-
- (Object) mailbox(name, &block)
(also: #in_mailbox, #in_label, #label)
Do something with given mailbox or within it context.
- - (Object) mailboxes
Constructor Details
- (Base) initialize(username, options = {})
A new instance of Base
17 18 19 20 21 22 |
# File 'lib/gmail/client/base.rb', line 17 def initialize(username, ={}) defaults = {} @username = fill_username(username) @options = defaults.merge() @mailbox_mutex = Mutex.new end |
Instance Attribute Details
- (Object) options (readonly)
Returns the value of attribute options
15 16 17 |
# File 'lib/gmail/client/base.rb', line 15 def @options end |
- (Object) username (readonly)
Returns the value of attribute username
14 15 16 |
# File 'lib/gmail/client/base.rb', line 14 def username @username end |
Instance Method Details
- (Object) compose(mail = nil, &block) Also known as: message
Compose new e-mail.
Examples
mail = gmail.compose
mail.from "test@gmail.org"
mail.to "friend@gmail.com"
... or block style:
mail = gmail.compose do
from "test@gmail.org"
to "friend@gmail.com"
subject "Hello!"
body "Hello my friend! long time..."
end
Now you can deliver your mail:
gmail.deliver(mail)
96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/gmail/client/base.rb', line 96 def compose(mail=nil, &block) if block_given? mail = Mail.new(&block) elsif !mail mail = Mail.new end mail.delivery_method(*smtp_settings) mail.from = username unless mail.from mail end |
- (Object) connect(raise_errors = false)
Connect to gmail service.
25 26 27 28 29 |
# File 'lib/gmail/client/base.rb', line 25 def connect(raise_errors=false) @imap = Net::IMAP.new(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, true, nil, false) rescue SocketError raise_errors and raise ConnectionError, "Couldn't establish connection with GMail IMAP service" end |
- (Object) connect!
This version of connect will raise error on failure...
32 33 34 |
# File 'lib/gmail/client/base.rb', line 32 def connect! connect(true) end |
- (Object) connection Also known as: conn
Return current connection. Log in automaticaly to specified account if it is necessary.
38 39 40 41 |
# File 'lib/gmail/client/base.rb', line 38 def connection login and at_exit { logout } unless logged_in? @imap end |
- (Object) deliver(mail = nil, raise_errors = false, &block)
Compose (optionaly) and send given email.
Examples
gmail.deliver do
to "friend@gmail.com"
subject "Hello friend!"
body "Hi! How are you?"
end
... or with already created message:
mail = Mail.new { ... }
gmail.deliver(mail)
mail = gmail.compose { ... }
gmail.deliver(mail)
126 127 128 129 130 131 |
# File 'lib/gmail/client/base.rb', line 126 def deliver(mail=nil, raise_errors=false, &block) mail = compose(mail, &block) if block_given? mail.deliver! rescue Object => ex raise_errors and raise DeliveryError, "Couldn't deliver email: #{ex.to_s}" end |
- (Object) deliver!(mail = nil, &block)
This version of deliver will raise error on failure...
134 135 136 |
# File 'lib/gmail/client/base.rb', line 134 def deliver!(mail=nil, &block) deliver(mail, true, &block) end |
- (Object) fill_username(username)
188 189 190 |
# File 'lib/gmail/client/base.rb', line 188 def fill_username(username) username =~ /@/ ? username : "#{username}@gmail.com" end |
- (Object) inbox
Alias for mailbox("INBOX"). See Gmail::Client#mailbox for details.
176 177 178 |
# File 'lib/gmail/client/base.rb', line 176 def inbox mailbox("INBOX") end |
- (Object) inspect
184 185 186 |
# File 'lib/gmail/client/base.rb', line 184 def inspect "#<Gmail::Client#{'0x%04x' % (object_id << 1)} (#{username}) #{'dis' if !logged_in?}connected>" end |
- (Object) labels
Return labels object, which helps you with managing your GMail labels. See Gmail::Labels for details.
72 73 74 |
# File 'lib/gmail/client/base.rb', line 72 def labels @labels ||= Labels.new(conn) end |
- (Boolean) logged_in? Also known as: signed_in?
Returns true when you are logged in to specified account.
57 58 59 |
# File 'lib/gmail/client/base.rb', line 57 def logged_in? !!@logged_in end |
- (Object) login(*args) Also known as: sign_in
Login to specified account.
45 46 47 |
# File 'lib/gmail/client/base.rb', line 45 def login(*args) raise NotImplementedError, "The `#{self.class.name}#login` method is not implemented." end |
- (Object) login! Also known as: sign_in!
This version of login will raise error on failure...
51 52 53 |
# File 'lib/gmail/client/base.rb', line 51 def login! login(true) end |
- (Object) logout Also known as: sign_out
Logout from GMail service.
63 64 65 66 67 |
# File 'lib/gmail/client/base.rb', line 63 def logout @imap && logged_in? and @imap.logout ensure @logged_in = false end |
- (Object) mail_domain
192 193 194 |
# File 'lib/gmail/client/base.rb', line 192 def mail_domain username.split('@')[0] end |
- (Object) mailbox(name, &block) Also known as: in_mailbox, in_label, label
Do something with given mailbox or within it context.
Examples
mailbox = gmail.mailbox("INBOX")
mailbox.emails(:all)
mailbox.count(:unread, :before => Time.now-(20*24*3600))
... or block style:
gmail.label("Work") do |mailbox|
mailbox.emails(:unread)
mailbox.count(:all)
...
end
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/gmail/client/base.rb', line 153 def mailbox(name, &block) @mailbox_mutex.synchronize do name = Net::IMAP.encode_utf7(name.to_s) mailbox = (mailboxes[name] ||= Mailbox.new(self, name)) switch_to_mailbox(name) if @current_mailbox != name if block_given? mailbox_stack << @current_mailbox result = block.arity == 1 ? block.call(mailbox) : block.call mailbox_stack.pop switch_to_mailbox(mailbox_stack.last) return result end return mailbox end end |
- (Object) mailboxes
180 181 182 |
# File 'lib/gmail/client/base.rb', line 180 def mailboxes @mailboxes ||= {} end |