data_pitcher 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +77 -6
- data/lib/data_pitcher.rb +1 -0
- data/lib/data_pitcher/batch.rb +9 -3
- data/lib/data_pitcher/command.rb +53 -0
- data/lib/data_pitcher/executor.rb +10 -3
- data/lib/data_pitcher/spreadsheet.rb +11 -12
- data/lib/data_pitcher/tasks/data_pitcher_tasks.rake +13 -2
- data/lib/data_pitcher/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9160b5110005e8d31749729dfdfedd2172fc0327
|
4
|
+
data.tar.gz: 12a7743e035d76d11fce3946e41b055e2f464525
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3a29f48edb6a2906826b044a8d416fa8153661efa00a99b7bebefd86eb81078dfc90993d9569182d370549fc81ae0b1339e4b8cae9ef361cc64b037dad2e4e35
|
7
|
+
data.tar.gz: 2f425a302fcfc7132fce72f89a80d5000f65409649838fdf523c9e916a97b2be8c2c3a112e4769fde5c0f48390bc1e8dc8fff4e3d53a233501c28888c3a9d237
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,26 +1,97 @@
|
|
1
1
|
# DataPitcher
|
2
2
|
|
3
|
-
DataPitcher send SQL result data to your
|
3
|
+
DataPitcher send SQL result data to your Google Spreadsheet.
|
4
4
|
|
5
5
|
## Installation
|
6
|
+
|
6
7
|
Add this line to your application's Gemfile:
|
7
8
|
|
8
9
|
```ruby
|
9
10
|
gem 'data_pitcher'
|
10
11
|
```
|
11
12
|
|
12
|
-
|
13
|
+
Download and install by running:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
$ bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Initialize with:
|
20
|
+
|
21
|
+
```bash
|
22
|
+
$ bundle exec rails generate data_pitcher:install
|
23
|
+
```
|
24
|
+
|
25
|
+
This adds the following files which are used for configuration:
|
26
|
+
|
27
|
+
* config/data_pitcher.yml
|
28
|
+
* config/initializer/data_pitcher.rb
|
29
|
+
|
30
|
+
Check the comments in each file for more information.
|
31
|
+
|
32
|
+
## How to use
|
33
|
+
|
34
|
+
### Authorization
|
35
|
+
|
36
|
+
DataPicther use a [service account](https://developers.google.com/identity/protocols/OAuth2ServiceAccount) for accessing Google Spreadsheet. Service account can only access documents explicitly shared (or created) with the service account. It means that your program can only access documents that can be accessed with your service account.
|
37
|
+
|
38
|
+
To use a service account, follow these steps:
|
39
|
+
|
40
|
+
1. Go to the [credentials page](https://console.developers.google.com/apis/credentials) in the Google Developer Console.
|
41
|
+
2. Create a new project, or select an exisiting project.
|
42
|
+
3. Click "Create credentials" -> "Service account".
|
43
|
+
4. Click "Create" and download the keys a JSON file.
|
44
|
+
5. Activate the Drive API for your project in the [Google API Console](https://console.developers.google.com/apis/library).
|
45
|
+
6. Rename your JSON file to "service_account.json", and put to "[RAILS_ROOT]/config/service_account.json"
|
46
|
+
|
47
|
+
Optionally, you can change the path, and the file name. When you change these items, please change the following code so as not to forget.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
# config/initializer/data_pitcher.rb
|
51
|
+
DataPitcher.configure do |config|
|
52
|
+
config.google_service_account_json_path = Rails.root.join('config', 'service_account.json')
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
### Setting the YAML file
|
57
|
+
|
58
|
+
DataPicther sends SQL results to the specified spreadsheet. To do this, you need to set the key of the destination spreadsheet and the path to the SQL file.
|
59
|
+
|
60
|
+
For example:
|
61
|
+
|
62
|
+
* Spreadsheet URL: `https://docs.google.com/spreadsheets/d/1QlOBVmsEau8EupE5Kj7AqoHLQTjInoq0GpwPkW7_WdU/edit#gid=0`
|
63
|
+
* SQL file path: `[RAILS_ROOT]/lib/sqls/conversion_rates.sql`
|
64
|
+
|
65
|
+
```yaml
|
66
|
+
# config/data_pitcher.yml
|
67
|
+
data_pitcher:
|
68
|
+
- spreadsheet_key: 1QlOBVmsEau8EupE5Kj7AqoHLQTjInoq0GpwPkW7_WdU
|
69
|
+
sql_path: <%= Rails.root.join('lib', 'sqls', 'conversion_rates.sql') %>
|
70
|
+
```
|
71
|
+
|
72
|
+
To send multiple SQLs to multiple spreadsheets, add them to the array in the YAML file.
|
73
|
+
|
74
|
+
### Send data
|
75
|
+
|
76
|
+
The easiest way is to set the following Rake task to cron. This task executes all the commands defined in the YAML file.
|
77
|
+
|
13
78
|
```bash
|
14
|
-
$ bundle
|
79
|
+
$ bundle exec rake data_pitcher:export:run
|
15
80
|
```
|
16
81
|
|
17
|
-
|
82
|
+
Or, you can manually execute sending data programmatically as follows:
|
18
83
|
|
19
84
|
```ruby
|
20
|
-
|
85
|
+
DataPitcher::Spreadsheet.new('<SPREADSHEET KEY>').replace_worksheet_with_query('<SQL QUERY STRING>')
|
21
86
|
```
|
22
87
|
|
23
|
-
|
88
|
+
### (Optionally) Check YAML definition
|
89
|
+
|
90
|
+
By executing the following command, you can check whether the definition of YAML file is correct without actually sending data.
|
91
|
+
|
92
|
+
```bash
|
93
|
+
$ bundle exec rake data_pitcher:export:dry_run
|
94
|
+
```
|
24
95
|
|
25
96
|
## Contributing
|
26
97
|
Contribution directions go here.
|
data/lib/data_pitcher.rb
CHANGED
data/lib/data_pitcher/batch.rb
CHANGED
@@ -11,9 +11,15 @@ module DataPitcher
|
|
11
11
|
YAML.load(ERB.new(File.read(@yaml_path)).result)['data_pitcher']
|
12
12
|
end
|
13
13
|
|
14
|
-
def execute
|
15
|
-
commands.each do |command|
|
16
|
-
|
14
|
+
def execute(dry_run: true)
|
15
|
+
commands.each.with_index(1) do |command, index|
|
16
|
+
begin
|
17
|
+
if DataPitcher::Command.new(command['spreadsheet_key'], command['sql_path'], dry_run: dry_run, index: index).execute
|
18
|
+
puts "##{index} command: sending completed."
|
19
|
+
end
|
20
|
+
rescue => e
|
21
|
+
puts "[ERROR] ##{index} command skipped: #{e.class} #{e.message}"
|
22
|
+
end
|
17
23
|
end
|
18
24
|
end
|
19
25
|
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module DataPitcher
|
2
|
+
class Command
|
3
|
+
def initialize(spreadsheet_key, sql_path, dry_run: true, index: 1)
|
4
|
+
@spreadsheet_key = spreadsheet_key
|
5
|
+
@sql_path = sql_path
|
6
|
+
@dry_run = dry_run
|
7
|
+
@index = index
|
8
|
+
end
|
9
|
+
|
10
|
+
def execute
|
11
|
+
@dry_run ? dry_run : run
|
12
|
+
end
|
13
|
+
|
14
|
+
def dry_run
|
15
|
+
puts dry_run_log
|
16
|
+
false
|
17
|
+
end
|
18
|
+
|
19
|
+
def run
|
20
|
+
unless spreadsheet.valid?
|
21
|
+
puts "[ERROR] ##{@index} command skipped: DataPitcher can not access to spreadsheet (#{@spreadsheet_key})"
|
22
|
+
return false
|
23
|
+
end
|
24
|
+
unless executor.valid?
|
25
|
+
puts "[ERROR] ##{@index} command skipped: SQL is invalid (#{@sql_path})"
|
26
|
+
return false
|
27
|
+
end
|
28
|
+
spreadsheet.replace_worksheet_with_query(sql_query)
|
29
|
+
end
|
30
|
+
|
31
|
+
def spreadsheet
|
32
|
+
@spreadsheet ||= DataPitcher::Spreadsheet.new(@spreadsheet_key)
|
33
|
+
end
|
34
|
+
|
35
|
+
def executor
|
36
|
+
@executor ||= DataPitcher::Executor.new(sql_query)
|
37
|
+
end
|
38
|
+
|
39
|
+
def sql_query
|
40
|
+
@sql_query ||= File.read(@sql_path)
|
41
|
+
end
|
42
|
+
|
43
|
+
def dry_run_log
|
44
|
+
<<-EOS
|
45
|
+
##{@index} command
|
46
|
+
spreadsheet_key: #{@spreadsheet_key}
|
47
|
+
valid?: #{spreadsheet.valid?}
|
48
|
+
sql_path: #{@sql_path}
|
49
|
+
valid?: #{executor.valid?}
|
50
|
+
EOS
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -5,7 +5,14 @@ module DataPitcher
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def execute
|
8
|
-
result(
|
8
|
+
result(select_query)
|
9
|
+
end
|
10
|
+
|
11
|
+
def valid?
|
12
|
+
select_query
|
13
|
+
true
|
14
|
+
rescue => e
|
15
|
+
false
|
9
16
|
end
|
10
17
|
|
11
18
|
private
|
@@ -23,9 +30,9 @@ module DataPitcher
|
|
23
30
|
result
|
24
31
|
end
|
25
32
|
|
26
|
-
def
|
33
|
+
def select_query
|
27
34
|
with_sandbox do
|
28
|
-
connection.exec_query(query)
|
35
|
+
connection.exec_query(@query)
|
29
36
|
end
|
30
37
|
end
|
31
38
|
|
@@ -2,9 +2,8 @@ require 'google_drive'
|
|
2
2
|
|
3
3
|
module DataPitcher
|
4
4
|
class Spreadsheet
|
5
|
-
def initialize(spreadsheet_key
|
5
|
+
def initialize(spreadsheet_key)
|
6
6
|
@spreadsheet_key = spreadsheet_key
|
7
|
-
@sql_path = sql_path
|
8
7
|
end
|
9
8
|
|
10
9
|
def spreadsheet
|
@@ -26,8 +25,8 @@ module DataPitcher
|
|
26
25
|
worksheet.save
|
27
26
|
end
|
28
27
|
|
29
|
-
def fill_sheet
|
30
|
-
result = DataPitcher::Executor.new(
|
28
|
+
def fill_sheet(sql_query)
|
29
|
+
result = DataPitcher::Executor.new(sql_query).execute
|
31
30
|
worksheet.reload
|
32
31
|
# fill header
|
33
32
|
result.header.each.with_index(1) do |val, col_index|
|
@@ -42,16 +41,16 @@ module DataPitcher
|
|
42
41
|
worksheet.save
|
43
42
|
end
|
44
43
|
|
45
|
-
def
|
46
|
-
|
47
|
-
|
48
|
-
file.read
|
49
|
-
end
|
44
|
+
def replace_worksheet_with_query(sql_query)
|
45
|
+
clear_sheet
|
46
|
+
fill_sheet(sql_query)
|
50
47
|
end
|
51
48
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
49
|
+
def valid?
|
50
|
+
worksheet
|
51
|
+
true
|
52
|
+
rescue => e
|
53
|
+
false
|
55
54
|
end
|
56
55
|
|
57
56
|
def session
|
@@ -1,6 +1,17 @@
|
|
1
1
|
namespace :data_pitcher do
|
2
2
|
desc 'Export data to google spreadsheets with SQL'
|
3
|
-
|
4
|
-
|
3
|
+
namespace :export do
|
4
|
+
task run: :environment do
|
5
|
+
export(dry_run: false)
|
6
|
+
end
|
7
|
+
task dry_run: :environment do
|
8
|
+
export(dry_run: true)
|
9
|
+
end
|
5
10
|
end
|
6
11
|
end
|
12
|
+
|
13
|
+
def export(dry_run: true)
|
14
|
+
puts "** Start export data / sending: #{!dry_run}"
|
15
|
+
DataPitcher::Batch.new.execute(dry_run: dry_run)
|
16
|
+
puts "** Completed export data / sending: #{!dry_run}"
|
17
|
+
end
|
data/lib/data_pitcher/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: data_pitcher
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Masahiro Nishimi
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- data_pitcher.gemspec
|
70
70
|
- lib/data_pitcher.rb
|
71
71
|
- lib/data_pitcher/batch.rb
|
72
|
+
- lib/data_pitcher/command.rb
|
72
73
|
- lib/data_pitcher/configuration.rb
|
73
74
|
- lib/data_pitcher/executor.rb
|
74
75
|
- lib/data_pitcher/railtie.rb
|