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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ad5b48b5dd9a15701d6addf471f7b5c0d6fe8051
4
- data.tar.gz: 9f3432b52d4c4d0d034ed38f7fac6bf987412c5e
3
+ metadata.gz: 9160b5110005e8d31749729dfdfedd2172fc0327
4
+ data.tar.gz: 12a7743e035d76d11fce3946e41b055e2f464525
5
5
  SHA512:
6
- metadata.gz: 80c06d4e5e9d3dc43ec914e8a723f96d40f61e67dd3da2946d3a6f36b853e9c9634ff66308b42b2896d3c9c0be414830c44d80e5a1afdd178ad56681fdcee71d
7
- data.tar.gz: d19d9efddc0e1dc59525e44222eeea653980d202fa15da9823216fff10d2267aeb528501813492822bad02cc4b0f5b6a2ae02cb105e8278c4b9972312188546c
6
+ metadata.gz: 3a29f48edb6a2906826b044a8d416fa8153661efa00a99b7bebefd86eb81078dfc90993d9569182d370549fc81ae0b1339e4b8cae9ef361cc64b037dad2e4e35
7
+ data.tar.gz: 2f425a302fcfc7132fce72f89a80d5000f65409649838fdf523c9e916a97b2be8c2c3a112e4769fde5c0f48390bc1e8dc8fff4e3d53a233501c28888c3a9d237
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- data_pitcher (0.1.0)
4
+ data_pitcher (0.1.1)
5
5
  activerecord (>= 3.2)
6
6
  google_drive
7
7
 
data/README.md CHANGED
@@ -1,26 +1,97 @@
1
1
  # DataPitcher
2
2
 
3
- DataPitcher send SQL result data to your google spreadsheets.
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
- And then execute:
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
- ## Setup
82
+ Or, you can manually execute sending data programmatically as follows:
18
83
 
19
84
  ```ruby
20
- bundle exec rails g data_pitcher:install
85
+ DataPitcher::Spreadsheet.new('<SPREADSHEET KEY>').replace_worksheet_with_query('<SQL QUERY STRING>')
21
86
  ```
22
87
 
23
- TODO: write more
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
@@ -4,6 +4,7 @@ require 'data_pitcher/executor'
4
4
  require 'data_pitcher/result'
5
5
  require 'data_pitcher/spreadsheet'
6
6
  require 'data_pitcher/batch'
7
+ require 'data_pitcher/command'
7
8
 
8
9
  module DataPitcher
9
10
  class << self
@@ -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
- DataPitcher::Spreadsheet.new(command['spreadsheet_key'], command['sql_path']).replace_worksheet_with_query
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(select(@query))
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 select(query)
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, sql_path)
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(read_query).execute
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 read_query
46
- # TODO: 例外処理
47
- File.open(@sql_path) do |file|
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 replace_worksheet_with_query
53
- clear_sheet
54
- fill_sheet
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
- task execute: :environment do
4
- DataPitcher::Batch.new.execute
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
@@ -1,3 +1,3 @@
1
1
  module DataPitcher
2
- VERSION = '0.1.0'
2
+ VERSION = '0.1.1'
3
3
  end
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.0
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-23 00:00:00.000000000 Z
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