Module: GoogleSpreadsheet

Defined in:
lib/google_spreadsheet.rb

Defined Under Namespace

Modules: Util Classes: AuthenticationError, Error, Record, Session, Spreadsheet, Table, Worksheet

Class Method Summary collapse

Class Method Details

.login(mail, password) ⇒ Object

Authenticates with given mail and password, and returns GoogleSpreadsheet::Session if succeeds. Raises GoogleSpreadsheet::AuthenticationError if fails. Google Apps account is supported.



31
32
33
# File 'lib/google_spreadsheet.rb', line 31

def self.(mail, password)
  return Session.(mail, password)
end

.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token") ⇒ Object

Restores GoogleSpreadsheet::Session from path and returns it. If path doesn’t exist or authentication has failed, prompts mail and password on console, authenticates with them, stores the session to path and returns it.

This method requires Ruby/Password library: www.caliban.org/ruby/ruby-password.shtml



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/google_spreadsheet.rb', line 40

def self.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token")
  session = Session.new(File.exist?(path) ? File.read(path) : nil)
  session.on_auth_fail = proc() do
    begin
      require "highline2"
    rescue LoadError
      raise(LoadError,
        "GoogleSpreadsheet.saved_session requires Highline library.\n" +
        "Run\n" +
        "  \$ sudo gem install highline\n" +
        "to install it.")
    end
    highline = HighLine.new()
    mail = highline.ask("Mail: ")
    password = highline.ask("Password: "){ |q| q.echo = false }
    session.(mail, password)
    open(path, "w", 0600){ |f| f.write(session.auth_token) }
    true
  end
  if !session.auth_token
    session.on_auth_fail.call()
  end
  return session
end