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.



112
113
114
# File 'lib/google_spreadsheet.rb', line 112

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

Instance Attribute Details

#auth_tokenObject

Authentication token.



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

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.



143
144
145
# File 'lib/google_spreadsheet.rb', line 143

def on_auth_fail
  @on_auth_fail
end

Class Method Details

.login(mail, password) ⇒ Object

The same as GoogleSpreadsheet.login.



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

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

Instance Method Details

#delete(url) ⇒ Object



172
173
174
175
176
# File 'lib/google_spreadsheet.rb', line 172

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

#get(url) ⇒ Object

:nodoc:



145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/google_spreadsheet.rb', line 145

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:



178
179
180
# File 'lib/google_spreadsheet.rb', line 178

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.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/google_spreadsheet.rb', line 119

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:



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

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:



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

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


207
208
209
210
# File 'lib/google_spreadsheet.rb', line 207

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


221
222
223
224
225
226
227
228
229
230
231
# File 'lib/google_spreadsheet.rb', line 221

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


189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/google_spreadsheet.rb', line 189

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


239
240
241
# File 'lib/google_spreadsheet.rb', line 239

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