Class: GoogleSpreadsheet::Session
- Inherits:
-
Object
- Object
- GoogleSpreadsheet::Session
- 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
-
#auth_token ⇒ Object
Authentication token.
-
#on_auth_fail ⇒ Object
Proc or Method called when authentication has failed.
Class Method Summary collapse
-
.login(mail, password) ⇒ Object
The same as GoogleSpreadsheet.login.
Instance Method Summary collapse
-
#delete(url) ⇒ Object
:nodoc:.
-
#get(url) ⇒ Object
:nodoc:.
-
#http_header ⇒ Object
:nodoc:.
-
#initialize(auth_token = nil) ⇒ Session
constructor
Creates session object with given authentication token.
-
#login(mail, password) ⇒ Object
Authenticates with given
mailandpassword, and updates current session object if succeeds. -
#post(url, data) ⇒ Object
:nodoc:.
-
#put(url, data) ⇒ Object
:nodoc:.
-
#spreadsheet_by_key(key) ⇒ Object
Returns GoogleSpreadsheet::Spreadsheet with given
key. -
#spreadsheet_by_url(url) ⇒ Object
Returns GoogleSpreadsheet::Spreadsheet with given
url. -
#spreadsheets(params = {}) ⇒ Object
Returns list of spreadsheets for the user as array of GoogleSpreadsheet::Spreadsheet.
-
#worksheet_by_url(url) ⇒ Object
Returns GoogleSpreadsheet::Worksheet with given
url.
Methods included from Util
as_utf8, encode_query, h, http_request, uri_encode
Constructor Details
#initialize(auth_token = nil) ⇒ Session
Creates session object with given authentication token.
136 137 138 |
# File 'lib/google_spreadsheet.rb', line 136 def initialize(auth_token = nil) @auth_token = auth_token end |
Instance Attribute Details
#auth_token ⇒ Object
Authentication token.
163 164 165 |
# File 'lib/google_spreadsheet.rb', line 163 def auth_token @auth_token end |
#on_auth_fail ⇒ Object
Proc or Method called when authentication has failed. When this function returns true, it tries again.
167 168 169 |
# File 'lib/google_spreadsheet.rb', line 167 def on_auth_fail @on_auth_fail end |
Class Method Details
.login(mail, password) ⇒ Object
The same as GoogleSpreadsheet.login.
129 130 131 132 133 |
# File 'lib/google_spreadsheet.rb', line 129 def self.login(mail, password) session = Session.new() session.login(mail, password) return session end |
Instance Method Details
#delete(url) ⇒ Object
:nodoc:
196 197 198 199 200 |
# File 'lib/google_spreadsheet.rb', line 196 def delete(url) #:nodoc: 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:
169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/google_spreadsheet.rb', line 169 def get(url) #:nodoc: while true begin response = open(url, self.http_header){ |f| f.read() } rescue OpenURI::HTTPError => ex if ex. =~ /^401/ && @on_auth_fail && @on_auth_fail.call() next end raise(ex. =~ /^401/ ? AuthenticationError : GoogleSpreadsheet::Error, "Error #{ex.message} for GET #{url}: " + ex.io.read()) end return Hpricot.XML(response) end end |
#http_header ⇒ Object
:nodoc:
202 203 204 |
# File 'lib/google_spreadsheet.rb', line 202 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.
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/google_spreadsheet.rb', line 143 def login(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:
184 185 186 187 188 |
# File 'lib/google_spreadsheet.rb', line 184 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:
190 191 192 193 194 |
# File 'lib/google_spreadsheet.rb', line 190 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")
231 232 233 234 |
# File 'lib/google_spreadsheet.rb', line 231 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")
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/google_spreadsheet.rb', line 245 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")
213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/google_spreadsheet.rb', line 213 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 = as_utf8(entry.search("title").text) url = as_utf8(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")
263 264 265 |
# File 'lib/google_spreadsheet.rb', line 263 def worksheet_by_url(url) return Worksheet.new(self, nil, url) end |