takenoko 0.1.2 → 0.2.0

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: afd7651c59b5026c0129a6844a0d4fc84a9e581c
4
- data.tar.gz: dfe614608f5718772f113611324231918c4b1bf3
3
+ metadata.gz: 1bb80ebb114ac3ee473ee833d63d9436dbf25ea9
4
+ data.tar.gz: 76dc1e87a834c27e407dff4180b5a0ea8f2590cf
5
5
  SHA512:
6
- metadata.gz: da1b26dc90fc9409d533d03d70c41ec50824a575021ed9d7087fff3ed9f79eb37f68a6179472dc14ea5b2364c5e49af9926097402d9b0dede187593adf5b1572
7
- data.tar.gz: 2b69692d6580d799e0e0d2f4571162e86cb064e5389db08936cc6bec88adad525e201819267ed98e06f0f37a67afe61c8679140a79f98c6cf32f8ef59d9b5fd2
6
+ metadata.gz: 7510fcbbbc44c414a0a5e0c3b25139be5ee11b340ee6b66e30a6592018fcf96696e3a7aac28b55931817c639adc48af709412dbf8473f3f6801d8f5b928f371f
7
+ data.tar.gz: 0fb1c6bfce088a274b00840b10d4fd6d14e0dc539f14a7435cdd994e2b2a36ad0e5d382dcc545613e413cd8b3b9b7cd1ad0ebecaf49bb8760d894f84cd60d854
@@ -35,4 +35,12 @@ Takenoko.config do |conf|
35
35
  # Class for post processing, nil for Class = table class name
36
36
  # conf.postprocess_class = nil
37
37
 
38
+ # Always reload mapping data, default true
39
+ # conf.always_reload = true
40
+
41
+ # Downloaded file global location, files will be saved under download_location/table_name
42
+ # conf.download_location = "tmp/attach_files" #Default
43
+
44
+ # Google drive folder id, where your attached files're stored
45
+ # conf.folder_id = nil
38
46
  end
@@ -0,0 +1,11 @@
1
+ require "google_drive"
2
+ module GoogleDrive
3
+ class Session
4
+ define_method :collection_by_id do |id|
5
+ raise "Collection #{id} not found" unless collection = files.select do |f|
6
+ (f.is_a? GoogleDrive::Collection) && f.id == id
7
+ end.first
8
+ collection
9
+ end
10
+ end
11
+ end
@@ -1,4 +1,5 @@
1
1
  require "google_drive"
2
+ require "google_drive/session"
2
3
 
3
4
  module GoogleDrive
4
5
  class Worksheet
@@ -0,0 +1,26 @@
1
+ module Takenoko
2
+ module AttachHelper
3
+ extend self
4
+ def download(table_data)
5
+ table_data[:attach_files].each do |col|
6
+ raise "Folder ID should be set" unless (folder_id = col[:folder_id] || table[:folder_id]).present?
7
+ column_name = col[:column_name]
8
+ raise "Column #{column_name} not found" unless table_data[:header].include?(column_name)
9
+ Rails.logger.info "Downloading file form table #{table_data[:table_name]}"
10
+ download_location = col[:download_location]
11
+ FileUtils.mkdir_p(download_location) unless File.directory?(download_location)
12
+ folder = Takenoko.google_client.folder_by_id col[:folder_id]
13
+
14
+ table_data[:rows].each do |row|
15
+ next if row[column_name].blank?
16
+ file_name = File.basename(row[column_name])
17
+ raise "File: '#{file_name}' not found" unless file = folder.file_by_title(file_name)
18
+ full_path_name = download_location+"/"+file_name
19
+ file.download_to_file(full_path_name)
20
+ Rails.logger.info "Downloaded file: #{full_path_name}"
21
+ end
22
+ end
23
+ return true
24
+ end
25
+ end
26
+ end
@@ -16,10 +16,14 @@ module Takenoko
16
16
  end
17
17
  end
18
18
 
19
+ def table_to_file(table)
20
+ public_send("table_to_#{table[:file_extension]}",table)
21
+ end
22
+
19
23
  SUPPORTED_FILE_EXT.each do |fx|
20
24
  define_method "table_to_#{fx}" do |table|
21
25
  dir = table[:export_file_location]
22
- FileUtils.mkdir(dir) unless File.directory?(dir)
26
+ FileUtils.mkdir_p(dir) unless File.directory?(dir)
23
27
  File.open("#{dir}/#{table[:table_name]}.#{fx}","w") do |f|
24
28
  f.write public_send("convert_to_#{fx}",table)
25
29
  end
