simple_gdrive 0.4.0

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
+ SHA256:
3
+ metadata.gz: 2deb1b8f04443033e24dca718a8fda59d4a5121b5d5e1b48ac1cc7bf54c64bbf
4
+ data.tar.gz: edf2eb26eb05a6e6f150759a5e83fa8b6bf3a688f6d1fc4c96c7ba189b33e118
5
+ SHA512:
6
+ metadata.gz: 927522cd800319aecbebbe3e22569a77faff86512e0d5720265cd8e584a9846a4d3432750951ed2023a90e31dd3617131674b32c425f38e3d7b5de2395fc0b9a
7
+ data.tar.gz: 69c8528403cbc6c836840c2bcd7982e9c031a5a58e1834246f19be38349a6d125fda8f0d191258cb01bd0346b2ce426586f2b414f126d72a11db0917149cd17b
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .ruby-version
13
+ .idea
14
+ Gemfile.lock
15
+ spec/fixtures/client_secrets.json
16
+ test
17
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,12 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.3
5
+ before_install: gem install bundler
6
+ before_script:
7
+ - bundle install
8
+ script: bundle exec rspec
9
+ notifications:
10
+ email:
11
+ on_success: never
12
+ on_failure: always
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in simple_gdrive.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Maxim Tretyakov
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ [![Build Status](https://travis-ci.org/yamax2/simple_gdrive.svg?branch=master)](https://travis-ci.org/yamax2/simple_gdrive)
2
+
3
+ # SimpleGdrive
4
+
5
+ [from this example](https://developers.google.com/drive/v3/web/quickstart/ruby)
6
+
7
+ Simple Google Drive file uploader
8
+
9
+ Creates a required folder and all its parents like `mkdir_p`. For example:
10
+
11
+ ```
12
+ MyMoney/<year>/<month>/<type>/money.csv
13
+ ```
14
+
15
+ ## Installation
16
+
17
+ Add this line to your application's Gemfile:
18
+
19
+ ```ruby
20
+ gem 'simple_gdrive'
21
+ ```
22
+
23
+ And then execute:
24
+
25
+ $ bundle
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install simple_gdrive
30
+
31
+ ## Usage with Rails
32
+
33
+ 1. Create `client_secrets.json` using [this wizard](https://console.developers.google.com/start/api?id=drive)
34
+
35
+ 2. Add the following initializer to `config/initializers`
36
+ ```ruby
37
+ SimpleGdrive.configure do |config|
38
+ config.client_secrets_file = Rails.root.join('config', 'client_secrets.json') # required
39
+ config.base_folder_id = '14lJD-WCxgCd9JxkBnsJktXhw0XrwrsLD' # required
40
+
41
+ config.app_name = 'My app' # optional, default "GDrive Simple Uploader"
42
+ config.credential_file = Rails.root.join('config', 'credentials.yaml') # optional, default ~/.credentials/gdrive-uploader.yaml
43
+ end
44
+ ```
45
+ 3. Start uploading test file from rails console and follow instructions to create `credentials.yaml`.
46
+
47
+ ### File upload
48
+ ```ruby
49
+ SimpleGdrive.upload '2018/01/MyMoney/money.csv', 'money.csv'
50
+ ```
51
+
52
+ folder id:<br>
53
+ ![folder_id](https://mytm.tk/pcmsk/folder_id.png)
54
+
55
+ import local csv to google drive table:
56
+ ```ruby
57
+ CSV.open(tempfile_path, 'w', col_sep: "\t") do |output|
58
+ output << ...
59
+ ...
60
+ end
61
+
62
+ SimpleGdrive.upload 'my/reports/folder/report',
63
+ tempfile_path,
64
+ content_type: 'text/csv',
65
+ mime_type: 'application/vnd.google-apps.spreadsheet'
66
+ ```
67
+
68
+ ### Folder clear
69
+ removes all files and folders in
70
+ ```ruby
71
+ SimpleGdrive.clear
72
+ ```
73
+
74
+ ## Known issues
75
+
76
+ * incorrect behaviour with cyrillic symbols in filenames:
77
+ ```ruby
78
+ SimpleGdrive.upload 'Расходы/01/money.csv', 'money.csv'
79
+ ```
80
+
81
+ ## License
82
+
83
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
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 "simple_gdrive"
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(__FILE__)
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,48 @@
1
+ require 'simple_gdrive/version'
2
+ require 'simple_gdrive/base'
3
+ require 'simple_gdrive/uploader'
4
+ require 'simple_gdrive/cleaner'
5
+
6
+ module SimpleGdrive
7
+ Config = Struct.new(
8
+ :base_folder_id,
9
+ :app_name,
10
+ :client_secrets_file,
11
+ :credential_file
12
+ )
13
+
14
+ def self.config
15
+ @config ||= Config.new.tap do |config|
16
+ config.base_folder_id = '14lJD-WCxgCd9JxkBnsJktXhw0XrwrsLD'
17
+ config.app_name = 'GDrive Simple Uploader'
18
+ config.client_secrets_file = 'client_secrets.json'
19
+ config.credential_file = File.join(
20
+ Dir.home,
21
+ '.credentials',
22
+ 'gdrive-uploader.yaml'
23
+ )
24
+ end
25
+ end
26
+
27
+ def self.configure
28
+ yield config
29
+ end
30
+
31
+ def self.upload(full_filename, upload_source, content_type: 'text/plain', mime_type: nil)
32
+ Uploader.new(
33
+ app_name: config.app_name,
34
+ base_folder_id: config.base_folder_id,
35
+ credential_file: config.credential_file,
36
+ client_secrets_file: config.client_secrets_file
37
+ ).call(full_filename, upload_source, content_type: content_type, mime_type: mime_type)
38
+ end
39
+
40
+ def self.clear
41
+ Cleaner.new(
42
+ app_name: config.app_name,
43
+ base_folder_id: config.base_folder_id,
44
+ credential_file: config.credential_file,
45
+ client_secrets_file: config.client_secrets_file
46
+ ).call
47
+ end
48
+ end
@@ -0,0 +1,63 @@
1
+ require 'fileutils'
2
+
3
+ require 'google/apis/drive_v3'
4
+ require 'googleauth'
5
+ require 'googleauth/stores/file_token_store'
6
+
7
+ module SimpleGdrive
8
+ class Base
9
+ OOB_URI = 'urn:ietf:wg:oauth:2.0:oob'.freeze
10
+ USER_ID = 'default'.freeze
11
+ FOLDER_MIME_TYPE = 'application/vnd.google-apps.folder'.freeze
12
+ OPTIONS = {retries: 5}.freeze
13
+
14
+ def initialize(app_name:, base_folder_id:, credential_file:, client_secrets_file:)
15
+ @app_name = app_name
16
+ @base_folder_id = base_folder_id
17
+ @credential_file = credential_file
18
+ @client_secrets_file = client_secrets_file
19
+ end
20
+
21
+ def service
22
+ @service ||= Google::Apis::DriveV3::DriveService.new.tap do |service|
23
+ service.client_options.application_name = @app_name
24
+ service.authorization = credentials
25
+ end
26
+ end
27
+
28
+ private
29
+
30
+ def auth_request(authorizer)
31
+ url = authorizer.get_authorization_url(base_url: OOB_URI)
32
+
33
+ puts 'Open the following URL in the browser and enter the '\
34
+ 'resulting code after authorization'
35
+
36
+ puts url
37
+ code = gets
38
+
39
+ authorizer.get_and_store_credentials_from_code(
40
+ user_id: USER_ID,
41
+ code: code,
42
+ base_url: OOB_URI
43
+ )
44
+ end
45
+
46
+ def credentials
47
+ return @credentials if defined?(@credentials)
48
+
49
+ FileUtils.mkdir_p File.dirname(@credential_file)
50
+
51
+ client_id = Google::Auth::ClientId.from_file(@client_secrets_file)
52
+ token_store = Google::Auth::Stores::FileTokenStore.new(file: @credential_file)
53
+
54
+ authorizer = Google::Auth::UserAuthorizer.new(
55
+ client_id,
56
+ Google::Apis::DriveV3::AUTH_DRIVE,
57
+ token_store
58
+ )
59
+
60
+ @credentials = authorizer.get_credentials(USER_ID) || auth_request(authorizer)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,36 @@
1
+ module SimpleGdrive
2
+ # Cleans the directory
3
+ # Returns array of removed file names
4
+ class Cleaner < Base
5
+ def call
6
+ @page_token = nil
7
+ @files_to_remove = {}
8
+
9
+ begin
10
+ fetch_batch
11
+ end until @page_token.nil?
12
+
13
+ remove_files
14
+
15
+ @files_to_remove.values
16
+ end
17
+
18
+ private
19
+
20
+ def fetch_batch
21
+ response = service.list_files(
22
+ q: "'#{@base_folder_id}' in parents and not trashed",
23
+ fields: 'nextPageToken, files(id, name, parents)',
24
+ page_token: @page_token
25
+ )
26
+
27
+ @page_token = response.next_page_token
28
+
29
+ response.files.each_with_object(@files_to_remove) { |file, memo| memo[file.id] = file.name }
30
+ end
31
+
32
+ def remove_files
33
+ @files_to_remove.keys.each { |id| service.delete_file(id) }
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,47 @@
1
+ module SimpleGdrive
2
+ # Uploads file
3
+ class Uploader < Base
4
+ def call(full_filename, upload_source, content_type:, mime_type: nil)
5
+ names = full_filename.split('/')
6
+ filename = names.pop
7
+ parent_id = find_folder(names)
8
+
9
+ meta = {name: filename, parents: [parent_id]}
10
+ meta[:mime_type] = mime_type if mime_type
11
+
12
+ file = service.create_file(
13
+ meta,
14
+ upload_source: upload_source,
15
+ content_type: content_type,
16
+ options: OPTIONS
17
+ )
18
+
19
+ {id: file.id, parent_id: parent_id}
20
+ end
21
+
22
+ private
23
+
24
+ def find_folder(names)
25
+ id = @base_folder_id
26
+
27
+ names.each { |folder_name| id = find_or_create_folder(folder_name, id) }
28
+
29
+ id
30
+ end
31
+
32
+ def find_or_create_folder(name, parent_id = nil)
33
+ folder_id = parent_id || @base_folder_id
34
+
35
+ res = service.list_files(
36
+ q: "mimeType='#{FOLDER_MIME_TYPE}' and name='#{name}' and '#{folder_id}' in parents and not trashed"
37
+ )
38
+
39
+ return res.files.first.id if res.files.any?
40
+
41
+ service.create_file(
42
+ {name: name, mime_type: FOLDER_MIME_TYPE, parents: [folder_id]},
43
+ options: OPTIONS
44
+ ).id
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,3 @@
1
+ module SimpleGdrive
2
+ VERSION = '0.4.0'.freeze
3
+ end
@@ -0,0 +1,34 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'simple_gdrive/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'simple_gdrive'
8
+ spec.version = SimpleGdrive::VERSION
9
+ spec.authors = ['Maxim Tretyakov']
10
+ spec.email = ['max@tretyakov-ma.ru']
11
+
12
+ spec.summary = %q{Simple Google Drive file uploader}
13
+ spec.description = %q{Simple Google Drive file uploader with autocreating required folders}
14
+ spec.homepage = 'https://github.com/yamax2/simple_gdrive'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_runtime_dependency 'google-api-client', '>= 0.24.3'
26
+
27
+ spec.add_development_dependency 'bundler'
28
+ spec.add_development_dependency 'pry-byebug'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec', '~> 3.0'
31
+ spec.add_development_dependency 'shoulda-matchers'
32
+ spec.add_development_dependency 'vcr'
33
+ spec.add_development_dependency 'webmock'
34
+ end
metadata ADDED
@@ -0,0 +1,171 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple_gdrive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.4.0
5
+ platform: ruby
6
+ authors:
7
+ - Maxim Tretyakov
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-10-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: google-api-client
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 0.24.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 0.24.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: pry-byebug
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: shoulda-matchers
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: vcr
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: webmock
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description: Simple Google Drive file uploader with autocreating required folders
126
+ email:
127
+ - max@tretyakov-ma.ru
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/simple_gdrive.rb
142
+ - lib/simple_gdrive/base.rb
143
+ - lib/simple_gdrive/cleaner.rb
144
+ - lib/simple_gdrive/uploader.rb
145
+ - lib/simple_gdrive/version.rb
146
+ - simple_gdrive.gemspec
147
+ homepage: https://github.com/yamax2/simple_gdrive
148
+ licenses:
149
+ - MIT
150
+ metadata: {}
151
+ post_install_message:
152
+ rdoc_options: []
153
+ require_paths:
154
+ - lib
155
+ required_ruby_version: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ required_rubygems_version: !ruby/object:Gem::Requirement
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ version: '0'
165
+ requirements: []
166
+ rubyforge_project:
167
+ rubygems_version: 2.7.6
168
+ signing_key:
169
+ specification_version: 4
170
+ summary: Simple Google Drive file uploader
171
+ test_files: []