class GoogleSpreadsheet::Session

Use GoogleSpreadsheet.login or GoogleSpreadsheet.saved_session to get GoogleSpreadsheet::Session object.

Attributes

on_auth_fail[RW]

Proc or Method called when authentication has failed. When this function returns true, it tries again.

Public Class Methods

login(mail, password, proxy = nil) click to toggle source

The same as GoogleSpreadsheet.login.

# File lib/google_spreadsheet/session.rb, line 30
def self.login(mail, password, proxy = nil)
  session = Session.new(nil, ClientLoginFetcher.new({}, proxy))
  session.login(mail, password)
  return session
end
login_with_oauth(oauth_token) click to toggle source

The same as GoogleSpreadsheet.login_with_oauth.

# File lib/google_spreadsheet/session.rb, line 37
def self.login_with_oauth(oauth_token)
  case oauth_token
    when OAuth::AccessToken
      fetcher = OAuth1Fetcher.new(oauth_token)
    when OAuth2::AccessToken
      fetcher = OAuth2Fetcher.new(oauth_token)
    else
      raise(GoogleSpreadsheet::Error,
          "oauth_token is neither OAuth::Token nor OAuth2::Token: %p" % oauth_token)
  end
  return Session.new(nil, fetcher)
end
new(auth_tokens = nil, fetcher = nil, proxy = nil) click to toggle source

DEPRECATED: Use GoogleSpreadsheet.restore_session instead.

# File lib/google_spreadsheet/session.rb, line 61
def initialize(auth_tokens = nil, fetcher = nil, proxy = nil)
  if fetcher
    @fetcher = fetcher
  else
    @fetcher = ClientLoginFetcher.new(auth_tokens || {}, proxy)
  end
end
new_dummy() click to toggle source

Creates a dummy GoogleSpreadsheet::Session object for testing.

# File lib/google_spreadsheet/session.rb, line 56
def self.new_dummy()
  return Session.new(nil, Object.new())
end
restore_session(auth_tokens, proxy = nil) click to toggle source

The same as GoogleSpreadsheet.restore_session.

# File lib/google_spreadsheet/session.rb, line 51
def self.restore_session(auth_tokens, proxy = nil)
  return Session.new(auth_tokens, nil, proxy)
end

Public Instance Methods

auth_token(auth = :wise) click to toggle source

Authentication token.

# File lib/google_spreadsheet/session.rb, line 99
def auth_token(auth = :wise)
  return self.auth_tokens[auth]
end
auth_tokens() click to toggle source

Authentication tokens.

# File lib/google_spreadsheet/session.rb, line 89
def auth_tokens
  if !@fetcher.is_a?(ClientLoginFetcher)
    raise(GoogleSpreadsheet::Error,
        "Cannot call auth_tokens for session created by " +
        "login_with_oauth.")
  end
  return @fetcher.auth_tokens
end
collection_by_url(url) click to toggle source

Returns GoogleSpreadsheet::Collection with given url. You must specify either of:

  • URL of the page you get when you go to docs.google.com/ with your browser and open a collection

  • URL of collection (folder) feed

e.g.

session.collection_by_url(
  "https://docs.google.com/?pli=1&authuser=0#folders/" +
  "0B9GfDpQ2pBVUODNmOGE0NjIzMWU3ZC00NmUyLTk5NzEtYaFkZjY1MjAyxjMc")
session.collection_by_url(
  "http://docs.google.com/feeds/default/private/full/folder%3A" +
  "0B9GfDpQ2pBVUODNmOGE0NjIzMWU3ZC00NmUyLTk5NzEtYaFkZjY1MjAyxjMc")
# File lib/google_spreadsheet/session.rb, line 192
def collection_by_url(url)
  uri = URI.parse(url)
  if uri.host == "docs.google.com" && uri.fragment =~ %r^folders\/(.+)$/
    # Looks like a URL of human-readable collection page. Converts to collection feed URL.
    url = "https://docs.google.com/feeds/default/private/full/folder%3A#{$1}"
  end
  return Collection.new(self, url)
end
create_spreadsheet( title = "Untitled", feed_url = "https://docs.google.com/feeds/documents/private/full") click to toggle source

Creates new spreadsheet and returns the new GoogleSpreadsheet::Spreadsheet.

e.g.

session.create_spreadsheet("My new sheet")
# File lib/google_spreadsheet/session.rb, line 205
        def create_spreadsheet(
            title = "Untitled",
            feed_url = "https://docs.google.com/feeds/documents/private/full")
          
          xml = "            <atom:entry
                xmlns:atom="http://www.w3.org/2005/Atom"
                xmlns:docs="http://schemas.google.com/docs/2007">
              <atom:category
                  scheme="http://schemas.google.com/g/2005#kind"
                  term="http://schemas.google.com/docs/2007#spreadsheet"
                  label="spreadsheet"/>
              <atom:title>#{h(title)}</atom:title>
            </atom:entry>
"

          doc = request(:post, feed_url, :data => xml, :auth => :writely)
          ss_url = doc.css(
            "link[rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']")[0]["href"]
          return Spreadsheet.new(self, ss_url, title)
          
        end
inspect() click to toggle source
# File lib/google_spreadsheet/session.rb, line 259
def inspect
  return "#<%p:0x%x>" % [self.class, self.object_id]
end
login(mail, password) click to toggle source

Authenticates with given mail and password, and updates current session object if succeeds. Raises GoogleSpreadsheet::AuthenticationError if fails. Google Apps account is supported.

# File lib/google_spreadsheet/session.rb, line 72
def login(mail, password)
  if !@fetcher.is_a?(ClientLoginFetcher)
    raise(GoogleSpreadsheet::Error,
        "Cannot call login for session created by login_with_oauth.")
  end
  begin
    @fetcher.auth_tokens = {
      :wise => authenticate(mail, password, :wise),
      :writely => authenticate(mail, password, :writely),
    }
  rescue GoogleSpreadsheet::Error => ex
    return true if @on_auth_fail && @on_auth_fail.call()
    raise(AuthenticationError, "Authentication failed for #{mail}: #{ex.message}")
  end
end
spreadsheet_by_key(key) click to toggle source

Returns GoogleSpreadsheet::Spreadsheet with given key.

e.g.

# http://spreadsheets.google.com/ccc?key=pz7XtlQC-PYx-jrVMJErTcg&hl=ja
session.spreadsheet_by_key("pz7XtlQC-PYx-jrVMJErTcg")
# File lib/google_spreadsheet/session.rb, line 133
def spreadsheet_by_key(key)
  url = "https://spreadsheets.google.com/feeds/worksheets/#{key}/private/full"
  return Spreadsheet.new(self, url)
end
spreadsheet_by_title(title) click to toggle source

Returns GoogleSpreadsheet::Spreadsheet with given title. Returns nil if not found. If multiple spreadsheets with the title are found, returns one of them.

# File lib/google_spreadsheet/session.rb, line 164
def spreadsheet_by_title(title)
  return spreadsheets({"title" => title})[0]
end
spreadsheet_by_url(url) click to toggle source

Returns GoogleSpreadsheet::Spreadsheet with given url. You must specify either of:

  • URL of the page you open to access the spreadsheet in your browser

  • URL of worksheet-based feed of the spreadseet

e.g.

session.spreadsheet_by_url(
  "https://docs.google.com/spreadsheet/ccc?key=pz7XtlQC-PYx-jrVMJErTcg")
session.spreadsheet_by_url(
  "https://spreadsheets.google.com/feeds/" +
  "worksheets/pz7XtlQC-PYx-jrVMJErTcg/private/full")
# File lib/google_spreadsheet/session.rb, line 148
def spreadsheet_by_url(url)
  # Tries to parse it as URL of human-readable spreadsheet.
  uri = URI.parse(url)
  if ["spreadsheets.google.com", "docs.google.com"].include?(uri.host) &&
      uri.path =~ %r\/ccc$/
    if (uri.query || "").split(%r&/).find(){ |s| s=~ %r^key=(.*)$/ }
      return spreadsheet_by_key($1)
    end
  end
  # Assumes the URL is worksheets feed URL.
  return Spreadsheet.new(self, url)
end
spreadsheets(params = {}) click to toggle source

Returns list of spreadsheets for the user as array of GoogleSpreadsheet::Spreadsheet. You can specify query parameters described at code.google.com/apis/spreadsheets/docs/2.0/reference.html#Parameters

e.g.

session.spreadsheets
session.spreadsheets("title" => "hoge")
# File lib/google_spreadsheet/session.rb, line 114
def spreadsheets(params = {})
  query = encode_query(params)
  doc = request(
      :get, "https://spreadsheets.google.com/feeds/spreadsheets/private/full?#{query}")
  result = []
  doc.css("feed > entry").each() do |entry|
    title = entry.css("title").text
    url = entry.css(
      "link[rel='http://schemas.google.com/spreadsheets/2006#worksheetsfeed']")[0]["href"]
    result.push(Spreadsheet.new(self, url, title))
  end
  return result
end
worksheet_by_url(url) click to toggle source

Returns GoogleSpreadsheet::Worksheet with given url. You must specify URL of cell-based feed of the worksheet.

e.g.

session.worksheet_by_url(
  "http://spreadsheets.google.com/feeds/" +
  "cells/pz7XtlQC-PYxNmbBVgyiNWg/od6/private/full")
# File lib/google_spreadsheet/session.rb, line 175
def worksheet_by_url(url)
  return Worksheet.new(self, nil, url)
end