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
-
.authorized(oauth_token) ⇒ Object
See these documents for details: oauth.rubyforge.org/ code.google.com/apis/accounts/docs/OAuth.html.
-
.login(mail, password) ⇒ Object
Authenticates with given
mailandpassword, and returns GoogleSpreadsheet::Session if succeeds. -
.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token") ⇒ Object
Restores GoogleSpreadsheet::Session from
pathand returns it.
Class Method Details
.authorized(oauth_token) ⇒ Object
See these documents for details: oauth.rubyforge.org/ code.google.com/apis/accounts/docs/OAuth.html
42 43 44 |
# File 'lib/google_spreadsheet.rb', line 42 def self.(oauth_token) return Session.(oauth_token) end |
.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.
20 21 22 |
# File 'lib/google_spreadsheet.rb', line 20 def self.login(mail, password) return Session.login(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 Highline library: rubyforge.org/projects/highline/
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/google_spreadsheet.rb', line 51 def self.saved_session(path = ENV["HOME"] + "/.ruby_google_spreadsheet.token") tokens = {} if File.exist?(path) open(path) do |f| for auth in [:wise, :writely] line = f.gets() tokens[auth] = line && line.chomp() end end end session = Session.new(tokens) session.on_auth_fail = proc() do begin require "highline" 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.login(mail, password) open(path, "w", 0600) do |f| f.puts(session.auth_token(:wise)) f.puts(session.auth_token(:writely)) end true end if !session.auth_token session.on_auth_fail.call() end return session end |