gdrivestrg 0.0.1

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 (44) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.rdoc +3 -0
  3. data/Rakefile +38 -0
  4. data/lib/gdrivestrg/gdrivestrg.rb +143 -0
  5. data/lib/gdrivestrg/gdrivestrg.rb~ +143 -0
  6. data/lib/gdrivestrg/version.rb +3 -0
  7. data/lib/gdrivestrg.rb +2 -0
  8. data/lib/generators/cloudstrg_generator.rb~ +19 -0
  9. data/lib/generators/dropboxstrg_generator.rb~ +19 -0
  10. data/lib/generators/gdrivestrg_generator.rb +16 -0
  11. data/lib/generators/gdrivestrg_generator.rb~ +16 -0
  12. data/lib/tasks/gdrivestrg_tasks.rake +4 -0
  13. data/lib/templates/create_gdrivestrgfields.rb +17 -0
  14. data/test/dummy/README.rdoc +261 -0
  15. data/test/dummy/Rakefile +7 -0
  16. data/test/dummy/app/assets/javascripts/application.js +15 -0
  17. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  18. data/test/dummy/app/controllers/application_controller.rb +3 -0
  19. data/test/dummy/app/helpers/application_helper.rb +2 -0
  20. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  21. data/test/dummy/config/application.rb +59 -0
  22. data/test/dummy/config/boot.rb +10 -0
  23. data/test/dummy/config/database.yml +25 -0
  24. data/test/dummy/config/environment.rb +5 -0
  25. data/test/dummy/config/environments/development.rb +37 -0
  26. data/test/dummy/config/environments/production.rb +67 -0
  27. data/test/dummy/config/environments/test.rb +37 -0
  28. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  29. data/test/dummy/config/initializers/inflections.rb +15 -0
  30. data/test/dummy/config/initializers/mime_types.rb +5 -0
  31. data/test/dummy/config/initializers/secret_token.rb +7 -0
  32. data/test/dummy/config/initializers/session_store.rb +8 -0
  33. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  34. data/test/dummy/config/locales/en.yml +5 -0
  35. data/test/dummy/config/routes.rb +58 -0
  36. data/test/dummy/config.ru +4 -0
  37. data/test/dummy/public/404.html +26 -0
  38. data/test/dummy/public/422.html +26 -0
  39. data/test/dummy/public/500.html +25 -0
  40. data/test/dummy/public/favicon.ico +0 -0
  41. data/test/dummy/script/rails +6 -0
  42. data/test/gdrivestrg_test.rb +7 -0
  43. data/test/test_helper.rb +15 -0
  44. metadata +166 -0
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2012 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Gdrivestrg
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'Gdrivestrg'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+
24
+
25
+
26
+ Bundler::GemHelper.install_tasks
27
+
28
+ require 'rake/testtask'
29
+
30
+ Rake::TestTask.new(:test) do |t|
31
+ t.libs << 'lib'
32
+ t.libs << 'test'
33
+ t.pattern = 'test/**/*_test.rb'
34
+ t.verbose = false
35
+ end
36
+
37
+
38
+ task :default => :test
@@ -0,0 +1,143 @@
1
+ require 'cloudstrg/cloudstrg'
2
+
3
+ class GdriveStrg < CloudStrg::CloudStorage
4
+ require 'google/api_client'
5
+
6
+ CLIENT_ID = "867108581948.apps.googleusercontent.com"
7
+ CLIENT_SECRET = "Z3TXaBvx36ex8RRD-Wu-3PGK"
8
+ SCOPES = [
9
+ 'https://www.googleapis.com/auth/drive.file', # we will only see the files createdby this app
10
+ 'https://www.googleapis.com/auth/userinfo.email',
11
+ 'https://www.googleapis.com/auth/userinfo.profile'
12
+ ]
13
+
14
+ def initialize params
15
+ @client = Google::APIClient.new
16
+ @drive_api = @client.discovered_api('drive', 'v2')
17
+ @auth_api = @client.discovered_api('oauth2', 'v2')
18
+
19
+ @client.authorization.client_id = CLIENT_ID
20
+ @client.authorization.client_secret = CLIENT_SECRET
21
+ #@client.authorization.redirect_uri = @secrets.redirect_uris.first
22
+ @client.authorization.scope = SCOPES
23
+ end
24
+
25
+ def config params
26
+ @client.authorization.redirect_uri = params[:redirect]
27
+
28
+ @username = params[:username]
29
+ user = Cloudstrguser.find_by_name(@username)
30
+
31
+ session = params[:session]
32
+
33
+ @client.authorization.update_token!(:access_token => session[:gdrive_access_token] , #:access_token => user.gdrive_access_token,
34
+ :refresh_token => user.gdrive_refresh_token,
35
+ :expires_in => user.gdrive_expires_in,
36
+ :issued_at => user.gdrive_issued_at)
37
+ if @client.authorization.refresh_token && @client.authorization.expired?
38
+ @client.authorization.fetch_access_token!
39
+ session[:gdrive_access_token] = @client.authorization.access_token
40
+ # user.gdrive_access_token = @client.authorization.access_token
41
+ user.gdrive_refresh_token = @client.authorization.refresh_token
42
+ user.gdrive_expires_in = @client.authorization.expires_in
43
+ user.gdrive_issued_at = @client.authorization.issued_at
44
+ user.save()
45
+ end
46
+
47
+
48
+ if params[:code]
49
+ authorize_code(params[:code])
50
+
51
+ session[:gdrive_access_token] = @client.authorization.access_token
52
+ # user.gdrive_access_token = @client.authorization.access_token
53
+ user.gdrive_refresh_token = @client.authorization.refresh_token
54
+ user.gdrive_expires_in = @client.authorization.expires_in
55
+ user.gdrive_issued_at = @client.authorization.issued_at
56
+ user.save()
57
+ elsif params[:error] # User denied the oauth grant
58
+ puts "Denied: #{params[:error]}"
59
+ end
60
+
61
+ if not authorized?
62
+ #user.gdrive_refresh_token = @client.authorization.refresh_token
63
+ #user.save
64
+ return session, auth_url
65
+ end
66
+ return session, false
67
+ end
68
+
69
+ def create_file params
70
+ filename = params[:filename]
71
+ filename += ".json" if not filename.include? ".json"
72
+
73
+ file = @drive_api.files.insert.request_schema.new({'title' => filename, 'description' => 'Netlab scenario', 'mimeType' => 'text/json'})
74
+ media=Google::APIClient::UploadIO.new(StringIO.new(params[:file_content]), 'text/json')
75
+ r = @client.execute(:api_method => @drive_api.files.insert, :body_object => file, :media => media, :parameters => {'uploadType' => 'multipart', 'alt' => 'json'})
76
+ if r.status != 200
77
+ return false
78
+ end
79
+ true
80
+ end
81
+
82
+ def create_folder params
83
+ end
84
+
85
+ def get_file params
86
+ r = @client.execute!(:api_method => @drive_api.files.get, :parameters => {'fileId' => params[:fileid]})
87
+ if r.status != 200
88
+ return nil, nil, nil
89
+ end
90
+ filename = r.data.title
91
+ r = @client.execute!(:uri => r.data.download_url)
92
+ if r.status != 200
93
+ return nil, nil, nil
94
+ end
95
+
96
+ return filename, params[:fileid], r.body
97
+ end
98
+
99
+ def update_file params
100
+ filename = params[:filename]
101
+ filename += ".json" if not filename.include? ".json"
102
+
103
+ file = @drive_api.files.insert.request_schema.new({'title' => filename, 'description' => 'Netlab scenario', 'mimeType' => 'text/json'})
104
+ media=Google::APIClient::UploadIO.new(StringIO.new(params[:file_content]), 'text/json')
105
+ r = @client.execute(:api_method => @drive_api.files.update, :body_object => file, :media => media, :parameters => {'fileId' => params[:fileid], 'uploadType' => 'multipart', 'alt' => 'json'})
106
+ if r.status != 200
107
+ return false
108
+ end
109
+ true
110
+ end
111
+
112
+ def remove_file params
113
+ r = @client.execute!(:api_method => @drive_api.files.delete, :parameters => {'fileId' => params[:fileid]})
114
+ end
115
+
116
+ def list_files
117
+ r=@client.execute!(:api_method => @drive_api.files.list)
118
+ if r.status != 200
119
+ return []
120
+ end
121
+
122
+ lines = []
123
+ r.data.items.each do |line|
124
+ lines.append([line.title, line.id]) if line.title.include? ".json"
125
+ end
126
+ return lines
127
+ end
128
+
129
+
130
+
131
+ def authorized?
132
+ return @client.authorization.refresh_token && @client.authorization.access_token
133
+ end
134
+
135
+ def authorize_code(authorization_code)
136
+ @client.authorization.code = authorization_code
137
+ @client.authorization.fetch_access_token!
138
+ end
139
+
140
+ def auth_url(state = '')
141
+ return @client.authorization.authorization_uri().to_s
142
+ end
143
+ end
@@ -0,0 +1,143 @@
1
+ require 'cloudstrg/cloudstrg'
2
+
3
+ class GdriveStorage < CloudStrg::CloudStorage
4
+ require 'google/api_client'
5
+
6
+ CLIENT_ID = "867108581948.apps.googleusercontent.com"
7
+ CLIENT_SECRET = "Z3TXaBvx36ex8RRD-Wu-3PGK"
8
+ SCOPES = [
9
+ 'https://www.googleapis.com/auth/drive.file', # we will only see the files createdby this app
10
+ 'https://www.googleapis.com/auth/userinfo.email',
11
+ 'https://www.googleapis.com/auth/userinfo.profile'
12
+ ]
13
+
14
+ def initialize params
15
+ @client = Google::APIClient.new
16
+ @drive_api = @client.discovered_api('drive', 'v2')
17
+ @auth_api = @client.discovered_api('oauth2', 'v2')
18
+
19
+ @client.authorization.client_id = CLIENT_ID
20
+ @client.authorization.client_secret = CLIENT_SECRET
21
+ #@client.authorization.redirect_uri = @secrets.redirect_uris.first
22
+ @client.authorization.scope = SCOPES
23
+ end
24
+
25
+ def config params
26
+ @client.authorization.redirect_uri = params[:redirect]
27
+
28
+ @username = params[:username]
29
+ user = Cloudstrguser.find_by_name(@username)
30
+
31
+ session = params[:session]
32
+
33
+ @client.authorization.update_token!(:access_token => session[:gdrive_access_token] , #:access_token => user.gdrive_access_token,
34
+ :refresh_token => user.gdrive_refresh_token,
35
+ :expires_in => user.gdrive_expires_in,
36
+ :issued_at => user.gdrive_issued_at)
37
+ if @client.authorization.refresh_token && @client.authorization.expired?
38
+ @client.authorization.fetch_access_token!
39
+ session[:gdrive_access_token] = @client.authorization.access_token
40
+ # user.gdrive_access_token = @client.authorization.access_token
41
+ user.gdrive_refresh_token = @client.authorization.refresh_token
42
+ user.gdrive_expires_in = @client.authorization.expires_in
43
+ user.gdrive_issued_at = @client.authorization.issued_at
44
+ user.save()
45
+ end
46
+
47
+
48
+ if params[:code]
49
+ authorize_code(params[:code])
50
+
51
+ session[:gdrive_access_token] = @client.authorization.access_token
52
+ # user.gdrive_access_token = @client.authorization.access_token
53
+ user.gdrive_refresh_token = @client.authorization.refresh_token
54
+ user.gdrive_expires_in = @client.authorization.expires_in
55
+ user.gdrive_issued_at = @client.authorization.issued_at
56
+ user.save()
57
+ elsif params[:error] # User denied the oauth grant
58
+ puts "Denied: #{params[:error]}"
59
+ end
60
+
61
+ if not authorized?
62
+ #user.gdrive_refresh_token = @client.authorization.refresh_token
63
+ #user.save
64
+ return session, auth_url
65
+ end
66
+ return session, false
67
+ end
68
+
69
+ def create_file params
70
+ filename = params[:filename]
71
+ filename += ".json" if not filename.include? ".json"
72
+
73
+ file = @drive_api.files.insert.request_schema.new({'title' => filename, 'description' => 'Netlab scenario', 'mimeType' => 'text/json'})
74
+ media=Google::APIClient::UploadIO.new(StringIO.new(params[:file_content]), 'text/json')
75
+ r = @client.execute(:api_method => @drive_api.files.insert, :body_object => file, :media => media, :parameters => {'uploadType' => 'multipart', 'alt' => 'json'})
76
+ if r.status != 200
77
+ return false
78
+ end
79
+ true
80
+ end
81
+
82
+ def create_folder params
83
+ end
84
+
85
+ def get_file params
86
+ r = @client.execute!(:api_method => @drive_api.files.get, :parameters => {'fileId' => params[:fileid]})
87
+ if r.status != 200
88
+ return nil, nil, nil
89
+ end
90
+ filename = r.data.title
91
+ r = @client.execute!(:uri => r.data.download_url)
92
+ if r.status != 200
93
+ return nil, nil, nil
94
+ end
95
+
96
+ return filename, params[:fileid], r.body
97
+ end
98
+
99
+ def update_file params
100
+ filename = params[:filename]
101
+ filename += ".json" if not filename.include? ".json"
102
+
103
+ file = @drive_api.files.insert.request_schema.new({'title' => filename, 'description' => 'Netlab scenario', 'mimeType' => 'text/json'})
104
+ media=Google::APIClient::UploadIO.new(StringIO.new(params[:file_content]), 'text/json')
105
+ r = @client.execute(:api_method => @drive_api.files.update, :body_object => file, :media => media, :parameters => {'fileId' => params[:fileid], 'uploadType' => 'multipart', 'alt' => 'json'})
106
+ if r.status != 200
107
+ return false
108
+ end
109
+ true
110
+ end
111
+
112
+ def remove_file params
113
+ r = @client.execute!(:api_method => @drive_api.files.delete, :parameters => {'fileId' => params[:fileid]})
114
+ end
115
+
116
+ def list_files
117
+ r=@client.execute!(:api_method => @drive_api.files.list)
118
+ if r.status != 200
119
+ return []
120
+ end
121
+
122
+ lines = []
123
+ r.data.items.each do |line|
124
+ lines.append([line.title, line.id]) if line.title.include? ".json"
125
+ end
126
+ return lines
127
+ end
128
+
129
+
130
+
131
+ def authorized?
132
+ return @client.authorization.refresh_token && @client.authorization.access_token
133
+ end
134
+
135
+ def authorize_code(authorization_code)
136
+ @client.authorization.code = authorization_code
137
+ @client.authorization.fetch_access_token!
138
+ end
139
+
140
+ def auth_url(state = '')
141
+ return @client.authorization.authorization_uri().to_s
142
+ end
143
+ end
@@ -0,0 +1,3 @@
1
+ module Gdrivestrg
2
+ VERSION = "0.0.1"
3
+ end
data/lib/gdrivestrg.rb ADDED
@@ -0,0 +1,2 @@
1
+ module Gdrivestrg
2
+ end
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class CloudstrgGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+
8
+ def self.next_migration_number(path)
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
10
+ end
11
+
12
+ def create_model_file
13
+ template "cloudstrglist.rb", "app/models/cloudstrglist.rb"
14
+ template "cloudstrguser.rb", "app/models/cloudstrguser.rb"
15
+ migration_template "create_cloudstrglists.rb", "db/migrate/create_cloudstrglists.rb"
16
+ migration_template "create_cloudstrgusers.rb", "db/migrate/create_cloudstrguser.rb"
17
+ end
18
+ end
19
+
@@ -0,0 +1,19 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class CloudstrgGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+
8
+ def self.next_migration_number(path)
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
10
+ end
11
+
12
+ def create_model_file
13
+ template "cloudstrglist.rb", "app/models/cloudstrglist.rb"
14
+ template "cloudstrguser.rb", "app/models/cloudstrguser.rb"
15
+ migration_template "create_cloudstrglists.rb", "db/migrate/create_cloudstrglists.rb"
16
+ migration_template "create_cloudstrgusers.rb", "db/migrate/create_cloudstrgusers.rb"
17
+ end
18
+ end
19
+
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class GdrivestrgGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+
8
+ def self.next_migration_number(path)
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
10
+ end
11
+
12
+ def create_model_file
13
+ migration_template "create_gdrivestrgfields.rb", "db/migrate/create_gdrivestrgfields.rb"
14
+ end
15
+ end
16
+
@@ -0,0 +1,16 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ class CloudstrgGenerator < Rails::Generators::Base
5
+ include Rails::Generators::Migration
6
+ source_root File.expand_path('../../templates', __FILE__)
7
+
8
+ def self.next_migration_number(path)
9
+ Time.now.utc.strftime("%Y%m%d%H%M%S%6N")
10
+ end
11
+
12
+ def create_model_file
13
+ migration_template "create_dropboxstrgfields.rb", "db/migrate/create_dropboxstrgfields.rb"
14
+ end
15
+ end
16
+
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :gdrivestrg do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,17 @@
1
+ class CreateGdrivestrgfields < ActiveRecord::Migration
2
+ def up
3
+ add_column :cloudstrgusers, :gdrive_refresh_token, :string
4
+ add_column :cloudstrgusers, :gdrive_expires_in, :integer
5
+ add_column :cloudstrgusers, :gdrive_issued_at, :date
6
+
7
+ Cloudstrglist.create :plugin_name => "gdrive"
8
+ end
9
+
10
+ def down
11
+ remove_column :cloudstrgusers, :gdrive_refresh_token
12
+ remove_column :cloudstrgusers, :gdrive_expires_in
13
+ remove_column :cloudstrgusers, :gdrive_issued_at
14
+
15
+ Cloudstrglist.delete_all :plugin_name => "gdrive"
16
+ end
17
+ end