Class: Gmail::Client
Defined Under Namespace
Classes: AuthorizationError, ConnectionError, DeliveryError
Constant Summary collapse
- 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
-
#email ⇒ Object
readonly
Returns the value of attribute email.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#secret ⇒ Object
readonly
Returns the value of attribute secret.
-
#token ⇒ Object
readonly
Returns the value of attribute token.
Instance Method Summary collapse
-
#compose(mail = nil, &block) ⇒ Object
(also: #message)
Compose new e-mail.
-
#connect(raise_errors = false) ⇒ Object
Connect to gmail service.
-
#connect! ⇒ Object
This version of connect will raise error on failure…
-
#connection ⇒ Object
(also: #conn)
Return current connection.
-
#deliver(mail = nil, raise_errors = false, &block) ⇒ Object
Compose (optionaly) and send given email.
-
#deliver!(mail = nil, &block) ⇒ Object
This version of deliver will raise error on failure…
-
#inbox ⇒ Object
Alias for
mailbox("INBOX")
. -
#initialize(email, token, secret, consumer_key, consumer_secret, options = {}) ⇒ Client
constructor
A new instance of Client.
- #inspect ⇒ Object
-
#labels ⇒ Object
Return labels object, which helps you with managing your GMail labels.
-
#logged_in? ⇒ Boolean
(also: #signed_in?)
Returns
true
when you are logged in to specified account. -
#login(raise_errors = false) ⇒ Object
(also: #sign_in)
Login to specified account.
-
#login! ⇒ Object
(also: #sign_in!)
This version of login will raise error on failure…
-
#logout ⇒ Object
(also: #sign_out)
Logout from GMail service.
-
#mail_domain ⇒ Object
def fill_username(username) username =~ /@/ ? username : “#[email protected]” end.
-
#mailbox(name, &block) ⇒ Object
(also: #in_mailbox, #in_label, #label)
Do something with given mailbox or within it context.
- #mailboxes ⇒ Object
Constructor Details
#initialize(email, token, secret, consumer_key, consumer_secret, options = {}) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/gmail/client.rb', line 23 def initialize(email, token, secret, consumer_key, consumer_secret, ={}) defaults = {} @email = email @username = email @token = token @secret = secret @consumer_key = consumer_key @consumer_secret = consumer_secret @options = defaults.merge() end |
Instance Attribute Details
#email ⇒ Object (readonly)
Returns the value of attribute email.
18 19 20 |
# File 'lib/gmail/client.rb', line 18 def email @email end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
21 22 23 |
# File 'lib/gmail/client.rb', line 21 def @options end |
#secret ⇒ Object (readonly)
Returns the value of attribute secret.
20 21 22 |
# File 'lib/gmail/client.rb', line 20 def secret @secret end |
#token ⇒ Object (readonly)
Returns the value of attribute token.
19 20 21 |
# File 'lib/gmail/client.rb', line 19 def token @token end |
Instance Method Details
#compose(mail = nil, &block) ⇒ Object Also known as: message
Compose new e-mail.
Examples
mail = gmail.compose
mail.from "[email protected]"
mail.to "[email protected]"
… or block style:
mail = gmail.compose do
from "[email protected]"
to "[email protected]"
subject "Hello!"
body "Hello my friend! long time..."
end
Now you can deliver your mail:
gmail.deliver(mail)
113 114 115 116 117 118 119 120 121 122 |
# File 'lib/gmail/client.rb', line 113 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 = @email unless mail.from mail end |
#connect(raise_errors = false) ⇒ Object
Connect to gmail service.
35 36 37 38 39 |
# File 'lib/gmail/client.rb', line 35 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 |
#connect! ⇒ Object
This version of connect will raise error on failure…
42 43 44 |
# File 'lib/gmail/client.rb', line 42 def connect! connect(true) end |
#connection ⇒ Object Also known as: conn
Return current connection. Log in automaticaly to specified account if it is necessary.
48 49 50 51 |
# File 'lib/gmail/client.rb', line 48 def connection login and at_exit { logout } unless logged_in? @imap end |
#deliver(mail = nil, raise_errors = false, &block) ⇒ Object
Compose (optionaly) and send given email.
Examples
gmail.deliver do
to "[email protected]"
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)
142 143 144 145 146 147 |
# File 'lib/gmail/client.rb', line 142 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 |
#deliver!(mail = nil, &block) ⇒ Object
This version of deliver will raise error on failure…
150 151 152 |
# File 'lib/gmail/client.rb', line 150 def deliver!(mail=nil, &block) deliver(mail, true, &block) end |
#inbox ⇒ Object
Alias for mailbox("INBOX")
. See Gmail::Client#mailbox
for details.
188 189 190 |
# File 'lib/gmail/client.rb', line 188 def inbox mailbox("INBOX") end |
#inspect ⇒ Object
196 197 198 |
# File 'lib/gmail/client.rb', line 196 def inspect "#<Gmail::Client#{'0x%04x' % (object_id << 1)} (#{@email}) #{'dis' if !logged_in?}connected>" end |
#labels ⇒ Object
Return labels object, which helps you with managing your GMail labels. See Gmail::Labels
for details.
89 90 91 |
# File 'lib/gmail/client.rb', line 89 def labels @labels ||= Labels.new(conn) end |
#logged_in? ⇒ Boolean Also known as: signed_in?
Returns true
when you are logged in to specified account.
74 75 76 |
# File 'lib/gmail/client.rb', line 74 def logged_in? !!@logged_in end |
#login(raise_errors = false) ⇒ Object Also known as: sign_in
Login to specified account.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/gmail/client.rb', line 55 def login(raise_errors=false) @imap and @logged_in = (login = @imap.authenticate('XOAUTH', @email, :consumer_key => @consumer_key, :consumer_secret => @consumer_secret, :token => @token, :token_secret => @secret )) && login.name == 'OK' rescue Net::IMAP::NoResponseError raise_errors and raise AuthorizationError, "Couldn't login to given GMail account" end |
#login! ⇒ Object Also known as: sign_in!
This version of login will raise error on failure…
68 69 70 |
# File 'lib/gmail/client.rb', line 68 def login! login(true) end |
#logout ⇒ Object Also known as: sign_out
Logout from GMail service.
80 81 82 83 84 |
# File 'lib/gmail/client.rb', line 80 def logout @imap && logged_in? and @imap.logout ensure @logged_in = false end |
#mail_domain ⇒ Object
def fill_username(username)
username =~ /@/ ? username : "#{username}@gmail.com"
end
204 205 206 |
# File 'lib/gmail/client.rb', line 204 def mail_domain @email.split('@')[0] end |
#mailbox(name, &block) ⇒ Object 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
169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/gmail/client.rb', line 169 def mailbox(name, &block) 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 mailbox end |
#mailboxes ⇒ Object
192 193 194 |
# File 'lib/gmail/client.rb', line 192 def mailboxes @mailboxes ||= {} end |