google_drive 0.3.2 → 0.3.3

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.
data/lib/google_drive.rb CHANGED
@@ -20,6 +20,13 @@ module GoogleDrive
20
20
 
21
21
  # Authenticates with given OAuth1 or OAuth2 token.
22
22
  #
23
+ # +access_token+ can be either OAuth2 access_token string, OAuth2::AccessToken or OAuth::AccessToken.
24
+ #
25
+ # +proxy+ can be nil or return value of Net::HTTP.Proxy. If +proxy+ is specified, all
26
+ # HTTP access in the session uses the proxy. If +proxy+ is nil, it uses the proxy
27
+ # specified by http_proxy environment variable if available. Otherwise it performs direct
28
+ # access.
29
+ #
23
30
  # OAuth2 code example:
24
31
  #
25
32
  # client = OAuth2::Client.new(
@@ -36,14 +43,14 @@ module GoogleDrive
36
43
  # # Redirect the user to auth_url and get authorization code from redirect URL.
37
44
  # auth_token = client.auth_code.get_token(
38
45
  # authorization_code, :redirect_uri => "http://example.com/")
39
- # session = GoogleDrive.login_with_oauth(auth_token)
46
+ # session = GoogleDrive.login_with_oauth(auth_token.token)
40
47
  #
41
48
  # Or, from existing refresh token:
42
49
  #
43
- # access_token = OAuth2::AccessToken.from_hash(client,
50
+ # auth_token = OAuth2::AccessToken.from_hash(client,
44
51
  # {:refresh_token => refresh_token, :expires_at => expires_at})
45
- # access_token = access_token.refresh!
46
- # session = GoogleDrive.login_with_oauth(access_token)
52
+ # auth_token = auth_token.refresh!
53
+ # session = GoogleDrive.login_with_oauth(auth_token.token)
47
54
  #
48
55
  # If your app is not a Web app, use "urn:ietf:wg:oauth:2.0:oob" as redirect_url. Then
49
56
  # authorization code is shown after authorization.
@@ -68,8 +75,8 @@ module GoogleDrive
68
75
  # - http://code.google.com/apis/accounts/docs/OAuth2.html
69
76
  # - http://oauth.rubyforge.org/
70
77
  # - http://code.google.com/apis/accounts/docs/OAuth.html
71
- def self.login_with_oauth(oauth_token)
72
- return Session.login_with_oauth(oauth_token)
78
+ def self.login_with_oauth(access_token, proxy = nil)
79
+ return Session.login_with_oauth(access_token, proxy)
73
80
  end
74
81
 
75
82
  # Restores session using return value of auth_tokens method of previous session.
@@ -0,0 +1,48 @@
1
+ # Author: Hiroshi Ichikawa <http://gimite.net/>
2
+ # The license of this source is "New BSD Licence"
3
+
4
+ require "net/https"
5
+ require "uri"
6
+ Net::HTTP.version_1_2
7
+
8
+
9
+ module GoogleDrive
10
+
11
+ class BasicFetcher #:nodoc:
12
+
13
+ def initialize(proxy)
14
+ if proxy
15
+ @proxy = proxy
16
+ elsif ENV["http_proxy"] && !ENV["http_proxy"].empty?
17
+ proxy_url = URI.parse(ENV["http_proxy"])
18
+ @proxy = Net::HTTP.Proxy(proxy_url.host, proxy_url.port)
19
+ else
20
+ @proxy = Net::HTTP
21
+ end
22
+ end
23
+
24
+ def request_raw(method, url, data, extra_header, auth)
25
+ uri = URI.parse(url)
26
+ http = @proxy.new(uri.host, uri.port)
27
+ http.use_ssl = true
28
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
29
+ http.start() do
30
+ path = uri.path + (uri.query ? "?#{uri.query}" : "")
31
+ header = auth_header(auth).merge(extra_header)
32
+ if method == :delete || method == :get
33
+ return http.__send__(method, path, header)
34
+ else
35
+ return http.__send__(method, path, data, header)
36
+ end
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def auth_header(auth)
43
+ return {}
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -1,45 +1,20 @@
1
1
  # Author: Hiroshi Ichikawa <http://gimite.net/>
2
2
  # The license of this source is "New BSD Licence"
3
3
 
4
- require "net/https"
5
- require "uri"
6
- Net::HTTP.version_1_2
4
+ require "google_drive/basic_fetcher"
7
5
 
8
6
 
9
7
  module GoogleDrive
10
8
 
11
- class ClientLoginFetcher #:nodoc:
9
+ class ClientLoginFetcher < BasicFetcher #:nodoc:
12
10
 
13
11
  def initialize(auth_tokens, proxy)
12
+ super(proxy)
14
13
  @auth_tokens = auth_tokens
