Class: Gmail::Client::XOAuth

Inherits:
Base show all
Defined in:
lib/gmail/client/xoauth.rb

Constant Summary

Constants inherited from Base

Base::GMAIL_IMAP_HOST, Base::GMAIL_IMAP_PORT, Base::GMAIL_SMTP_HOST, Base::GMAIL_SMTP_PORT

Instance Attribute Summary collapse

Attributes inherited from Base

#options, #username

Instance Method Summary collapse

Methods inherited from Base

#compose, #connect, #connect!, #connection, #deliver, #deliver!, #disconnect, #fill_username, #inbox, #inspect, #labels, #logged_in?, #login!, #logout, #mail_domain, #mailbox, #mailboxes

Constructor Details

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

Returns a new instance of XOAuth


11
12
13
14
15
16
17
18
# File 'lib/gmail/client/xoauth.rb', line 11

def initialize(username, options = {})
  @token           = options.delete(:token)
  @secret          = options.delete(:secret)
  @consumer_key    = options.delete(:consumer_key)
  @consumer_secret = options.delete(:consumer_secret)

  super(username, options)
end

Instance Attribute Details

#consumer_keyObject (readonly)

Returns the value of attribute consumer_key


8
9
10
# File 'lib/gmail/client/xoauth.rb', line 8

def consumer_key
  @consumer_key
end

#consumer_secretObject (readonly)

Returns the value of attribute consumer_secret


9
10
11
# File 'lib/gmail/client/xoauth.rb', line 9

def consumer_secret
  @consumer_secret
end

#secretObject (readonly)

Returns the value of attribute secret


7
8
9
# File 'lib/gmail/client/xoauth.rb', line 7

def secret
  @secret
end

#tokenObject (readonly)

Returns the value of attribute token


6
7
8
# File 'lib/gmail/client/xoauth.rb', line 6

def token
  @token
end

Instance Method Details

#access_tokenObject


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/gmail/client/xoauth.rb', line 35

def access_token
  consumer_options = {
    :site               => "https://www.google.com",
    :request_token_path => "/accounts/OAuthGetRequestToken",
    :authorize_path     => "/accounts/OAuthAuthorizeToken",
    :access_token_path  => "/accounts/OAuthGetAccessToken"
  }
  consumer = OAuth::Consumer.new(consumer_key, consumer_secret, consumer_options)
  @access_token ||= OAuth::AccessToken.new(consumer, token, secret)
  @access_token
end

#login(raise_errors = false) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gmail/client/xoauth.rb', line 20

def (raise_errors = false)
  @imap and @logged_in = ( = @imap.authenticate('XOAUTH', username,
    :consumer_key    => consumer_key,
    :consumer_secret => consumer_secret,
    :token           => token,
    :token_secret    => secret
  )) && .name == 'OK'
rescue Net::IMAP::NoResponseError => e
  if raise_errors
    message = "Couldn't login to given Gmail account: #{username}"
    message += " (#{e.response.data.text.strip})"
    raise(AuthorizationError.new(e.response), message, e.backtrace)
  end
end

#smtp_settingsObject


47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gmail/client/xoauth.rb', line 47

def smtp_settings
  [:smtp, {
     :address => GMAIL_SMTP_HOST,
     :port => GMAIL_SMTP_PORT,
     :domain => mail_domain,
     :user_name => username,
     :password => {
       :consumer_key    => consumer_key,
       :consumer_secret => consumer_secret,
       :token           => token,
       :token_secret    => secret,
       :access_token    => access_token
     },
     :authentication => :xoauth,
     :enable_starttls_auto => true
   }]
end