google-spreadsheet-ruby 0.0.9 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/README.rdoc +1 -0
  2. data/lib/google_spreadsheet.rb +81 -32
  3. metadata +12 -2
data/README.rdoc CHANGED
@@ -17,6 +17,7 @@ Example:
17
17
  require "google_spreadsheet"
18
18
 
19
19
  # Logs in.
20
+ # You can also use OAuth. See document of GoogleSpreadsheet.login_with_oauth for details.
20
21
  session = GoogleSpreadsheet.login("username@gmail.com", "mypassword")
21
22
 
22
23
  # First worksheet of http://spreadsheets.google.com/ccc?key=pz7XtlQC-PYx-jrVMJErTcg&hl=en
@@ -9,6 +9,7 @@ require "cgi"
9
9
  require "uri"
10
10
  require "rubygems"
11
11
  require "hpricot"
12
+ require "oauth"
12
13
  Net::HTTP.version_1_2
13
14
 
14
15
  module GoogleSpreadsheet
@@ -19,7 +20,29 @@ module GoogleSpreadsheet
19
20
  def self.login(mail, password)
20
21
  return Session.login(mail, password)
21
22
  end
22
-
23
+
24
+ # Authenticates with given OAuth token.
25
+ #
26
+ # For generating oauth_token, you can proceed as follow:
27
+ #
28
+ # 1) First generate OAuth consumer object with key and secret for your site by registering site with google
29
+ # @consumer = OAuth::Consumer.new( "key","secret", {:site=>"https://agree2"})
30
+ # 2) Request token with OAuth
31
+ # @request_token = @consumer.get_request_token
32
+ # session[:request_token] = @request_token
33
+ # redirect_to @request_token.authorize_url
34
+ # 3) Create an oauth access token
35
+ # @oauth_access_token = @request_token.get_access_token
36
+ # @access_token = OAuth::AccessToken.new(@consumer, @oauth_access_token.token, @oauth_access_token.secret)
37
+ #
38
+ # See these documents for details:
39
+ #
40
+ # - http://oauth.rubyforge.org/
41
+ # - http://code.google.com/apis/accounts/docs/OAuth.html
42
+ def self.login_with_oauth(oauth_token)
43
+ return Session.login_with_oauth(oauth_token)
44
+ end
45
+
23
46
  # Restores GoogleSpreadsheet::Session from +path+ and returns it.
24
47
  # If +path+ doesn't exist or authentication has failed, prompts mail and password on console,
25
48
  # authenticates with them, stores the session to +path+ and returns it.
@@ -111,12 +134,21 @@ module GoogleSpreadsheet
111
134
  session.login(mail, password)
112
135
  return session
113
136
  end
114
-
137
+
138
+ # The same as GoogleSpreadsheet.login_with_oauth.
139
+ def self.login_with_oauth(oauth_token)
140
+ session = Session.new(nil, oauth_token)
141
+ end
142
+
115
143
  # Restores session using return value of auth_tokens method of previous session.
116
- def initialize(auth_tokens = {})
117
- @auth_tokens = auth_tokens
144
+ def initialize(auth_tokens = nil, oauth_token = nil)
145
+ if oauth_token
146
+ @oauth_token = oauth_token
147
+ else
148
+ @auth_tokens = auth_tokens
149
+ end
118
150
  end
119
-
151
+
120
152
  # Authenticates with given +mail+ and +password+, and updates current session object
121
153
  # if succeeds. Raises GoogleSpreadsheet::AuthenticationError if fails.
122
154
  # Google Apps account is supported.
@@ -244,41 +276,58 @@ module GoogleSpreadsheet
244
276
  end
245
277
  response_type = params[:response_type] || :xml
246
278
 
247
- http = Net::HTTP.new(uri.host, uri.port)
248
- http.use_ssl = uri.scheme == "https"
249
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
250
- http.start() do
251
- while true
252
- path = uri.path + (uri.query ? "?#{uri.query}" : "")
253
- header = auth_header(auth).merge(add_header)
254
- if method == :delete || method == :get
255
- response = http.__send__(method, path, header)
256
- else
257
- response = http.__send__(method, path, data, header)
258
- end
259
- if response.code == "401" && @on_auth_fail && @on_auth_fail.call()
260
- next
261
- end
262
- if !(response.code =~ /^2/)
263
- raise(
264
- response.code == "401" ? AuthenticationError : GoogleSpreadsheet::Error,
265
- "Response code #{response.code} for #{method} #{url}: " +
266
- CGI.unescapeHTML(response.body))
267
- end
268
- case response_type
269
- when :xml
270
- return Hpricot.XML(response.body)
271
- when :raw
272
- return response.body
279
+ if @oauth_token
280
+
281
+ if method == :delete || method == :get
282
+ response = @oauth_token.__send__(method, url)
283
+ else
284
+ response = @oauth_token.__send__(method, url, data)
285
+ end
286
+ return convert_response(response, response_type)
287
+
288
+ else
289
+
290
+ http = Net::HTTP.new(uri.host, uri.port)
291
+ http.use_ssl = uri.scheme == "https"
292
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
293
+ http.start() do
294
+ while true
295
+ path = uri.path + (uri.query ? "?#{uri.query}" : "")
296
+ header = auth_header(auth).merge(add_header)
297
+ if method == :delete || method == :get
298
+ response = http.__send__(method, path, header)
273
299
  else
274
- raise("unknown params[:response_type]: %s" % response_type)
300
+ response = http.__send__(method, path, data, header)
301
+ end
302
+ if response.code == "401" && @on_auth_fail && @on_auth_fail.call()
303
+ next
304
+ end
305
+ if !(response.code =~ /^2/)
306
+ raise(
307
+ response.code == "401" ? AuthenticationError : GoogleSpreadsheet::Error,
308
+ "Response code #{response.code} for #{method} #{url}: " +
309
+ CGI.unescapeHTML(response.body))
310
+ end
311
+ return convert_response(response, response_type)
275
312
  end
276
313
  end
314
+
277
315
  end
278
316
  end
279
317
 
280
318
  private
281
319
 
320
+ def convert_response(response, response_type)
321
+ case response_type
322
+ when :xml
323
+ return Hpricot.XML(response.body)
324
+ when :raw
325
+ return response.body
326
+ else
327
+ raise("unknown params[:response_type]: %s" % response_type)
328
+ end
329
+ end
330
+
282
331
  def authenticate(mail, password, auth)
283
332
  params = {
284
333
  "accountType" => "HOSTED_OR_GOOGLE",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google-spreadsheet-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.9
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hiroshi Ichikawa
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-01-11 00:00:00 +09:00
12
+ date: 2010-01-17 00:00:00 +09:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -22,6 +22,16 @@ dependencies:
22
22
  - !ruby/object:Gem::Version
23
23
  version: "0.3"
24
24
  version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: oauth
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.6
34
+ version:
25
35
  description: This is a library to read/write Google Spreadsheet.
26
36
  email:
27
37
  - gimite+github@gmail.com