15
- if proxy
16
- @proxy = proxy
17
- elsif ENV["http_proxy"] && !ENV["http_proxy"].empty?
18
- proxy_url = URI.parse(ENV["http_proxy"])
19
- @proxy = Net::HTTP.Proxy(proxy_url.host, proxy_url.port)
20
- else
21
- @proxy = Net::HTTP
22
- end
23
14
  end
24
15
 
25
16
  attr_accessor(:auth_tokens)
26
17
 
27
- def request_raw(method, url, data, extra_header, auth)
28
- uri = URI.parse(url)
29
- http = @proxy.new(uri.host, uri.port)
30
- http.use_ssl = true
31
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
32
- http.start() do
33
- path = uri.path + (uri.query ? "?#{uri.query}" : "")
34
- header = auth_header(auth).merge(extra_header)
35
- if method == :delete || method == :get
36
- return http.__send__(method, path, header)
37
- else
38
- return http.__send__(method, path, data, header)
39
- end
40
- end
41
- end
42
-
43
18
  private
44
19
 
45
20
  def auth_header(auth)
@@ -14,8 +14,7 @@ module GoogleDrive
14
14
 
15
15
  include(Util)
16
16
 
17
- #:nodoc:
18
- ROOT_URL = "#{DOCS_BASE_URL}/folder%3Aroot"
17
+ ROOT_URL = "#{DOCS_BASE_URL}/folder%3Aroot" #:nodoc:
19
18
 
20
19
  alias collection_feed_url document_feed_url
21
20
 
@@ -1,47 +1,24 @@
1
1
  # Author: Hiroshi Ichikawa <http://gimite.net/>
2
2
  # The license of this source is "New BSD Licence"
3
3
 
4
- require "rubygems"
5
- require "oauth2"
4
+ require "google_drive/basic_fetcher"
6
5
 
7
6
 
8
7
  module GoogleDrive
9
8
 
10
- class OAuth2Fetcher #:nodoc:
9
+ class OAuth2Fetcher < BasicFetcher #:nodoc:
11
10
 
12
- class Response
13
-
14
- def initialize(raw_res)
15
- @raw_res = raw_res
16
- end
17
-
18
- def code
19
- return @raw_res.status.to_s()
20
- end
21
-
22
- def body
23
- return @raw_res.body
24
- end
25
-
26
- def [](name)
27
- return @raw_res.headers[name]
28
- end
29
-
11
+ def initialize(auth_token, proxy)
12
+ super(proxy)
13
+ @auth_token = auth_token
30
14
  end
31
15
 
32
- def initialize(oauth2_token)
33
- @oauth2_token = oauth2_token
34
- end
35
-
36
- def request_raw(method, url, data, extra_header, auth)
37
- if method == :delete || method == :get
38
- raw_res = @oauth2_token.request(method, url, {:headers => extra_header})
39
- else
40
- raw_res = @oauth2_token.request(method, url, {:headers => extra_header, :body => data})
41
- end
42
- return Response.new(raw_res)
16
+ private
17
+
18
+ def auth_header(auth)
19
+ return {"Authorization" => "Bearer %s" % @auth_token}
43
20
  end
44
-
21
+
45
22
  end
46
23
 
47
24
  end
@@ -6,6 +6,8 @@ require "stringio"
6
6
 
7
7
  require "rubygems"
8
8
  require "nokogiri"
9
+ require "oauth"
10
+ require "oauth2"
9
11
 
10
12
  require "google_drive/util"
11
13
  require "google_drive/client_login_fetcher"
@@ -38,15 +40,18 @@ module GoogleDrive
38
40
  end
39
41
 
40
42
  # The same as GoogleDrive.login_with_oauth.
41
- def self.login_with_oauth(oauth_token)
42
- case oauth_token
43
+ def self.login_with_oauth(access_token, proxy = nil)
44
+ case access_token
43
45
  when OAuth::AccessToken
44
- fetcher = OAuth1Fetcher.new(oauth_token)
46
+ raise(GoogleDrive::Error, "proxy is not supported with OAuth1.") if proxy
47
+ fetcher = OAuth1Fetcher.new(access_token)
45
48
  when OAuth2::AccessToken
46
- fetcher = OAuth2Fetcher.new(oauth_token)
49
+ fetcher = OAuth2Fetcher.new(access_token.token, proxy)
50
+ when String
51
+ fetcher = OAuth2Fetcher.new(access_token, proxy)
47
52
  else
48
53
  raise(GoogleDrive::Error,
49
- "oauth_token is neither OAuth::Token nor OAuth2::Token: %p" % oauth_token)
54
+ "access_token is neither String, OAuth2::Token nor OAuth::Token: %p" % access_token)
50
55
  end
51
56
  return Session.new(nil, fetcher)
52
57
  end
@@ -241,7 +246,7 @@ module GoogleDrive
241
246
  # Looks like a URL of human-readable collection page. Converts to collection feed URL.
