Sha256: 3ca34c6eb6b6d514b7f82d9c3802ab39a0e73f0798031d5986ce678d38ad5e3b

Contents?: true

Size: 1.88 KB

Versions: 1

Compression:

Stored size: 1.88 KB

Contents

class GoogleDrive::PersistentSession
  extend Forwardable
  include GoogleDrive::CredentialStorage

  def_delegator :@highline, :ask

  THREAD_KEY = "#{self.to_s}::THREAD_KEY"

  def self.login
    self.new
  end

  def initialize
    @highline = HighLine.new
    ensure_login
  end

  private

  def ensure_login
    store_credential unless credential_stored?
    credential = storage.authorize

    if credential.expired?
      refresh(credential)
    end

    unless session
      login_with_oauth(credential.access_token)
    end
  end

  def store_credential
    client_id = ask('Enter CLIENT ID: ')
    client_secret = ask('Enter CLIENT SECRET: ') {|q| q.echo = false }
    puts # line break

    credential =  Google::Auth::UserRefreshCredentials.new(
      :client_id => client_id,
      :client_secret => client_secret,
      :scope => [
        'https://www.googleapis.com/auth/drive',
        'https://spreadsheets.google.com/feeds/'
      ],
      :redirect_uri => 'urn:ietf:wg:oauth:2.0:oob',
      :grant_type => 'authorization_code'
    )

    fetch_access_token(credential)
    storage.write_credentials(credential)
  end

  def fetch_access_token(credential)
    message =  "1. Open this page:\n%s\n\n" % credential.authorization_uri
    message << "2. Enter the authorization code shown in the page: "
    credential.code = ask(message)

    credential.fetch_access_token!
    storage.write_credentials(credential)
    login_with_oauth(credential.access_token)
  end

  def refresh(credential)
    credential.refresh!
    storage.write_credentials(credential)
    login_with_oauth(credential.access_token)
  end

  def session
    Thread.current[THREAD_KEY]
  end

  def login_with_oauth(access_token)
    Thread.current[THREAD_KEY] = GoogleDrive.login_with_oauth(access_token)
  end

  def method_missing(method_name, *args , &block)
    ensure_login
    session.send(method_name, *args, &block)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
google_drive-persistent_session-0.2.0 lib/google_drive/persistent_session/persistent_session.rb