@@ -92,6 +92,10 @@ module Takenoko
92
92
  session.spreadsheet_by_key(sheet_id)
93
93
  end
94
94
 
95
+ def folder_by_id(folder_id)
96
+ session.collection_by_id(folder_id)
97
+ end
98
+
95
99
  private
96
100
  def update_table_config(table,ws_header)
97
101
  columns_mapping = HashWithIndifferentAccess.new
@@ -1,3 +1,3 @@
1
1
  module Takenoko
2
- VERSION = "0.1.2"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/takenoko.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rails'
2
2
  require 'google_drive'
3
3
  require 'google_drive/google_drive'
4
+ require 'google_drive/google_drive/session'
4
5
 
5
6
  module Takenoko
6
7
  extend self
@@ -13,6 +14,9 @@ module Takenoko
13
14
  mattr_accessor :mapping_file
14
15
  @@mapping_file = false
15
16
 
17
+ mattr_accessor :always_reload
18
+ @@always_reload = true
19
+
16
20
  @@google_client = nil
17
21
  @@mapping_config = nil
18
22
 
@@ -38,8 +42,16 @@ module Takenoko
38
42
  mattr_accessor :postprocess_class
39
43
  @@postprocess_class = nil
40
44
 
45
+ mattr_accessor :download_location
46
+ @@download_location = "tmp/attach_files"
47
+
48
+ mattr_accessor :folder_id
49
+ @@folder_id = nil
50
+
51
+
41
52
  require 'takenoko/exporter'
42
53
  require 'takenoko/google_client'
54
+ require 'takenoko/attach_helper'
43
55
 
44
56
  def config
45
57
  yield self
@@ -57,24 +69,77 @@ module Takenoko
57
69
  end
58
70
 
59
71
  def mapping_config
72
+ (@@always_reload && generate_base_config) || @@mapping_config || generate_base_config
73
+ end
74
+
75
+ def table_config(table_name)
76
+ raise "#{table_name} config not exists" unless conf = mapping_config[:tables][table_name]
77
+ return conf
78
+ end
79
+
80
+ def download_attached_files(table_name)
81
+ table_data = google_client.get_table(table_name)
82
+ return false unless table_data[:attach_files].present?
83
+ return AttachHelper.download table_data
84
+ end
85
+
86
+ def download_table_files(table_name)
87
+ table_data = google_client.get_table(table_name)
88
+ raise "attach_files not set" unless table_data[:attach_files].present?
89
+ AttachHelper.download table_data
90
+ end
91
+
92
+ def download_all_files
93
+ mapping_config[:tables].each do |table,conf|
94
+ next if conf[:attach_files].blank?
95
+ download_table_files table
96
+ end
97
+ return true
98
+ end
99
+
100
+ (SUPPORTED_FILE_EXT.clone << [:db,:file]).flatten!.each do |output|
101
+ define_method "table_to_#{output}" do | table_name |
102
+ data = google_client.get_table(table_name)
103
+ Exporter.public_send("table_to_#{output}",data)
104
+ end
105
+
106
+ define_method "all_to_#{output}" do
107
+ mapping_config[:tables].each do |table,conf|
108
+ Takenoko.public_send("table_to_#{output}",table)
109
+ end
110
+ end
111
+ end
112
+
113
+ def generate_base_config
60
114
  return unless check_config
61
- if @@mapping_file
62
- conf = YAML.load_file(@@mapping_file).with_indifferent_access
63
- raise "tables not exists" if conf[:tables].blank?
64
- else
65
- conf = HashWithIndifferentAccess.new({tables: {}})
115
+
116
+ conf = HashWithIndifferentAccess.new
117
+
118
+ if @@sheet_id
119
+ sheet_conf = HashWithIndifferentAccess.new({tables: {}})
66
120
  google_client.spreadsheet.worksheets.each do |ws|
