Module: GoogleAPI::ActiveRecordInclusions::ClassMethods

Defined in:
lib/google-api/active_record_inclusions.rb

Instance Method Summary collapse

Instance Method Details

#oauthableObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/google-api/active_record_inclusions.rb', line 14

def oauthable
  define_method :oauth_hash do
    {
      access_token: GoogleAPI::Encrypter.decrypt!(oauth_access_token),
      refresh_token: GoogleAPI::Encrypter.decrypt!(oauth_refresh_token),
      expires_at: oauth_access_token_expires_at
    }
  end

  # This method is used both within the GoogleAPI and can be used outside it in your own app to update
  # the OAuth2 values in the database. Refresh token doesn't need to be passed, and any other attributes
  # that you want to update on the object can be passed as a final parameter.
  define_method :update_oauth! do |access_token, refresh_token = nil, additional_attrs = {}|
    attrs = {
      oauth_access_token: GoogleAPI::Encrypter.encrypt!(access_token),
      oauth_access_token_expires_at: 59.minutes.from_now # it's actually an hour from now, but just to make sure we don't overlap at all, let's set it to 59 minutes
    }.merge(additional_attrs)
    attrs[:oauth_refresh_token] = GoogleAPI::Encrypter.encrypt!(refresh_token) if refresh_token
    update_attributes(attrs)
  end

  define_method :google do
    GoogleAPI::Client.new(self)
  end
end