activerecord_to_google_spreadsheet 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e1d3dff602a73f423926bda7306c3384bb2ea05c
4
+ data.tar.gz: f651821d37ab9f50bf76133a68a31552d1ebdc2e
5
+ SHA512:
6
+ metadata.gz: f5156a3453a1fa69836f4f8db201903311bc43ce07da466e0595aa7e72e602f19763ae2051499e9812fde974e3748878f451ef948efe7901ff47ac532b3cb96b
7
+ data.tar.gz: 6220188532d12a71e8bccd1bad2a8c6b6feb1a29b268f1abba1607e75358c4ec861f6079888d94f88fad145acf7bde0bc86ab1966cc8819d301117bddfcd3356
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -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 TODO: Write your email address. 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,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in activerecord_to_google_spreadsheet.gemspec
4
+ gemspec :name => 'activerecord_to_google_spreadsheet'
5
+ gem 'google_drive'
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 TODO: Write your name
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,75 @@
1
+ # ActiverecordToGoogleSpreadsheet
2
+
3
+ This is a rails gem which can help you dump or export models to google spreadsheet easily.
4
+
5
+ TODO: Apply to_google_spreadsheet to ARRAY
6
+
7
+ ## Installation
8
+
9
+ 1. Add this line to your application's Gemfile:
10
+ ```sh
11
+ gem 'google_drive'
12
+ gem 'activerecord_to_google_spreadsheet'
13
+ ```
14
+ 2. Add file to config/initializers/activerecord_to_google_spreadsheet.rb
15
+ ```sh
16
+ require 'activerecord_to_google_spreadsheet'
17
+ ActiveRecordToGoogleSpreadsheet.configure do |config|
18
+ config.client_id = "$GOOGLE_CLIENT_ID"
19
+ config.client_secret = "$GOOGLE_CLIENT_SECRET"
20
+ config.redirect_uri = "$CALLBACK_URL"
21
+ end
22
+ ```
23
+ And then execute:
24
+
25
+ $ bundle install
26
+
27
+ Or install it yourself as:
28
+
29
+ $ gem install activerecord_to_google_spreadsheet
30
+
31
+ ## Usage
32
+
33
+ Then you can use following api.
34
+ ```sh
35
+ ActiveRecordToGoogleSpreadsheet.dump_to_spreadsheet(session, "$SPREADSHEET_KEY")
36
+ ```
37
+ Then you can see your databases show on https://docs.google.com/spreadsheets/d/$SPREADSHEET_KEY/
38
+
39
+ You can also restore databases from google spreadsheet by following api.
40
+ ```sh
41
+ ActiveRecordToGoogleSpreadsheet.restore_from_spreadsheet(session, "$SPREADSHEET_KEY")
42
+ ```
43
+
44
+ Then your databases will apply data from google spreadsheet.
45
+
46
+ There are some other api like this.
47
+ ```sh
48
+ Product.where('price < 50').to_google_spreadsheet(session, "$SPREADSHEET_KEY", row_offset: 10, worksheet_title: true)
49
+ ```
50
+
51
+ The variable session is GoogleDrive::Session.
52
+
53
+ You can follow the following url to get GoogleDrive::Session.
54
+
55
+ https://github.com/gimite/google-drive-ruby
56
+
57
+ Or ActiveRecordToGoogleSpreadsheet provide other way to get session.
58
+ ```sh
59
+ //You can use the following code to login google account to grant auth code
60
+ redirect_to ActiveRecordToGoogleSpreadsheet.google_login_url
61
+
62
+ //Then put following code to the callback url controller function
63
+ ActiveRecordToGoogleSpreadsheet.setup_session(params['code'])
64
+
65
+ //Then you can get session now.
66
+ session = ActiveRecordToGoogleSpreadsheet.get_session()
67
+ ```
68
+
69
+ Good Luck.
70
+
71
+
72
+ ## License
73
+
74
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
75
+
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'activerecord_to_google_spreadsheet/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "activerecord_to_google_spreadsheet"
8
+ spec.version = ActiverecordToGoogleSpreadsheet::VERSION
9
+ spec.authors = ["Otis Chen"]
10
+ spec.email = ["kkooff2000@gmail.com"]
11
+
12
+ spec.summary = %q{ActiveRecord to Google SpreadSheets.}
13
+ spec.description = %q{Dump databases to Google SpreadSheets and can also from SpreadSheets.}
14
+ spec.homepage = "https://github.com/kkooff2000/activerecord_to_google_spreadsheet"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.12"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "activerecord_to_google_spreadsheet"
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,178 @@
1
+ require "activerecord_to_google_spreadsheet/version"
2
+ require "activerecord_to_google_spreadsheet/utils"
3
+ require "google_drive"
4
+ module ActiveRecordToGoogleSpreadsheet
5
+ extend Utils
6
+
7
+ class << self
8
+ attr_accessor :configuration
9
+ end
10
+
11
+ class Configuration
12
+ attr_accessor :client_id, :client_secret, :redirect_uri
13
+
14
+ def initialize
15
+
16
+ end
17
+ end
18
+
19
+ def self.configure
20
+ self.configuration ||= Configuration.new
21
+ yield self.configuration
22
+ end
23
+
24
+ def self.get_db_info
25
+ info = {}
26
+ i = 1
27
+ ActiveRecord::Base.connection.tables.map do |model|
28
+ if i == 1
29
+ i += 1
30
+ next
31
+ end
32
+ clazz = model.capitalize.singularize.camelize.to_s.constantize
33
+ info[model.capitalize.singularize.camelize.to_s] = clazz.column_names
34
+ i += 1
35
+ end
36
+ return info
37
+ end
38
+
39
+ def self.dump_to_spreadsheet(session, spreadsheet_key)
40
+ self.each_tables(session, spreadsheet_key) do |name, value, ws, clazz, info|
41
+
42
+ info[name].each_with_index do |val, index|
43
+ ws[1, index+1] = val
44
+ end
45
+
46
+ clazz.all.each_with_index do |val, index|
47
+ clazz.column_names.each_with_index do |col, i|
48
+ ws[index+2, i+1] = val[col]
49
+ end
50
+ end
51
+ ws.save
52
+ ws.reload
53
+ end
54
+ end
55
+
56
+ def self.restore_from_spreadsheet(session, spreadsheet_key)
57
+ self.each_tables(session, spreadsheet_key) do |name, value, ws, clazz, info|
58
+ clazz = name.capitalize.singularize.camelize.to_s.constantize
59
+
60
+ (2..ws.num_rows).each do |row|
61
+ c = clazz.find_by_id(ws[row, 1])
62
+ clazz.column_names.each_with_index do |col, i|
63
+ if col == 'id' || col == 'created_at' || col == 'updated_at'
64
+ next
65
+ end
66
+ c[col] = ws[row, i+1]
67
+ end
68
+ c.save
69
+ end
70
+ end
71
+ end
72
+
73
+ def self.setup_session(code)
74
+ if $session
75
+ return $session
76
+ end
77
+ auth = get_auth
78
+ auth.code = code
79
+ auth.fetch_access_token!
80
+ $session = GoogleDrive.login_with_oauth(auth.access_token)
81
+ return $session
82
+ end
83
+
84
+ def self.get_session
85
+ if $session
86
+ return $session
87
+ else
88
+ raise GetSessionException
89
+ end
90
+ end
91
+
92
+ class GetSessionException < Exception
93
+ def message
94
+ "call setup_session(code) before get_session"
95
+ end
96
+ end
97
+
98
+ def self.get_auth
99
+ client = Google::APIClient.new
100
+ auth = client.authorization
101
+ auth.client_id = self.configuration.client_id
102
+ auth.client_secret = self.configuration.client_secret
103
+ auth.scope =
104
+ "https://www.googleapis.com/auth/drive " +
105
+ "https://spreadsheets.google.com/feeds/"
106
+ auth.redirect_uri = self.configuration.redirect_uri
107
+ return auth
108
+ end
109
+
110
+ def self.google_login_url
111
+ auth = get_auth
112
+ auth_url = auth.authorization_uri
113
+ return auth_url.to_s
114
+ end
115
+
116
+ module ActiveRecordBaseConverter
117
+ def to_google_spreadsheet
118
+ puts '****************************base test************'
119
+ clazz = self.model_name.name.capitalize.singularize.camelize.to_s.constantize
120
+ puts clazz.column_names
121
+ clazz.column_names.each do |name|
122
+ puts self[name]
123
+ end
124
+ end
125
+
126
+ def from_google_spreadsheet
127
+ end
128
+ end
129
+
130
+ module ActiveRecordRelationConverter
131
+ include Utils
132
+ extend Utils
133
+ def to_google_spreadsheet(session, spreadsheet_key, name: self.table_name, worksheet_title: false, row_offset: 1)
134
+ spreadsheet = session.spreadsheet_by_key(spreadsheet_key)
135
+ ws = get_worksheet_by_name(spreadsheet, name)
136
+
137
+ if worksheet_title
138
+ self.column_names.each_with_index do |name, column_index|
139
+ ws[row_offset, column_index+1] = name
140
+ end
141
+ row_offset += 1
142
+ end
143
+
144
+ self.each_with_index do |item, index|
145
+ self.column_names.each_with_index do |name, column_index|
146
+ ws[index + row_offset, column_index + 1] = item[name]
147
+ end
148
+ end
149
+ ws.save
150
+ ws.reload
151
+ end
152
+
153
+ def from_google_spreadsheet
154
+ end
155
+ end
156
+
157
+ class ActiveRecord::Base
158
+ include ActiveRecordBaseConverter
159
+ end
160
+
161
+ class ActiveRecord::Relation
162
+ include ActiveRecordRelationConverter
163
+ end
164
+
165
+ def self.each_tables(session, spreadsheet_key)
166
+ spreadsheet = session.spreadsheet_by_key(spreadsheet_key)
167
+ info = get_db_info
168
+ start_time = Time.now
169
+ info.each do |name, value|
170
+ worksheet = get_worksheet_by_name(spreadsheet, name)
171
+ clazz = name.capitalize.singularize.camelize.to_s.constantize
172
+ yield(name, value, worksheet, clazz, info)
173
+ end
174
+ puts '*************************'
175
+ puts Time.now - start_time
176
+ puts '*************************'
177
+ end
178
+ end
@@ -0,0 +1,11 @@
1
+ module ActiveRecordToGoogleSpreadsheet
2
+ module Utils
3
+ def get_worksheet_by_name(st, name)
4
+ ws = st.worksheet_by_title(name)
5
+ if !ws
6
+ ws = st.add_worksheet(name)
7
+ end
8
+ return ws
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module ActiverecordToGoogleSpreadsheet
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: activerecord_to_google_spreadsheet
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Otis Chen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-15 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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
+ description: Dump databases to Google SpreadSheets and can also from SpreadSheets.
42
+ email:
43
+ - kkooff2000@gmail.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - CODE_OF_CONDUCT.md
50
+ - Gemfile
51
+ - LICENSE.txt
52
+ - README.md
53
+ - Rakefile
54
+ - activerecord_to_google_spreadsheet.gemspec
55
+ - bin/console
56
+ - bin/setup
57
+ - lib/activerecord_to_google_spreadsheet.rb
58
+ - lib/activerecord_to_google_spreadsheet/utils.rb
59
+ - lib/activerecord_to_google_spreadsheet/version.rb
60
+ homepage: https://github.com/kkooff2000/activerecord_to_google_spreadsheet
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.5.1
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: ActiveRecord to Google SpreadSheets.
84
+ test_files: []