67
121
  next if ws.title.match(/\s*#.*/)
68
- conf[:tables][ws.title] = {
122
+ sheet_conf[:tables][ws.title] = {
69
123
  worksheet_id: ws.gid,
70
124
  worksheet: ws.title
71
125
  }
72
126
  end
127
+ conf.deep_merge!(sheet_conf)
128
+ end
129
+
130
+ if @@mapping_file
131
+ file_conf = YAML.load_file(@@mapping_file).with_indifferent_access
132
+ raise "tables not exists" if file_conf[:tables].blank?
133
+ conf.deep_merge!(file_conf)
73
134
  end
135
+
136
+ conf[:tables].compact!
74
137
  conf[:tables].each do |t, v|
138
+ unless conf[:tables][t]
139
+ conf[:tables][t] = Hash.new
140
+ end
75
141
  table = conf[:tables][t]
76
- raise "Table config cannot be nil" unless table
77
- raise "#{f} cannot be blank" unless table[:sheet_id] ||= @@sheet_id
142
+ raise "#{sheet_id} cannot be blank" unless table[:sheet_id] ||= @@sheet_id
78
143
  table[:worksheet] = t if table[:worksheet].blank? && table[:worksheet_id].blank?
79
144
  table[:find_column] = table[:find_column] || :id
80
145
  table_name = table[:table_name] = t.pluralize if table[:table_name].blank?
@@ -85,7 +150,8 @@ module Takenoko
85
150
  :file_extension,
86
151
  :export_file_location,
87
152
  :enable_postprocess,
88
- :postprocess_class
153
+ :postprocess_class,
154
+ :folder_id
89
155
  ].each do |f|
90
156
  table[f] = class_variable_get("@@" + f.to_s) unless table.key?(f)
91
157
  end
@@ -95,27 +161,20 @@ module Takenoko
95
161
  end
96
162
 
97
163
  raise "Not support file extension: #{table[:file_extension]}" unless SUPPORTED_FILE_EXT.include?(table[:file_extension])
98
- end
99
- @@mapping_config = conf
100
- end
101
164
 
102
- def table_config(table_name)
103
- raise "#{table_name} config not exists" unless conf = mapping_config[:tables][table_name]
104
- return conf
105
- end
106
-
107
- (SUPPORTED_FILE_EXT.clone << :db).each do |output|
108
- define_method "table_to_#{output}" do | table_name |
109
- data = google_client.get_table(table_name)
110
- Exporter.public_send("table_to_#{output}",data)
111
- end
112
-
113
- define_method "all_to_#{output}" do
114
- mapping_config[:tables].each do |table,conf|
115
- Takenoko.public_send("table_to_#{output}",table)
165
+ table[:download_location] = table[:download_location] || (@@download_location && (@@download_location +"/"+table[:table_name]))
166
+ if attach_files = table[:attach_files].present?
167
+ attach_files = table[:attach_files]
168
+ attach_files = attach_files.map do |col|
169
+ raise "column_name must be set" unless col[:column_name]
170
+ col[:download_location] = col[:download_location].present? ? table[:download_location] + "/" + col[:download_location] : table[:download_location]
171
+ raise "folder_id should be set" unless col[:folder_id] = col[:folder_id] || table[:folder_id]
172
+ end
116
173
  end
117
174
  end
175
+ @@mapping_config = conf
118
176
  end
177
+ alias reload_config! generate_base_config
119
178
 
120
179
  class Railtie < Rails::Railtie
121
180
  railtie_name :takenoko
@@ -1,5 +1,5 @@
1
1
  namespace :takenoko do
2
- (Takenoko::SUPPORTED_FILE_EXT.clone << :db ).each do |output|
2
+ (Takenoko::SUPPORTED_FILE_EXT.clone << [:db,:file] ).flatten!.each do |output|
3
3
  task "all_to_#{output}".to_sym => :environment do
4
4
  Takenoko.public_send("all_to_#{output}")
5
5
  end
@@ -8,4 +8,13 @@ namespace :takenoko do
8
8
  Takenoko.public_send("table_to_#{output}",args[:table])
9
9
  end
10
10
  end
11
+
12
+ task :download_table_files,[:table] => :environment do |t, args|
13
+ Takenoko.download_table_files(args[:table])
14
+ end
15
+
16
+ task :download_all_files => :environment do
17
+ Takenoko.download_all_files
18
+ end
19
+
11
20
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: takenoko
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - KhiemNS
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-09-06 00:00:00.000000000 Z
11
+ date: 2016-09-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -78,14 +78,16 @@ files:
78
78
  - lib/generators/takenoko/config_generator.rb
79
79
  - lib/generators/takenoko/templates/takenoko.rb
80
80
  - lib/google_drive/google_drive.rb
81
+ - lib/google_drive/google_drive/session.rb
81
82
  - lib/takenoko.rb
83
+ - lib/takenoko/attach_helper.rb
82
84
  - lib/takenoko/exporter.rb
83
85
  - lib/takenoko/google_client.rb
84
86
  - lib/takenoko/version.rb
85
87
  - lib/tasks/takenoko.rake
86
88
  - test/takenoko_test.rb
87
89
  - test/test_helper.rb
88
- homepage: https://github.com/khiemns54/takenoko
90
+ homepage: https://github.com/khiemns54/takenoko/tree/release/0.2.0
89
91
  licenses: []
90
92
  metadata: {}
91
93
  post_install_message: