Class: GoogleSpreadsheet::Session

Inherits:
Object
  • Object
show all
Extended by:
Util
Includes:
Util
Defined in:
lib/google_spreadsheet.rb

Overview

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

encode_query, h, http_request, uri_encode

Constructor Details

#initialize(auth_token = nil) ⇒ Session

Creates session object with given authentication token.



107
108
109
# File 'lib/google_spreadsheet.rb', line 107

def initialize(auth_token = nil)
  @auth_token = auth_token
end

Instance Attribute Details

#auth_tokenObject

Authentication token.



134
135
136
# File 'lib/google_spreadsheet.rb', line 134

def auth_token
  @auth_token
end

#on_auth_failObject

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



138
139
140
# File 'lib/google_spreadsheet.rb', line 138

def on_auth_fail
  @on_auth_fail
end

Class Method Details

.login(mail, password) ⇒ Object

The same as GoogleSpreadsheet.login.



100
101
102
103
104
# File 'lib/google_spreadsheet.rb', line 100

def self.(mail, password)
  session = Session.new()
  session.(mail, password)
  return session
end

Instance Method Details

#get(url) ⇒ Object

:nodoc:



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/google_spreadsheet.rb', line 140

def get(url) #:nodoc:
  while true
    begin
      response = open(url, self.http_header){ |f| f.read() }
    rescue OpenURI::HTTPError => ex
      if ex.message =~ /^401/ && @on_auth_fail && @on_auth_fail.call()
        next
      end
      raise(ex.message =~ /^401/ ? AuthenticationError : GoogleSpreadsheet::Error,
        "Error #{ex.message} for GET #{url}: " + ex.io.read())
    end
    return Hpricot.XML(response)
  end
end

#http_headerObject

:nodoc:



167
168
169
# File 'lib/google_spreadsheet.rb', line 167

def http_header #:nodoc:
  return {"Authorization" => "GoogleLogin auth=#{@auth_token}"}
end

#login(mail, password) ⇒ Object

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



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/google_spreadsheet.rb', line 114

def (mail, password)
  begin
    @auth_token = nil
    params = {
      "accountType" => "HOSTED_OR_GOOGLE",
      "Email" => mail,
      "Passwd" => password,
      "service" => "wise",
      "source" => "Gimite-RubyGoogleSpreadsheet-1.00",
    }
    response = http_request(:post,
      "https://www.google.com/accounts/ClientLogin", encode_query(params))
    @auth_token = response.slice(/^Auth=(.*)$/, 1)
  rescue GoogleSpreadsheet::Error => ex
    return true if @on_auth_fail && @on_auth_fail.call()
    raise(AuthenticationError, "authentication failed for #{mail}: #{ex.message}")
  end
end

#post(url, data) ⇒ Object

:nodoc:



155
156
157
158
159
# File 'lib/google_spreadsheet.rb', line 155

def post(url, data) #:nodoc:
  header = self.http_header.merge({"Content-Type" => "application/atom+xml"})
  response = http_request(:post, url, data, header)
  return Hpricot.XML(response)
end

#put(url, data) ⇒ Object

:nodoc:



161
162
163
164
165
# File 'lib/google_spreadsheet.rb', line 161

def put(url, data) #:nodoc:
  header = self.http_header.merge({"Content-Type" => "application/atom+xml"})
  response = http_request(:put, url, data, header)
  return Hpricot.XML(response)
end

#spreadsheet_by_key(key) ⇒ Object

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")


196
197
198
199
# File 'lib/google_spreadsheet.rb', line 196

def spreadsheet_by_key(key)
  url = "http://spreadsheets.google.com/feeds/worksheets/#{key}/private/full"
  return Spreadsheet.new(self, url)
end

#spreadsheet_by_url(url) ⇒ Object

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(
  "http://spreadsheets.google.com/ccc?key=pz7XtlQC-PYx-jrVMJErTcg&hl=en")
session.spreadsheet_by_url(
  "http://spreadsheets.google.com/feeds/worksheets/pz7XtlQC-PYx-jrVMJErTcg/private/full")


210
211
212
213
214
215
216
217
218
219
220
# File 'lib/google_spreadsheet.rb', line 210

def spreadsheet_by_url(url)
  # Tries to parse it as URL of human-readable spreadsheet.
  uri = URI.parse(url)
  if uri.host == "spreadsheets.google.com" && uri.path =~ /\/ccc$/
    if (uri.query || "").split(/&/).find(){ |s| s=~ /^key=(.*)$/ }
      return spreadsheet_by_key($1)
    end
  end
  # Assumes the URL is worksheets feed URL.
  return Spreadsheet.new(self, url)
end

#spreadsheets(params = {}) ⇒ Object

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")


178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/google_spreadsheet.rb', line 178

def spreadsheets(params = {})
  query = encode_query(params)
  doc = get("http://spreadsheets.google.com/feeds/spreadsheets/private/full?#{query}")
  result = []
  for entry in doc.search("entry")
    title = entry.search("title").text
    url = entry.search(
      "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) ⇒ Object

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")


228
229
230
# File 'lib/google_spreadsheet.rb', line 228

def worksheet_by_url(url)
  return Worksheet.new(self, url)
end