class GoogleOAuth::Client

Attributes

access_token[RW]
authentication[RW]
expires_at[RW]
google_client[RW]
refresh_token[RW]

Public Class Methods

new(options = {}) click to toggle source
# File lib/google_oauth/client.rb, line 10
def initialize(options = {})
  [:refresh_token, :access_token, :expires_at].each do |attr|
    instance_variable_set("@#{attr}".to_sym, options[:authentication].send(attr))
  end

  @authentication = options[:authentication]

  setup_google_client!
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source

Delegate to Google OAuth Client

# File lib/google_oauth/client.rb, line 21
def method_missing(method, *args, &block)
  google_client.send(method, *args, &block)
end

Private Instance Methods

oauth_expired?() click to toggle source
# File lib/google_oauth/client.rb, line 52
def oauth_expired?
  expires_at < Time.now
end
service() click to toggle source
# File lib/google_oauth/client.rb, line 56
def service
  @service ||= google_client.discovered_api('calendar', 'v3')
end
setup_google_client!() click to toggle source
# File lib/google_oauth/client.rb, line 27
def setup_google_client!
  @google_client = Google::APIClient.new
  @google_client.authorization.client_id      = GoogleOAuth.client_id
  @google_client.authorization.client_secret  = GoogleOAuth.client_secret
  @google_client.authorization.scope          = GoogleOAuth.scope
  @google_client.authorization.refresh_token  = refresh_token
  @google_client.authorization.access_token   = access_token

  puts "@google_client: #{@google_client.authorization.inspect}"

  # Google OAuth API is a big liar!
  # Their API does not seem to respect the expiration time they specify
  # in their initial response
  if @google_client.authorization.refresh_token && oauth_expired?
    new_tokens = @google_client.authorization.fetch_access_token!

    expires_at = 10.minutes.from_now
    if authentication
      authentication.access_token   = new_tokens["access_token"]
      authentication.expires_at     = 10.minutes.from_now
      authentication.save
    end
  end
end