easy-google-drive 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 79aaf7be5f4100be302d186b13a9740192deed4f
4
+ data.tar.gz: 208ae462248a3d7aaacc80fd5401aef89eb9e680
5
+ SHA512:
6
+ metadata.gz: 2c49ef1c9962c807e3fbab46e650530d059003e266d0dfd5eaf86843087ff8cf7521870de43af4a8aa3549e30a44de83c353c8c70d2f6a1cc8ce663757d1e679
7
+ data.tar.gz: d4f9bd240b6c432d1c5f91f3f28e5b64c60c62112ccf670cf5ba4b4f5ee84da4581a9d2bab6ab98d61fe01dfbf799dcf9298a4246d03b34cf3ae5f286a0162e3
data/.gitignore ADDED
@@ -0,0 +1,53 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+ tags
13
+ client_secret.json
14
+ gdrive.dat
15
+
16
+ # Used by dotenv library to load environment variables.
17
+ # .env
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ # Gemfile.lock
49
+ # .ruby-version
50
+ # .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.1
4
+ before_install: gem install bundler -v 1.11.2
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at saitou088@dsn.lapis-semi.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in easy-google-drive.gemspec
4
+ gemspec
5
+ gem "google_drive","2.1.3"
6
+ gem "termcolor","1.2.2"
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ easy-google-drive (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ diff-lcs (1.3)
10
+ rake (10.5.0)
11
+ rspec (3.6.0)
12
+ rspec-core (~> 3.6.0)
13
+ rspec-expectations (~> 3.6.0)
14
+ rspec-mocks (~> 3.6.0)
15
+ rspec-core (3.6.0)
16
+ rspec-support (~> 3.6.0)
17
+ rspec-expectations (3.6.0)
18
+ diff-lcs (>= 1.2.0, < 2.0)
19
+ rspec-support (~> 3.6.0)
20
+ rspec-mocks (3.6.0)
21
+ diff-lcs (>= 1.2.0, < 2.0)
22
+ rspec-support (~> 3.6.0)
23
+ rspec-support (3.6.0)
24
+
25
+ PLATFORMS
26
+ ruby
27
+
28
+ DEPENDENCIES
29
+ bundler (~> 1.11)
30
+ easy-google-drive!
31
+ rake (~> 10.0)
32
+ rspec (~> 3.0)
33
+
34
+ BUNDLED WITH
35
+ 1.11.2
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 NaotakaSaito
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,107 @@
1
+ # About easy-google-drive
2
+ This repository is ruby library to use google drive of file managmenemt and asscess to spread sheet.
3
+ This libary use google DriveV3 and Sheet API V4.
4
+ The library saves "gdrive.dat" in root folder of google drive.
5
+
6
+ ## how to use
7
+ 1. install google drive api for ruby.
8
+ ```bash
9
+ $ sudo gem install easy-google-drive
10
+ ```
11
+
12
+ recommend to git it for sample execution.
13
+ ```bash
14
+ $ git clone git://github.com/NaotakaSaito/easy-google-drive
15
+ ```
16
+ 2. setting of google drive
17
+ 1. access to [https://console.developers.google.com/](https://console.developers.google.com/)
18
+
19
+ 2. login google account
20
+ 3. click to "プロジェクトを選択"
21
+ 4. click "+" button and create new project![choose_project](img/choose_project.png)
22
+ 5. click "API Manager" and "Library". Then choose Drive API and Sheet API
23
+ ![google_drive_api](img/google_drive_api.png)
24
+ 6. Enable "Drive API" and "Sheet API"
25
+ 7. Click "認証情報" and click "認証情報を作成", Then Choose "OAuth Client ID"
26
+ 8. click "同意画面を設定". Input "service name".
27
+ 9. choose "その他" and click "作成"
28
+ 10. click download button. And change file name to "client_secret.json"
29
+ 11. move the file to project folder.
30
+
31
+ 3. execute sample program
32
+ ```bash
33
+ $ ruby test_drive.rb
34
+ ```
35
+ 1. execute test_drive.rb
36
+ 2. Copy URL for authorization on display. And paste browser. And click twice.
37
+ 3. Display "token" on browser. And copy "token", then paste it on console.
38
+ Then display URL for authorization. The URL is copied and paste to URL in browser.Then please copy token code in browser and paste on console display.
39
+
40
+ ## APIs
41
+ ### Drive APIs
42
+ ```ruby
43
+ require 'easy-google-drive'
44
+ myDrive = EasyGoogleDrive::Drive.new
45
+ ```
46
+
47
+ #### cd (change directory)
48
+ command | operation
49
+ --------------------------|-------------------------
50
+ myDrive.cd("directory") | change directory
51
+ myDrive.cd("$") | move to shared folder
52
+ myDrive.cd("~") | move to root foloder of my drive
53
+ myDrive.cd("~/directory")| move to directory on root foloder of my drive
54
+
55
+ #### mkdir (make directory)
56
+ command | operation
57
+ ----------------------------|-------------------------
58
+ myDrive.mkdir("directory") | make directory named "directory"
59
+
60
+ #### rm (remove file)
61
+ command | operation
62
+ --------------------------------|-------------------------
63
+ myDrive.rm("file") | delete file
64
+ myDrive.rm("~/directory/file") | delete file
65
+
66
+ #### get (get file from google drive)
67
+ command | operation
68
+ --------------------------------|-------------------------
69
+ myDrive.get("src","dst") | get "src" in google drive. "dst" is destination file name.
70
+
71
+ #### send (send file to google driver)
72
+ command | operation
73
+ --------------------------------|-------------------------
74
+ myDrive.send("src","dst") | send "src" in local drive and put "dst" in google drive.
75
+
76
+ ### Spreadsheet APIs
77
+ ```ruby
78
+ require 'easy-google-drive'
79
+ mySheet = EasyGoogleDrive::Spreadsheet.new
80
+ ```
81
+ #### open (open file)
82
+ command | operation
83
+ --------------------------------|-------------------------
84
+ mySheet.open("file") | open "file".
85
+
86
+ #### addNewLine(sheet,data) (add data at the bottom line in sheet)
87
+ command | operation
88
+ ----------------------------------|-------------------------
89
+ mySheet.addNewLine("sheet",data) | Add new line. <br>"sheet": sheet name <br>data: Array data.
90
+ (example)<br>
91
+ - mySheet.addNewLine("",[10,100,1000]) :<br>
92
+ add [10,100,1000] to new line in default sheet.<br>
93
+ - mySheet.addNewLine("Sheet2",[10,100,1000]) :<br>
94
+ add [10,100,1000] to new line in "sheet2"
95
+
96
+ #### getData(sheet,range) (open file)
97
+ command | operation
98
+ ----------------------------------|-------------------------
99
+ mySheet.getData("sheet","range") | get data in "range" from "sheet"
100
+ - mySheet.getData("","A:E") :<br>
101
+ get all data of "A:E" from default sheet
102
+ - mySheet.getData("Sheet2","A2:E5") :<br>
103
+ get data in "A2:E5" from "Sheet2"
104
+
105
+
106
+ ## Tips
107
+ The authorization data is stored in "~/.credentials/"
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "easy-google-drive"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,35 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'easy-google-drive/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "easy-google-drive"
8
+ spec.version = EasyGoogleDrive::VERSION
9
+ spec.authors = ["NaotakaSaito"]
10
+ spec.email = ["saitou088@dsn.lapis-semi.com"]
11
+
12
+ spec.summary = %q{"ruby library to use google drive simply and easily."}
13
+ spec.description = %q{"use Drive API V3 and Sheet API V4"}
14
+ spec.homepage = "http://www.lapis-semi.com/lazurite-jp"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org by setting 'allowed_push_host', or
18
+ # delete this section to allow pushing this gem to any host.
19
+ if spec.respond_to?(:metadata)
20
+ #spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against public gem pushes."
23
+ end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
26
+ spec.bindir = "exe"
27
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
+ spec.require_paths = ["lib"]
29
+
30
+ spec.add_development_dependency "bundler", "~> 1.11"
31
+ spec.add_development_dependency "rake", "~> 10.0"
32
+ spec.add_development_dependency "rspec", "~> 3.0"
33
+ spec.add_dependency "google_drive", "~> 2.1"
34
+ spec.add_dependency "termcolor", "~> 1.2"
35
+ end
Binary file
Binary file
@@ -0,0 +1,15 @@
1
+ require 'google/apis/drive_v3'
2
+ require 'googleauth'
3
+ require 'googleauth/stores/file_token_store'
4
+ require 'termcolor'
5
+
6
+ require 'fileutils'
7
+
8
+ module EasyGoogleDrive
9
+ end
10
+
11
+ require_relative 'easy-google-drive/version.rb'
12
+ require_relative 'easy-google-drive/file_base.rb'
13
+ require_relative 'easy-google-drive/file_api.rb'
14
+ require_relative 'easy-google-drive/spreadsheet.rb'
15
+
@@ -0,0 +1,188 @@
1
+
2
+ class EasyGoogleDrive::Drive
3
+ def get(src,dst)
4
+ if dst == "" then
5
+ puts "please enter dst file name"
6
+ end
7
+ tmp_list = []
8
+ tmp_path = []
9
+ tmp_path.push(@root_path.last)
10
+ target_list = list_files(src,tmp_path,tmp_list)
11
+ if target_list == [] then
12
+ puts "cannot file file"
13
+ else
14
+ target_list.each do |file|
15
+ if file.mime_type != 'application/vnd.google-apps.folder' then
16
+ @service.get_file(file.id,{download_dest: dst})
17
+ puts "success to get file::" + file.name + "," + file.id
18
+ return
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+
25
+ def mkdir(name)
26
+ parents = []
27
+ parents.push(@root_path.last.id)
28
+ file_metadata = {
29
+ name: name,
30
+ parents: parents,
31
+ mime_type: 'application/vnd.google-apps.folder',
32
+ }
33
+ @service.create_file(file_metadata, fields: 'id')
34
+ puts "success to create folder:: " + name
35
+ list(@root_path,@current_file_list)
36
+ end
37
+
38
+ def cd(target)
39
+ path_split = target.split("/")
40
+ path_split.each do |folder|
41
+ if directory(folder,@root_path,@current_file_list) == false
42
+ return false
43
+ end
44
+ end
45
+ puts "success to change directory:: "
46
+ pwd()
47
+ end
48
+
49
+ def ls(path=nil)
50
+ tmp_list = []
51
+ tmp_path = []
52
+ tmp_path.push(@root_path.last)
53
+ list = list_files(path,tmp_path,tmp_list)
54
+ if list == false or list == [] then
55
+ puts "file not found"
56
+ else
57
+ # when target is folder, display inside of target
58
+ if list.length == 1 and list[0].mime_type == "application/vnd.google-apps.folder" then
59
+ directory(list[0].name, tmp_path, list)
60
+ end
61
+ list.each do |file|
62
+ if file.mime_type == "application/vnd.google-apps.folder" then
63
+ puts TermColor.parse("<blue>"+file.name+"</blue>")
64
+ else
65
+ puts TermColor.parse(file.name)
66
+ end
67
+ end
68
+ end
69
+ end
70
+
71
+ def rmdir(name)
72
+ list = []
73
+ tmp_path = []
74
+ tmp_path.push(@root_path.last)
75
+ list_files(name,tmp_path,list)
76
+ # filtering only folder
77
+ list.each do |file|
78
+ if file.mime_type != "application/vnd.google-apps.folder" then
79
+ list.delete(file)
80
+ end
81
+ end
82
+ # check inside of folder
83
+ list.each do |folder|
84
+ list2=[]
85
+ tmp_path2 = []
86
+ tmp_path2.push({"name":folder.name,"id":folder.id})
87
+ list(tmp_path2,list2)
88
+ if list2 == [] then
89
+ @service.delete_file(folder[:id])
90
+ puts "success to remove folder:: "+folder[:id]
91
+ else
92
+ puts "cannot remove folder:: "+folder[:id]
93
+ end
94
+ end
95
+ end
96
+ def rm(fname)
97
+ tmp_list = []
98
+ tmp_path = []
99
+ tmp_path.push(@root_path.last)
100
+ list = list_files(fname,tmp_path,tmp_list)
101
+ list.each do |file|
102
+ if file.mime_type != "application/vnd.google-apps.folder" then
103
+ @service.delete_file(file.id)
104
+ puts "success to remove file:: " + file.name + ","+ file.id
105
+ end
106
+ end
107
+ list(@root_path,@current_file_list)
108
+ end
109
+
110
+ def send(src,dst)
111
+ # change directory
112
+ tmp_list = []
113
+ tmp_path = []
114
+ tmp_path.push(@root_path.last)
115
+ list = list_files(dst,tmp_path,tmp_list)
116
+ if list == [] then
117
+ target = dst.split("/").last
118
+ elsif list.length ==1 and list[0].mime_type == "application/vnd.google-apps.folder" then
119
+ directory(list[0].name,tmp_path,tmp_list)
120
+ target = src.split("/").last
121
+ else
122
+ puts "cannot send file"
123
+ end
124
+
125
+ # check last path is directory or filename
126
+ parents = []
127
+ parents.push(tmp_path.last[:id])
128
+ file_metadata = Google::Apis::DriveV3::File.new(
129
+ name: target,
130
+ mine_type: 'application/vnd.google-apps.unknown',
131
+ parents: parents,
132
+ )
133
+ @service.create_file(file_metadata, upload_source: src, fields: 'id')
134
+ msg = "success to send file:: "
135
+ tmp_path.each do |folder|
136
+ if folder[:name] == "root" then
137
+ msg = msg +"~/"
138
+ elsif folder[:name] == "shared" then
139
+ msg = msg + "$/"
140
+ else
141
+ msg = msg + folder[:name] +"/"
142
+ end
143
+ end
144
+ msg += target
145
+ puts msg
146
+ end
147
+ def pwd()
148
+ path = ""
149
+ @root_path.each do |folder|
150
+ if folder[:name] == "root" then
151
+ path = "~"
152
+ elsif folder[:name] == "shared"
153
+ path = "$"
154
+ else
155
+ path = path + "/" + folder[:name]
156
+ end
157
+ end
158
+ puts path
159
+ end
160
+ def help
161
+ puts "EasyGoogleDrive help"
162
+ puts ""
163
+ puts "EasyGoogleDrive::File.cd(""directory"")"
164
+ puts "EasyGoogleDrive::File.cd(""~"")"
165
+ puts " move to root folder"
166
+ puts "EasyGoogleDrive::File.cd(""$"")"
167
+ puts " move to shared folder"
168
+ puts "EasyGoogleDrive::File.cd("".."")"
169
+ puts " move to upper folder"
170
+ puts ""
171
+ puts "EasyGoogleDrive::File.ls()"
172
+ puts " list file and folder in current folder"
173
+ puts ""
174
+
175
+ puts "EasyGoogleDrive::File.get(""src"",""dst"")"
176
+ puts " copy src file in google drive to dst in local file"
177
+ puts " src: source file name in gllgle drive"
178
+ puts " dst: destination file name in local file"
179
+
180
+ puts ""
181
+ puts "EasyGoogleDrive::File.send(""src"",""dst"")"
182
+ puts " copy src file in local drive to dst in google drive"
183
+ puts " src: source file name in local drive"
184
+ puts " dst: destination file name in google file"
185
+
186
+ puts ""
187
+ end
188
+ end
@@ -0,0 +1,246 @@
1
+ class EasyGoogleDrive::Drive
2
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
3
+ APPLICATION_NAME = 'Google Apps Script Execution API Ruby Quickstart'
4
+ CLIENT_SECRETS_PATH = 'client_secret.json'
5
+ CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
6
+ "script-ruby-quickstart.yaml")
7
+ SCOPE = 'https://www.googleapis.com/auth/drive'
8
+ ##
9
+ # Ensure valid credentials, either by restoring from the saved credentials
10
+ # files or intitiating an OAuth2 authorization. If authorization is required,
11
+ # the user's default browser will be launched to approve the request.
12
+ #
13
+ # @return [Google::Auth::UserRefreshCredentials] OAuth2 credentials
14
+ '''
15
+ Supported MIME Types
16
+ You can use MIME types to filter query results or have your app listed in the Chrome Web Store list of apps that can open specific file types.
17
+ The following table lists MIME types that are specific to G Suite and Google Drive.
18
+ MIME Type Description
19
+ application/vnd.google-apps.audio
20
+ application/vnd.google-apps.document Google Docs
21
+ application/vnd.google-apps.drawing Google Drawing
22
+ application/vnd.google-apps.file Google Drive file
23
+ application/vnd.google-apps.folder Google Drive folder
24
+ application/vnd.google-apps.form Google Forms
25
+ application/vnd.google-apps.fusiontable Google Fusion Tables
26
+ application/vnd.google-apps.map Google My Maps
27
+ application/vnd.google-apps.photo
28
+ application/vnd.google-apps.presentation Google Slides
29
+ application/vnd.google-apps.script Google Apps Scripts
30
+ application/vnd.google-apps.sites Google Sites
31
+ application/vnd.google-apps.spreadsheet Google Sheets
32
+ application/vnd.google-apps.unknown
33
+ application/vnd.google-apps.video
34
+ application/vnd.google-apps.drive-sdk 3rd party shortcut
35
+ '''
36
+ def initialize
37
+ @drive = Google::Apis::DriveV3
38
+ @service = @drive::DriveService.new
39
+ @service.client_options.application_name = APPLICATION_NAME
40
+ @service.authorization = authorize
41
+
42
+
43
+ @current_file_list=[]
44
+ @root_path=[]
45
+ root(@root_path,@current_file_list)
46
+
47
+ return
48
+ end
49
+ def authorize
50
+ FileUtils.mkdir_p(File.dirname(CREDENTIALS_PATH))
51
+
52
+ client_id = Google::Auth::ClientId.from_file(CLIENT_SECRETS_PATH)
53
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: CREDENTIALS_PATH)
54
+ authorizer = Google::Auth::UserAuthorizer.new(
55
+ client_id, SCOPE, token_store)
56
+ user_id = 'default'
57
+ credentials = authorizer.get_credentials(user_id)
58
+ if credentials.nil?
59
+ url = authorizer.get_authorization_url(
60
+ base_url: OOB_URI)
61
+ puts "Open the following URL in the browser and enter the " +
62
+ "resulting code after authorization"
63
+ puts url
64
+ code = gets
65
+ credentials = authorizer.get_and_store_credentials_from_code(
66
+ user_id: user_id, code: code, base_url: OOB_URI)
67
+ end
68
+ credentials
69
+ end
70
+
71
+ def root(path,file_list)
72
+ # file list
73
+ page_token = nil
74
+ ref_file = []
75
+ begin
76
+ response = @service.list_files(
77
+ q: "name='gdrive.dat' and trashed = false",
78
+ spaces: 'drive',
79
+ fields: "nextPageToken, files(id, name, parents,kind,mimeType)",
80
+ page_token: page_token)
81
+ for file in response.files
82
+ ref_file.push(file)
83
+ end
84
+ page_token = response.next_page_token
85
+ end while !page_token.nil?
86
+ if ref_file != [] then
87
+ current_folder_id = ref_file[0].parents
88
+ else
89
+ puts "ref file not found"
90
+ send(nil,"gdrive.dat","text/plain")
91
+ begin
92
+ response = @service.list_files(
93
+ q: "name='gdrive.dat'",
94
+ spaces: 'drive',
95
+ fields: "nextPageToken, files(id, name, parents,kind,mimeType)",
96
+ page_token: page_token)
97
+ for file in response.files
98
+ ref_file.push(file)
99
+ end
100
+ page_token = response.next_page_token
101
+ end while !page_token.nil?
102
+ current_folder_id = ref_file[0].parents
103
+ end
104
+
105
+ path.clear
106
+ path.push({"name":"root","id":current_folder_id[0]})
107
+ list(path,file_list)
108
+ return
109
+ end
110
+
111
+ def list_files(target,path,list)
112
+ if target == nil then
113
+ list.clear
114
+ @current_file_list.each do |file|
115
+ list.push(file)
116
+ end
117
+ return list
118
+ end
119
+ target_split = target.split("/")
120
+ if target_split == nil then
121
+ if target == "~" or target == "$" then
122
+ directory(target,path,list)
123
+ return
124
+ else
125
+ list.clear
126
+ @current_file_list.each do |file|
127
+ if file.name == target then
128
+ list.push(file)
129
+ end
130
+ end
131
+ end
132
+ else
133
+ target_file = target_split.last
134
+ target_split.pop()
135
+ end
136
+ tmp_list = []
137
+ if target_split == [] then
138
+ list(path,tmp_list)
139
+ else
140
+ target_split.each do |folder|
141
+ if directory(folder,path,tmp_list) == false then
142
+ return false
143
+ end
144
+ end
145
+ end
146
+ list.clear
147
+ tmp_list.each do |file|
148
+ if file.name == target_file or target_file == "*" then
149
+ list.push(file)
150
+ end
151
+ end
152
+ return list
153
+ end
154
+
155
+ def get_folderid(target,file_list)
156
+ file_list.each do |file|
157
+ if file.mime_type == "application/vnd.google-apps.folder" then
158
+ if file.name == target then
159
+ return file
160
+ end
161
+ end
162
+ end
163
+ return nil
164
+ end
165
+
166
+ def shared(path,list)
167
+ path.clear
168
+ list.clear
169
+ page_token = nil
170
+ begin
171
+ response = @service.list_files(
172
+ q: "trashed = false",
173
+ spaces: 'drive',
174
+ fields: "nextPageToken, files(id, name, parents,kind,mimeType)",
175
+ page_token: page_token)
176
+ for file in response.files
177
+ if file.parents == nil then
178
+ list.push(file)
179
+ end
180
+ end
181
+ page_token = response.next_page_token
182
+ end while !page_token.nil?
183
+ path.push({"name":"shared","id":nil})
184
+ return
185
+ end
186
+
187
+ def list(path,file_list)
188
+ current_folder = path.last
189
+ qmsg = sprintf("""'%s' in parents and trashed=false""",current_folder[:id])
190
+ file_list.clear
191
+
192
+ page_token = nil
193
+ begin
194
+ response = @service.list_files(
195
+ q: qmsg,
196
+ spaces: 'drive',
197
+ fields: "nextPageToken, files(id, name, parents,kind,trashed, mimeType)",
198
+ page_token: page_token)
199
+ for file in response.files
200
+ # Process change
201
+ file_list.push(file)
202
+ end
203
+ page_token = response.next_page_token
204
+ end while !page_token.nil?
205
+ return
206
+ end
207
+ def cd(target)
208
+ path_split = target.split("/")
209
+ path_split.each do |folder|
210
+ if directory(folder,@root_path,@current_file_list) == false
211
+ return false
212
+ end
213
+ end
214
+ end
215
+ def directory(target,path,list)
216
+ if target == ".." then
217
+ if(path.length > 1) then
218
+ path.pop()
219
+ list(path,list)
220
+ else
221
+ puts "root folder"
222
+ return false
223
+ end
224
+ elsif target == "~" then
225
+ root(path,list)
226
+ elsif target == "$"
227
+ shared(path,list)
228
+ elsif target == "."
229
+ list(path,list)
230
+ else
231
+ if list == [] then
232
+ list(path,list)
233
+ end
234
+ newfolder = get_folderid(target,list)
235
+ if newfolder != nil then
236
+ path.push({"name":newfolder.name,"id":newfolder.id})
237
+ list(path,list)
238
+ else
239
+ puts "folder not find:: "+target
240
+ return false
241
+ end
242
+ end
243
+ return true
244
+ end
245
+ end
246
+
@@ -0,0 +1,104 @@
1
+ require 'google/apis/sheets_v4'
2
+
3
+ class EasyGoogleDrive::Spreadsheet < EasyGoogleDrive::Drive
4
+ #OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'
5
+ #APPLICATION_NAME = 'Google Sheets API Ruby Quickstart'
6
+ #CLIENT_SECRETS_PATH = 'client_secret.json'
7
+ #CREDENTIALS_PATH = File.join(Dir.home, '.credentials',
8
+ #"sheets.googleapis.com-ruby-quickstart.yaml")
9
+ #SCOPE = Google::Apis::SheetsV4::AUTH_SPREADSHEETS_READONLY
10
+ #application/vnd.google-apps.spreadsheet Google Sheets
11
+ def open(file)
12
+ tmp_list = []
13
+ tmp_path = []
14
+ tmp_path.push(@root_path.last)
15
+ list = list_files(file,tmp_path,tmp_list)
16
+ if list.length == 1 and list[0].mime_type == "application/vnd.google-apps.spreadsheet" then
17
+ @spreadsheet = {
18
+ file: list[0],
19
+ opened: true,
20
+ }
21
+ init_sheet_api()
22
+ elsif list.length == 1 and list[0].mime_type != "application/vnd.google-apps.spreadsheet" then
23
+ puts "file type of "+ file + "is not spreadsheet."
24
+ puts "the file type is " + spreadsheet_file.mime_type + "."
25
+ @spreadsheet = {}
26
+ return true
27
+ elsif list.length > 1 then
28
+ puts "find "+ list.length + "files."
29
+ @spreadsheet = {}
30
+ return false
31
+ else
32
+ puts "cannot find file. Do you create ? (Yes/No)"
33
+ begin
34
+ data = gets.chop
35
+ if ["Yes","Y","yes","y","YES"].find {|n| n == data}
36
+ continue = false
37
+ # create new file
38
+ name = file.split("/").last
39
+ parents = [tmp_path.last[:id]]
40
+ file_metadata = {
41
+ name: name,
42
+ parents: parents,
43
+ mime_type: 'application/vnd.google-apps.spreadsheet',
44
+ }
45
+ @service.create_file(file_metadata, fields: 'id')
46
+ #get file id
47
+ list = list_files(name,tmp_path,tmp_list)
48
+ @spreadsheet = {
49
+ file: list[0],
50
+ opened: true,
51
+ }
52
+ puts "success to create file:: " + name
53
+ init_sheet_api()
54
+ elsif ["No","N","No","n","NO"].find {|n| n == data}
55
+ continue = false
56
+ data = "no"
57
+ @spreadsheet = {}
58
+ else
59
+ puts "cannot find file. Do you create ? (Yes/No)"
60
+ continue = true
61
+ end
62
+ end while(continue)
63
+ end
64
+ end
65
+ def init_sheet_api()
66
+ service = Google::Apis::SheetsV4::SheetsService.new
67
+ service.client_options.application_name = APPLICATION_NAME
68
+ service.authorization = authorize
69
+ spreadsheet_id = @spreadsheet[:file].id
70
+ @spreadsheet[:service] = service
71
+ end
72
+ def addNewLine(sheet,data)
73
+
74
+ request_body = Google::Apis::SheetsV4::ValueRange.new
75
+ spreadsheet_id = @spreadsheet[:file].id
76
+ if sheet == "" then
77
+ range = "A:"+("A".ord + data.length).chr
78
+ else
79
+ range = sheet
80
+ end
81
+
82
+ value_range_object = {
83
+ majorDimension:"ROWS",
84
+ values: [data],
85
+ }
86
+ update_res = @spreadsheet[:service].append_spreadsheet_value(spreadsheet_id, range, value_range_object, value_input_option: 'USER_ENTERED')
87
+ return update_res
88
+ end
89
+ def getData(sheet,range)
90
+ if sheet == "" then
91
+ get_range = range
92
+ else
93
+ get_range = sheet + "!" + range
94
+ end
95
+ spreadsheet_id = @spreadsheet[:file].id
96
+ response = @spreadsheet[:service].get_spreadsheet_values(spreadsheet_id, get_range)
97
+ return response.values
98
+ end
99
+ def close()
100
+ initialize()
101
+ end
102
+ def help()
103
+ end
104
+ end
@@ -0,0 +1,3 @@
1
+ module EasyGoogleDrive
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'easy-google-drive'
2
+ myDrive = EasyGoogleDrive::Drive.new
3
+
4
+ myDrive.cd("~")
5
+ myDrive.ls()
6
+ myDrive.cd("$")
7
+ myDrive.ls()
8
+ myDrive.cd("~")
9
+ myDrive.send("sample_drive.rb","~/sample_drive.rb")
10
+ myDrive.get("~/gdrive.dat","./gdrive.dat")
11
+ myDrive.ls()
12
+ myDrive.rm("sample_drive.rb")
13
+ myDrive.ls()
@@ -0,0 +1,8 @@
1
+ require 'easy-google-drive'
2
+ mySheet = EasyGoogleDrive::Spreadsheet.new
3
+ mySheet.open("~/sample_spreadsheet")
4
+ p mySheet.addNewLine("",[100,150,200])
5
+ p mySheet.getData("","A:E")
6
+ mySheet.close()
7
+
8
+
metadata ADDED
@@ -0,0 +1,135 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy-google-drive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - NaotakaSaito
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: google_drive
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.1'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.1'
69
+ - !ruby/object:Gem::Dependency
70
+ name: termcolor
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.2'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.2'
83
+ description: '"use Drive API V3 and Sheet API V4"'
84
+ email:
85
+ - saitou088@dsn.lapis-semi.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - Gemfile.lock
96
+ - LICENSE
97
+ - README.md
98
+ - Rakefile
99
+ - bin/console
100
+ - bin/setup
101
+ - easy-google-drive.gemspec
102
+ - img/choose_project.png
103
+ - img/google_drive_api.png
104
+ - lib/easy-google-drive.rb
105
+ - lib/easy-google-drive/file_api.rb
106
+ - lib/easy-google-drive/file_base.rb
107
+ - lib/easy-google-drive/spreadsheet.rb
108
+ - lib/easy-google-drive/version.rb
109
+ - sample/sample_drive.rb
110
+ - sample/sample_spreadsheet.rb
111
+ homepage: http://www.lapis-semi.com/lazurite-jp
112
+ licenses:
113
+ - MIT
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubyforge_project:
131
+ rubygems_version: 2.5.1
132
+ signing_key:
133
+ specification_version: 4
134
+ summary: '"ruby library to use google drive simply and easily."'
135
+ test_files: []