google_drive 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,148 +0,0 @@
1
- # Author: Hiroshi Ichikawa <http://gimite.net/>
2
- # The license of this source is "New BSD Licence"
3
-
4
- require "rubygems"
5
- require "google/api_client"
6
- require "json"
7
-
8
- require "google_drive_v0/session"
9
-
10
-
11
- module GoogleDriveV0
12
-
13
- # Authenticates with given +mail+ and +password+, and returns GoogleDriveV0::Session
14
- # if succeeds. Raises GoogleDriveV0::AuthenticationError if fails.
15
- # Google Apps account is supported.
16
- #
17
- # +proxy+ is deprecated, and will be removed in the next version.
18
- def self.login(mail, password, proxy = nil)
19
- return Session.login(mail, password, proxy)
20
- end
21
-
22
- # Authenticates with given OAuth2 token.
23
- #
24
- # +access_token+ can be either OAuth2 access_token string or OAuth2::AccessToken.
25
- # Specifying OAuth::AccessToken is deprecated, and will not work in the next version.
26
- #
27
- # +proxy+ is deprecated, and will be removed in the next version.
28
- #
29
- # OAuth2 code example:
30
- #
31
- # client = OAuth2::Client.new(
32
- # your_client_id, your_client_secret,
33
- # :site => "https://accounts.google.com",
34
- # :token_url => "/o/oauth2/token",
35
- # :authorize_url => "/o/oauth2/auth")
36
- # auth_url = client.auth_code.authorize_url(
37
- # :redirect_uri => "http://example.com/",
38
- # :scope =>
39
- # "https://docs.google.com/feeds/ " +
40
- # "https://docs.googleusercontent.com/ " +
41
- # "https://spreadsheets.google.com/feeds/")
42
- # # Redirect the user to auth_url and get authorization code from redirect URL.
43
- # auth_token = client.auth_code.get_token(
44
- # authorization_code, :redirect_uri => "http://example.com/")
45
- # session = GoogleDriveV0.login_with_oauth(auth_token.token)
46
- #
47
- # Or, from existing refresh token:
48
- #
49
- # auth_token = OAuth2::AccessToken.from_hash(client,
50
- # {:refresh_token => refresh_token, :expires_at => expires_at})
51
- # auth_token = auth_token.refresh!
52
- # session = GoogleDriveV0.login_with_oauth(auth_token.token)
53
- #
54
- # If your app is not a Web app, use "urn:ietf:wg:oauth:2.0:oob" as redirect_url. Then
55
- # authorization code is shown after authorization.
56
- #
57
- # See these documents for details:
58
- #
59
- # - https://github.com/intridea/oauth2
60
- # - http://code.google.com/apis/accounts/docs/OAuth2.html
61
- # - http://oauth.rubyforge.org/
62
- # - http://code.google.com/apis/accounts/docs/OAuth.html
63
- def self.login_with_oauth(access_token, proxy = nil)
64
- return Session.login_with_oauth(access_token, proxy)
65
- end
66
-
67
- # Restores session using return value of auth_tokens method of previous session.
68
- #
69
- # See GoogleDriveV0.login for description of parameter +proxy+.
70
- def self.restore_session(auth_tokens, proxy = nil)
71
- return Session.restore_session(auth_tokens, proxy)
72
- end
73
-
74
- # Restores GoogleDriveV0::Session from +path+ and returns it.
75
- # If +path+ doesn't exist or authentication has failed, prompts the user to authorize the access,
76
- # stores the session to +path+ and returns it.
77
- #
78
- # +path+ defaults to ENV["HOME"] + "/.ruby_google_drive.token".
79
- #
80
- # You can specify your own OAuth +client_id+ and +client_secret+. Otherwise the default one is used.
81
- def self.saved_session(path = nil, proxy = nil, client_id = nil, client_secret = nil)
82
-
83
- if proxy
84
- raise(
85
- ArgumentError,
86
- "Specifying a proxy object is no longer supported. Set ENV[\"http_proxy\"] instead.")
87
- end
88
-
89
- if !client_id && !client_secret
90
- client_id = "452925651630-egr1f18o96acjjvphpbbd1qlsevkho1d.apps.googleusercontent.com"
91
- client_secret = "1U3-Krii5x1oLPrwD5zgn-ry"
92
- elsif !client_id || !client_secret
93
- raise(ArgumentError, "client_id and client_secret must be both specified or both omitted")
94
- end
95
-
96
- path ||= ENV["HOME"] + "/.ruby_google_drive.token"
97
- if ::File.exist?(path)
98
- lines = ::File.readlines(path)
99
- case lines.size
100
- when 1
101
- token_data = JSON.parse(lines[0].chomp())
102
- when 2
103
- # Old format.
104
- token_data = nil
105
- else
106
- raise(ArgumentError, "Not a token file: %s" % path)
107
- end
108
- else
109
- token_data = nil
110
- end
111
-
112
- client = Google::APIClient.new(
113
- :application_name => "google_drive Ruby library",
114
- :application_version => "0.3.11"
115
- )
116
- auth = client.authorization
117
- auth.client_id = client_id
118
- auth.client_secret = client_secret
119
- auth.scope =
120
- "https://www.googleapis.com/auth/drive " +
121
- "https://spreadsheets.google.com/feeds/ " +
122
- "https://docs.google.com/feeds/ " +
123
- "https://docs.googleusercontent.com/"
124
- auth.redirect_uri = "urn:ietf:wg:oauth:2.0:oob"
125
-
126
- if token_data
127
-
128
- auth.refresh_token = token_data["refresh_token"]
129
- auth.fetch_access_token!()
130
-
131
- else
132
-
133
- $stderr.print("\n1. Open this page:\n%s\n\n" % auth.authorization_uri)
134
- $stderr.print("2. Enter the authorization code shown in the page: ")
135
- auth.code = $stdin.gets().chomp()
136
- auth.fetch_access_token!()
137
- token_data = {"refresh_token" => auth.refresh_token}
138
- open(path, "w", 0600) do |f|
139
- f.puts(JSON.dump(token_data))
140
- end
141
-
142
- end
143
-
144
- return GoogleDriveV0.login_with_oauth(auth.access_token)
145
-
146
- end
147
-
148
- end
@@ -1,115 +0,0 @@
1
- # Author: Guy Boertje <https://github.com/guyboertje>
2
- # Author: David R. Albrecht <https://github.com/eldavido>
3
- # Author: Hiroshi Ichikawa <http://gimite.net/>
4
- # The license of this source is "New BSD Licence"
5
-
6
- require "google_drive_v0/acl_entry"
7
-
8
- module GoogleDriveV0
9
-
10
- # ACL (access control list) of a spreadsheet.
11
- #
12
- # Use GoogleDriveV0::Spreadsheet#acl to get GoogleDriveV0::Acl object.
13
- # See GoogleDriveV0::Spreadsheet#acl for usage example.
14
- #
15
- # This code is based on https://github.com/guyboertje/gdata-spreadsheet-ruby .
16
- class Acl
17
-
18
- include(Util)
19
- extend(Forwardable)
20
-
21
- def initialize(session, acls_feed_url) #:nodoc:
22
- @session = session
23
- @acls_feed_url = acls_feed_url
24
- header = {"GData-Version" => "3.0"}
25
- doc = @session.request(:get, @acls_feed_url, :header => header, :auth => :writely)
26
- @acls = doc.css("entry").map(){ |e| AclEntry.new(entry_to_params(e)) }
27
- end
28
-
29
- def_delegators(:@acls, :size, :[], :each)
30
-
31
- # Adds a new entry. +entry+ is either a GoogleDriveV0::AclEntry or a Hash with keys
32
- # :scope_type, :scope and :role. See GoogleDriveV0::AclEntry#scope_type and
33
- # GoogleDriveV0::AclEntry#role for the document of the fields.
34
- #
35
- # NOTE: This sends email to the new people.
36
- #
37
- # e.g.
38
- # # A specific user can read or write.
39
- # spreadsheet.acl.push(
40
- # {:scope_type => "user", :scope => "example2@gmail.com", :role => "reader"})
41
- # spreadsheet.acl.push(
42
- # {:scope_type => "user", :scope => "example3@gmail.com", :role => "writer"})
43
- # # Publish on the Web.
44
- # spreadsheet.acl.push(
45
- # {:scope_type => "default", :role => "reader"})
46
- # # Anyone who knows the link can read.
47
- # spreadsheet.acl.push(
48
- # {:scope_type => "default", :with_key => true, :role => "reader"})
49
- def push(entry)
50
-
51
- entry = AclEntry.new(entry) if entry.is_a?(Hash)
52
-
53
- header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml;charset=utf-8"}
54
- doc = @session.request(
55
- :post, @acls_feed_url, :data => entry.to_xml(), :header => header, :auth => :writely)
56
-
57
- entry.params = entry_to_params(doc.root)
58
- @acls.push(entry)
59
- return entry
60
-
61
- end
62
-
63
- # Deletes an ACL entry.
64
- #
65
- # e.g.
66
- # spreadsheet.acl.delete(spreadsheet.acl[1])
67
- def delete(entry)
68
- header = {"GData-Version" => "3.0"}
69
- @session.request(:delete, entry.edit_url_internal, :header => header, :auth => :writely)
70
- @acls.delete(entry)
71
- end
72
-
73
- def update_role(entry) #:nodoc:
74
-
75
- header = {"GData-Version" => "3.0", "Content-Type" => "application/atom+xml;charset=utf-8"}
76
- doc = @session.request(
77
- :put, entry.edit_url_internal, :data => entry.to_xml(), :header => header, :auth => :writely)
78
-
79
- entry.params = entry_to_params(doc.root)
80
- return entry
81
-
82
- end
83
-
84
- def inspect
85
- return "\#<%p %p>" % [self.class, @acls]
86
- end
87
-
88
- private
89
-
90
- def entry_to_params(entry)
91
-
92
- if !entry.css("gAcl|withKey").empty?
93
- with_key = true
94
- role = entry.css("gAcl|withKey gAcl|role")[0]["value"]
95
- else
96
- with_key = false
97
- role = entry.css("gAcl|role")[0]["value"]
98
- end
99
-
100
- return {
101
- :acl => self,
102
- :scope_type => entry.css("gAcl|scope")[0]["type"],
103
- :scope => entry.css("gAcl|scope")[0]["value"],
104
- :with_key => with_key,
105
- :role => role,
106
- :title => entry.css("title").text,
107
- :edit_url => entry.css("link[rel='edit']")[0]["href"],
108
- :etag => entry["etag"],
109
- }
110
-
111
- end
112
-
113
- end
114
-
115
- end
@@ -1,100 +0,0 @@
1
- # Author: Guy Boertje <https://github.com/guyboertje>
2
- # Author: David R. Albrecht <https://github.com/eldavido>
3
- # Author: Hiroshi Ichikawa <http://gimite.net/>
4
- # Author: Phuogn Nguyen <https://github.com/phuongnd08>
5
- # The license of this source is "New BSD Licence"
6
-
7
- module GoogleDriveV0
8
-
9
- # An entry of an ACL (access control list) of a spreadsheet.
10
- #
11
- # Use GoogleDriveV0::Acl#[] to get GoogleDriveV0::AclEntry object.
12
- #
13
- # This code is based on https://github.com/guyboertje/gdata-spreadsheet-ruby .
14
- class AclEntry
15
-
16
- include(Util)
17
-
18
- PARAM_NAMES = [:acl, :scope_type, :scope, :with_key, :role, :title, :edit_url, :etag] #:nodoc:
19
-
20
- # +params+ is a Hash object with keys +:scope_type+, +:scope+ and +:role+.
21
- # See scope_type and role for the document of the fields.
22
- def initialize(params)
23
- @params = {:role => "reader"}
24
- for name, value in params
25
- if !name.is_a?(Symbol)
26
- raise(ArgumentError, "Key must be Symbol, but is %p" % name)
27
- elsif !PARAM_NAMES.include?(name)
28
- raise(ArgumentError, "Invalid key: %p" % name)
29
- end
30
- @params[name] = value
31
- end
32
- end
33
-
34
- attr_accessor(:params) #:nodoc:
35
-
36
- PARAM_NAMES.each() do |name|
37
- define_method(name) do
38
- return @params[name]
39
- end
40
- end
41
-
42
- def edit_url
43
- warn(
44
- "WARNING: GoogleDriveV0::AclEntry\#edit_url is deprecated and will be removed in the next version.")
45
- return self.edit_url_internal
46
- end
47
-
48
- def edit_url_internal #:nodoc:
49
- return @params[:edit_url]
50
- end
51
-
52
- # Changes the role of the scope.
53
- #
54
- # e.g.
55
- # spreadsheet.acl[1].role = "writer"
56
- def role=(role)
57
- @params[:role] = role
58
- @params[:acl].update_role(self)
59
- end
60
-
61
- def inspect
62
- return "\#<%p scope_type=%p, scope=%p, with_key=%p, role=%p>" %
63
- [self.class, @params[:scope_type], @params[:scope], @params[:with_key], @params[:role]]
64
- end
65
-
66
- def to_xml() #:nodoc:
67
-
68
- etag_attr = self.etag ? "gd:etag='#{h(self.etag)}'" : ""
69
- value_attr = self.scope ? "value='#{h(self.scope)}'" : ""
70
- if self.with_key
71
- role_tag = <<-EOS
72
- <gAcl:withKey key='[ACL KEY]'>
73
- <gAcl:role value='#{h(self.role)}'/>
74
- </gAcl:withKey>
75
- EOS
76
- else
77
- role_tag = <<-EOS
78
- <gAcl:role value='#{h(self.role)}'/>
79
- EOS
80
- end
81
-
82
- return <<-EOS
83
- <entry
84
- xmlns='http://www.w3.org/2005/Atom'
85
- xmlns:gAcl='http://schemas.google.com/acl/2007'
86
- xmlns:gd='http://schemas.google.com/g/2005'
87
- #{etag_attr}>
88
- <category scheme='http://schemas.google.com/g/2005#kind'
89
- term='http://schemas.google.com/acl/2007#accessRule'/>
90
- #{role_tag}
91
- <gAcl:scope type='#{h(self.scope_type)}' #{value_attr}/>
92
- </entry>
93
- EOS
94
-
95
- end
96
-
97
- end
98
-
99
- end
100
-
@@ -1,47 +0,0 @@
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 GoogleDriveV0
10
-
11
- class ApiClientFetcher
12
-
13
- class Response
14
-
15
- def initialize(client_response)
16
- @client_response = client_response
17
- end
18
-
19
- def code
20
- return @client_response.status.to_s()
21
- end
22
-
23
- def body
24
- return @client_response.body
25
- end
26
-
27
- end
28
-
29
- def initialize(client)
30
- @client = client
31
- end
32
-
33
- attr_reader(:client)
34
-
35
- def request_raw(method, url, data, extra_header, auth)
36
- p [method, url, data, extra_header, auth]
37
- client_response = @client.execute(
38
- :http_method => method,
39
- :uri => url,
40
- :body => data,
41
- :headers => extra_header)
42
- return Response.new(client_response)
43
- end
44
-
45
- end
46
-
47
- end
@@ -1,14 +0,0 @@
1
- # Author: Hiroshi Ichikawa <http://gimite.net/>
2
- # The license of this source is "New BSD Licence"
3
-
4
- require "google_drive_v0/error"
5
-
6
-
7
- module GoogleDriveV0
8
-
9
- # Raised when GoogleDriveV0.login has failed.
10
- class AuthenticationError < GoogleDriveV0::Error
11
-
12
- end
13
-
14
- end
@@ -1,50 +0,0 @@
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 GoogleDriveV0
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
- # No timeout. It can take long e.g., when it tries to fetch a large file.
30
- http.read_timeout = nil
31
- http.start() do
32
- path = uri.path + (uri.query ? "?#{uri.query}" : "")
33
- header = auth_header(auth).merge(extra_header)
34
- if method == :delete || method == :get
35
- return http.__send__(method, path, header)
36
- else
37
- return http.__send__(method, path, data, header)
38
- end
39
- end
40
- end
41
-
42
- private
43
-
44
- def auth_header(auth)
45
- return {}
46
- end
47
-
48
- end
49
-
50
- end