data_pitcher 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ad5b48b5dd9a15701d6addf471f7b5c0d6fe8051
4
+ data.tar.gz: 9f3432b52d4c4d0d034ed38f7fac6bf987412c5e
5
+ SHA512:
6
+ metadata.gz: 80c06d4e5e9d3dc43ec914e8a723f96d40f61e67dd3da2946d3a6f36b853e9c9634ff66308b42b2896d3c9c0be414830c44d80e5a1afdd178ad56681fdcee71d
7
+ data.tar.gz: d19d9efddc0e1dc59525e44222eeea653980d202fa15da9823216fff10d2267aeb528501813492822bad02cc4b0f5b6a2ae02cb105e8278c4b9972312188546c
@@ -0,0 +1,7 @@
1
+ .bundle/
2
+ log/*.log
3
+ pkg/
4
+ spec/dummy/db/*.sqlite3
5
+ spec/dummy/db/*.sqlite3-journal
6
+ spec/dummy/log/*.log
7
+ spec/dummy/tmp/
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in data_pitcher.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
@@ -0,0 +1,92 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ data_pitcher (0.1.0)
5
+ activerecord (>= 3.2)
6
+ google_drive
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (5.1.1)
12
+ activesupport (= 5.1.1)
13
+ activerecord (5.1.1)
14
+ activemodel (= 5.1.1)
15
+ activesupport (= 5.1.1)
16
+ arel (~> 8.0)
17
+ activesupport (5.1.1)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (~> 0.7)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ addressable (2.5.1)
23
+ public_suffix (~> 2.0, >= 2.0.2)
24
+ arel (8.0.0)
25
+ concurrent-ruby (1.0.5)
26
+ declarative (0.0.9)
27
+ declarative-option (0.1.0)
28
+ faraday (0.12.1)
29
+ multipart-post (>= 1.2, < 3)
30
+ google-api-client (0.12.0)
31
+ addressable (~> 2.5, >= 2.5.1)
32
+ googleauth (~> 0.5)
33
+ httpclient (>= 2.8.1, < 3.0)
34
+ mime-types (~> 3.0)
35
+ representable (~> 3.0)
36
+ retriable (>= 2.0, < 4.0)
37
+ google_drive (2.1.5)
38
+ google-api-client (>= 0.11.0, < 0.13.0)
39
+ googleauth (>= 0.5.0, < 1.0.0)
40
+ nokogiri (>= 1.5.3, < 2.0.0)
41
+ googleauth (0.5.1)
42
+ faraday (~> 0.9)
43
+ jwt (~> 1.4)
44
+ logging (~> 2.0)
45
+ memoist (~> 0.12)
46
+ multi_json (~> 1.11)
47
+ os (~> 0.9)
48
+ signet (~> 0.7)
49
+ httpclient (2.8.3)
50
+ i18n (0.8.4)
51
+ jwt (1.5.6)
52
+ little-plugger (1.1.4)
53
+ logging (2.2.2)
54
+ little-plugger (~> 1.1)
55
+ multi_json (~> 1.10)
56
+ memoist (0.16.0)
57
+ mime-types (3.1)
58
+ mime-types-data (~> 3.2015)
59
+ mime-types-data (3.2016.0521)
60
+ mini_portile2 (2.2.0)
61
+ minitest (5.10.2)
62
+ multi_json (1.12.1)
63
+ multipart-post (2.0.0)
64
+ nokogiri (1.8.0)
65
+ mini_portile2 (~> 2.2.0)
66
+ os (0.9.6)
67
+ public_suffix (2.0.5)
68
+ rake (12.0.0)
69
+ representable (3.0.4)
70
+ declarative (< 0.1.0)
71
+ declarative-option (< 0.2.0)
72
+ uber (< 0.2.0)
73
+ retriable (3.0.2)
74
+ signet (0.7.3)
75
+ addressable (~> 2.3)
76
+ faraday (~> 0.9)
77
+ jwt (~> 1.5)
78
+ multi_json (~> 1.10)
79
+ thread_safe (0.3.6)
80
+ tzinfo (1.2.3)
81
+ thread_safe (~> 0.1)
82
+ uber (0.1.0)
83
+
84
+ PLATFORMS
85
+ ruby
86
+
87
+ DEPENDENCIES
88
+ data_pitcher!
89
+ rake
90
+
91
+ BUNDLED WITH
92
+ 1.14.6
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Masahiro Nishimi
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.
@@ -0,0 +1,29 @@
1
+ # DataPitcher
2
+
3
+ DataPitcher send SQL result data to your google spreadsheets.
4
+
5
+ ## Installation
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'data_pitcher'
10
+ ```
11
+
12
+ And then execute:
13
+ ```bash
14
+ $ bundle
15
+ ```
16
+
17
+ ## Setup
18
+
19
+ ```ruby
20
+ bundle exec rails g data_pitcher:install
21
+ ```
22
+
23
+ TODO: write more
24
+
25
+ ## Contributing
26
+ Contribution directions go here.
27
+
28
+ ## License
29
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -0,0 +1,23 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DataPitcher'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ require 'bundler/gem_tasks'
23
+
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ $: << File.expand_path(File.expand_path("../../test", __FILE__))
3
+
4
+ require "bundler/setup"
5
+ require "rails/plugin/test"
@@ -0,0 +1,23 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "data_pitcher/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "data_pitcher"
9
+ s.version = DataPitcher::VERSION
10
+ s.authors = ["Masahiro Nishimi"]
11
+ s.email = ["nishimi@sonicgarden.jp"]
12
+ s.homepage = "https://github.com/mahm/data_pitcher"
13
+ s.summary = "Data Pitcher throw your pesisted data to Google Spreadsheet."
14
+ s.description = "Data Pitcher throw your pesisted data to Google Spreadsheet."
15
+ s.license = "MIT"
16
+
17
+ s.files = `git ls-files`.split($\)
18
+
19
+ s.required_ruby_version = ">= 2.3"
20
+ s.add_runtime_dependency "activerecord", ">= 3.2"
21
+ s.add_runtime_dependency "google_drive"
22
+ s.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,22 @@
1
+ require 'data_pitcher/version'
2
+ require 'data_pitcher/configuration'
3
+ require 'data_pitcher/executor'
4
+ require 'data_pitcher/result'
5
+ require 'data_pitcher/spreadsheet'
6
+ require 'data_pitcher/batch'
7
+
8
+ module DataPitcher
9
+ class << self
10
+ attr_writer :configuration
11
+
12
+ def configuration
13
+ @configuration ||= Configuration.new
14
+ end
15
+
16
+ def configure
17
+ yield(configuration)
18
+ end
19
+ end
20
+ end
21
+
22
+ require 'data_pitcher/railtie' if defined?(Rails::Railtie)
@@ -0,0 +1,20 @@
1
+ require 'erb'
2
+
3
+ module DataPitcher
4
+ class Batch
5
+ def initialize(yaml_path = DataPitcher.configuration.data_pitcher_yaml_path)
6
+ @yaml_path = yaml_path
7
+ end
8
+
9
+ def commands
10
+ # TODO: 例外処理
11
+ YAML.load(ERB.new(File.read(@yaml_path)).result)['data_pitcher']
12
+ end
13
+
14
+ def execute
15
+ commands.each do |command|
16
+ DataPitcher::Spreadsheet.new(command['spreadsheet_key'], command['sql_path']).replace_worksheet_with_query
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,11 @@
1
+ module DataPitcher
2
+ class Configuration
3
+ attr_accessor :google_service_account_json_path
4
+ attr_accessor :data_pitcher_yaml_path
5
+
6
+ def initialize
7
+ @google_service_account_json_path = Rails.root.join('config', 'service_account.json')
8
+ @data_pitcher_yaml_path = Rails.root.join('config', 'data_pitcher.yml')
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,36 @@
1
+ module DataPitcher
2
+ class Executor
3
+ def initialize(query)
4
+ @query = query
5
+ end
6
+
7
+ def execute
8
+ result(select(@query))
9
+ end
10
+
11
+ private
12
+
13
+ def connection
14
+ ActiveRecord::Base.connection
15
+ end
16
+
17
+ def with_sandbox
18
+ result = nil
19
+ connection.transaction do
20
+ result = yield
21
+ raise ActiveRecord::Rollback
22
+ end
23
+ result
24
+ end
25
+
26
+ def select(query)
27
+ with_sandbox do
28
+ connection.exec_query(query)
29
+ end
30
+ end
31
+
32
+ def result(ar)
33
+ DataPitcher::Result.new(ar.columns, ar.rows)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,7 @@
1
+ module DataPitcher
2
+ class Railtie < ::Rails::Railtie
3
+ rake_tasks do
4
+ load 'data_pitcher/tasks/data_pitcher_tasks.rake'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module DataPitcher
2
+ class Result
3
+ attr_reader :header, :rows
4
+
5
+ def initialize(header, rows = [])
6
+ @header = header
7
+ @rows = rows
8
+ end
9
+
10
+ def <<(row)
11
+ rows << row
12
+ end
13
+
14
+ def ==(obj)
15
+ header == obj.header && rows == obj.rows
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,61 @@
1
+ require 'google_drive'
2
+
3
+ module DataPitcher
4
+ class Spreadsheet
5
+ def initialize(spreadsheet_key, sql_path)
6
+ @spreadsheet_key = spreadsheet_key
7
+ @sql_path = sql_path
8
+ end
9
+
10
+ def spreadsheet
11
+ @spreadsheet ||= session.spreadsheet_by_key(@spreadsheet_key)
12
+ end
13
+
14
+ def worksheet
15
+ # NOTE: DataPitcher will access first sheet only
16
+ @worksheet ||= spreadsheet.worksheets.first
17
+ end
18
+
19
+ def clear_sheet
20
+ worksheet.reload
21
+ (1..worksheet.num_rows).each do |row|
22
+ (1..worksheet.num_cols).each do |col|
23
+ worksheet[row, col] = ''
24
+ end
25
+ end
26
+ worksheet.save
27
+ end
28
+
29
+ def fill_sheet
30
+ result = DataPitcher::Executor.new(read_query).execute
31
+ worksheet.reload
32
+ # fill header
33
+ result.header.each.with_index(1) do |val, col_index|
34
+ worksheet[1, col_index] = val
35
+ end
36
+ # fill rows
37
+ result.rows.each.with_index(2) do |row, row_index|
38
+ row.each.with_index(1) do |val, col_index|
39
+ worksheet[row_index, col_index] = val
40
+ end
41
+ end
42
+ worksheet.save
43
+ end
44
+
45
+ def read_query
46
+ # TODO: 例外処理
47
+ File.open(@sql_path) do |file|
48
+ file.read
49
+ end
50
+ end
51
+
52
+ def replace_worksheet_with_query
53
+ clear_sheet
54
+ fill_sheet
55
+ end
56
+
57
+ def session
58
+ @session ||= ::GoogleDrive::Session.from_service_account_key(DataPitcher.configuration.google_service_account_json_path)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,6 @@
1
+ namespace :data_pitcher do
2
+ desc 'Export data to google spreadsheets with SQL'
3
+ task execute: :environment do
4
+ DataPitcher::Batch.new.execute
5
+ end
6
+ end
@@ -0,0 +1,3 @@
1
+ module DataPitcher
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,25 @@
1
+ require "rails"
2
+
3
+ module DataPitcher
4
+ class InstallGenerator < ::Rails::Generators::Base
5
+ def create_yaml_file
6
+ create_file 'config/data_pitcher.yml' do
7
+ <<-EOS
8
+ data_pitcher:
9
+ - spreadsheet_key: <YOUR GOOGLE SPREADSHEET KEY>
10
+ sql_path: <YOUR SQL FILE PATH>
11
+ EOS
12
+ end
13
+ end
14
+
15
+ def create_initializer_file
16
+ initializer 'data_pitcher.rb' do
17
+ <<-EOS
18
+ DataPitcher.configure do |config|
19
+ config.google_service_account_json_path = Rails.root.join('config', 'service_account.json')
20
+ end
21
+ EOS
22
+ end
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: data_pitcher
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Masahiro Nishimi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-06-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activerecord
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: google_drive
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: rake
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
+ description: Data Pitcher throw your pesisted data to Google Spreadsheet.
56
+ email:
57
+ - nishimi@sonicgarden.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - Gemfile
64
+ - Gemfile.lock
65
+ - MIT-LICENSE
66
+ - README.md
67
+ - Rakefile
68
+ - bin/test
69
+ - data_pitcher.gemspec
70
+ - lib/data_pitcher.rb
71
+ - lib/data_pitcher/batch.rb
72
+ - lib/data_pitcher/configuration.rb
73
+ - lib/data_pitcher/executor.rb
74
+ - lib/data_pitcher/railtie.rb
75
+ - lib/data_pitcher/result.rb
76
+ - lib/data_pitcher/spreadsheet.rb
77
+ - lib/data_pitcher/tasks/data_pitcher_tasks.rake
78
+ - lib/data_pitcher/version.rb
79
+ - lib/generators/data_pitcher/install/install_generator.rb
80
+ homepage: https://github.com/mahm/data_pitcher
81
+ licenses:
82
+ - MIT
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '2.3'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.2
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Data Pitcher throw your pesisted data to Google Spreadsheet.
104
+ test_files: []