Class: Gmail::Client::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/gmail/client/base.rb

Direct Known Subclasses

Plain, XOAuth, XOAuth2

Constant Summary collapse

GMAIL_IMAP_HOST =

Gmail IMAP defaults

'imap.gmail.com'.freeze
GMAIL_IMAP_PORT =
993
GMAIL_SMTP_HOST =

Gmail SMTP defaults

"smtp.gmail.com".freeze
GMAIL_SMTP_PORT =
587

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, options = {}) ⇒ Base

Returns a new instance of Base.



15
16
17
18
19
20
# File 'lib/gmail/client/base.rb', line 15

def initialize(username, options = {})
  defaults       = {}
  @username      = fill_username(username)
  @options       = defaults.merge(options)
  @mailbox_mutex = Mutex.new
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/gmail/client/base.rb', line 13

def options
  @options
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/gmail/client/base.rb', line 12

def username
  @username
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)


101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gmail/client/base.rb', line 101

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

#connect(raise_errors = false) ⇒ Object

Connect to gmail service.



23
24
25
26
27
28
29
# File 'lib/gmail/client/base.rb', line 23

def connect(raise_errors = false)
  @imap = Net::IMAP.new(GMAIL_IMAP_HOST, GMAIL_IMAP_PORT, true, nil, false)
  Gmail::ImapExtensions.patch_net_imap_response_parser
  @imap
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…



32
33
34
# File 'lib/gmail/client/base.rb', line 32

def connect!
  connect(true)
end

#connectionObject 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
   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)


131
132
133
134
135
136
# File 'lib/gmail/client/base.rb', line 131

def deliver(mail = nil, raise_errors = false, &block)
  mail = compose(mail, &block)
  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…



139
140
141
# File 'lib/gmail/client/base.rb', line 139

def deliver!(mail = nil, &block)
  deliver(mail, true, &block)
end

#disconnectObject

Disconnect from Gmail service.



71
72
73
# File 'lib/gmail/client/base.rb', line 71

def disconnect
  @imap && @imap.disconnect
end

#fill_username(username) ⇒ Object



210
211
212
# File 'lib/gmail/client/base.rb', line 210

def fill_username(username)
  username =~ /@/ ? username : "#{username}@gmail.com"
end

#find(rfc822msgid) ⇒ Object

Functionality like rails #find method support.google.com/mail/answer/7190?hl=en Messages with a certain message-id header Rfc822msgid: Example: rfc822msgid:[email protected]



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/gmail/client/base.rb', line 190

def find(rfc822msgid)
  message = :message_before_built

  mailbox(:all) do |mailbox|
    uid = conn.uid_search(['X-GM-RAW', "rfc822msgid:#{rfc822msgid.to_s.strip}"]).first
    raise EmailNotFound, "Can't find message with ID #{rfc822msgid}" unless uid
    message = Message.new(mailbox, uid)
  end

  message
end

#inboxObject

Alias for mailbox("INBOX"). See Gmail::Mailbox for details.



181
182
183
# File 'lib/gmail/client/base.rb', line 181

def inbox
  mailbox("INBOX")
end

#inspectObject



206
207
208
# File 'lib/gmail/client/base.rb', line 206

def inspect
  "#<Gmail::Client#{'0x%04x' % (object_id << 1)} (#{username}) #{'dis' if !logged_in?}connected>"
end

#labelsObject

Return labels object, which helps you with managing your Gmail labels. See Gmail::Labels for details.



77
78
79
# File 'lib/gmail/client/base.rb', line 77

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.

Returns:

  • (Boolean)


57
58
59
# File 'lib/gmail/client/base.rb', line 57

def logged_in?
  !!@logged_in
end

#login(*args) ⇒ Object Also known as: sign_in

Login to specified account.

Raises:

  • (NotImplementedError)


45
46
47
# File 'lib/gmail/client/base.rb', line 45

def (*args)
  raise NotImplementedError, "The `#{self.class.name}#login` method is not implemented."
end

#login!Object 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!
  (true)
end

#logoutObject 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

#mail_domainObject



214
215
216
# File 'lib/gmail/client/base.rb', line 214

def mail_domain
  username.split('@').last
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


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/gmail/client/base.rb', line 158

def mailbox(name, &block)
  @mailbox_mutex.synchronize do
    name = labels.localize(name)
    mailbox = (mailboxes[name] ||= Mailbox.new(self, name))
    switch_to_mailbox(mailbox) if @current_mailbox != mailbox

    if block_given?
      mailbox_stack << @current_mailbox
      result = block.arity == 1 ? yield(mailbox) : yield
      mailbox_stack.pop
      switch_to_mailbox(mailbox_stack.last)
      return result
    end

    return mailbox
  end
end

#mailboxesObject



202
203
204
# File 'lib/gmail/client/base.rb', line 202

def mailboxes
  @mailboxes ||= {}
end