242
247
  url = "#{DOCS_BASE_URL}/folder%3A#{$1}"
243
248
  end
244
- return Collection.new(self, url)
249
+ return Collection.new(self, to_v3_url(url))
245
250
  end
246
251
 
247
252
  # Creates new spreadsheet and returns the new GoogleDrive::Spreadsheet.
@@ -307,7 +312,7 @@ module GoogleDrive
307
312
  # session.upload_from_file("/path/to/hoge.tsv", "Hoge")
308
313
  # session.upload_from_file("/path/to/hoge.csv", "Hoge")
309
314
  # session.upload_from_file("/path/to/hoge", "Hoge", :content_type => "text/tab-separated-values")
310
- # session.upload_from_file("/path/to/hoge", "Hoge", :content_type => "text/tsv")
315
+ # session.upload_from_file("/path/to/hoge", "Hoge", :content_type => "text/csv")
311
316
  def upload_from_file(path, title = nil, params = {})
312
317
  file_name = ::File.basename(path)
313
318
  params = {:file_name => file_name}.merge(params)
@@ -154,6 +154,14 @@ module GoogleDrive
154
154
  end
155
155
  end
156
156
 
157
+ def download_to_io(io, params = {})
158
+ # General downloading API doesn't work for spreadsheets because it requires a different
159
+ # authorization token, and it has a bug that it downloads PDF when text/html is
160
+ # requested.
161
+ raise(NotImplementedError,
162
+ "Use export_as_file or export_as_string instead for GoogleDrive::Spreadsheet.")
163
+ end
164
+
157
165
  # Returns worksheets of the spreadsheet as array of GoogleDrive::Worksheet.
158
166
  def worksheets
159
167
  doc = @session.request(:get, @worksheets_feed_url)
@@ -272,7 +272,7 @@ module GoogleDrive
272
272
  "&min-col=#{cols.min}&max-col=#{cols.max}")
273
273
  doc = @session.request(:get, url)
274
274
 
275
- doc.css("entry").each() do |entry|
275
+ for entry in doc.css("entry")
276
276
  row = entry.css("gs|cell")[0]["row"].to_i()
277
277
  col = entry.css("gs|cell")[0]["col"].to_i()
278
278
  cell_entries[[row, col]] = entry
@@ -280,7 +280,7 @@ module GoogleDrive
280
280
 
281
281
  # Updates cell values using batch operation.
282
282
  # If the data is large, we split it into multiple operations, otherwise batch may fail.
283
- @modified.each_slice(250) do |chunk|
283
+ @modified.each_slice(25) do |chunk|
284
284
 
285
285
  xml = <<-EOS
286
286
  <feed xmlns="http://www.w3.org/2005/Atom"
@@ -310,7 +310,7 @@ module GoogleDrive
310
310
 
311
311
  batch_url = concat_url(@cells_feed_url, "/batch")
312
312
  result = @session.request(:post, batch_url, :data => xml)
313
- result.css("atom|entry").each() do |entry|
313
+ for entry in result.css("atom|entry")
314
314
  interrupted = entry.css("batch|interrupted")[0]
315
315
  if interrupted
316
316
  raise(GoogleDrive::Error, "Update has failed: %s" %
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: google_drive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.3.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-10-01 00:00:00.000000000 Z
12
+ date: 2013-01-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -87,22 +87,6 @@ dependencies:
87
87
  - - ! '>='
88
88
  - !ruby/object:Gem::Version
89
89
  version: 0.8.0
90
- - !ruby/object:Gem::Dependency
91
- name: debugger
92
- requirement: !ruby/object:Gem::Requirement
93
- none: false
94
- requirements:
95
- - - ! '>='
96
- - !ruby/object:Gem::Version
97
- version: '0'
98
- type: :development
99
- prerelease: false
100
- version_requirements: !ruby/object:Gem::Requirement
101
- none: false
102
- requirements:
103
- - - ! '>='
104
- - !ruby/object:Gem::Version
105
- version: '0'
106
90
  description: A library to read/write files/spreadsheets in Google Drive/Docs.
107
91
  email:
108
92
  - gimite+github@gmail.com
@@ -131,11 +115,13 @@ files:
131
115
  - lib/google_drive/authentication_error.rb
132
116
  - lib/google_drive/util.rb
133
117
  - lib/google_drive/list.rb
118
+ - lib/google_drive/basic_fetcher.rb
134
119
  - lib/google_drive/record.rb
135
120
  - doc_src/google_drive/acl.rb
136
121
  - doc_src/google_drive/acl_entry.rb
137
122
  homepage: https://github.com/gimite/google-drive-ruby
138
- licenses: []
123
+ licenses:
124
+ - New BSD
139
125
  post_install_message:
140
126
  rdoc_options:
141
127
  - --main