ad_localize 3.6.0 → 4.0.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 +4 -4
- data/CHANGELOG.md +41 -1
- data/Gemfile.lock +34 -11
- data/README.md +148 -145
- data/ad_localize.gemspec +2 -0
- data/bin/console +1 -0
- data/exe/ad_localize +1 -1
- data/lib/ad_localize.rb +52 -11
- data/lib/ad_localize/ad_logger.rb +22 -9
- data/lib/ad_localize/cli.rb +10 -0
- data/lib/ad_localize/constant.rb +2 -21
- data/lib/ad_localize/entities/key.rb +74 -0
- data/lib/ad_localize/entities/locale_wording.rb +60 -0
- data/lib/ad_localize/entities/translation.rb +20 -0
- data/lib/ad_localize/entities/wording.rb +24 -0
- data/lib/ad_localize/interactors/execute_export_request.rb +43 -0
- data/lib/ad_localize/interactors/export_csv_files.rb +17 -0
- data/lib/ad_localize/interactors/export_g_spreadsheet.rb +55 -0
- data/lib/ad_localize/interactors/export_wording.rb +27 -0
- data/lib/ad_localize/interactors/merge_wordings.rb +43 -0
- data/lib/ad_localize/interactors/platforms/export_android_locale_wording.rb +39 -0
- data/lib/ad_localize/interactors/platforms/export_ios_locale_wording.rb +62 -0
- data/lib/ad_localize/interactors/platforms/export_json_locale_wording.rb +23 -0
- data/lib/ad_localize/interactors/platforms/export_platform_factory.rb +44 -0
- data/lib/ad_localize/interactors/platforms/export_properties_locale_wording.rb +29 -0
- data/lib/ad_localize/interactors/platforms/export_yaml_locale_wording.rb +23 -0
- data/lib/ad_localize/mappers/android_translation_mapper.rb +18 -0
- data/lib/ad_localize/mappers/csv_path_to_wording.rb +76 -0
- data/lib/ad_localize/mappers/ios_translation_mapper.rb +12 -0
- data/lib/ad_localize/mappers/locale_wording_to_hash.rb +25 -0
- data/lib/ad_localize/mappers/options_to_export_request.rb +28 -0
- data/lib/ad_localize/mappers/translation_group_mapper.rb +14 -0
- data/lib/ad_localize/mappers/translation_mapper.rb +30 -0
- data/lib/ad_localize/mappers/value_range_to_wording.rb +69 -0
- data/lib/ad_localize/option_handler.rb +30 -57
- data/lib/ad_localize/repositories/file_system_repository.rb +13 -0
- data/lib/ad_localize/repositories/g_sheets_repository.rb +44 -0
- data/lib/ad_localize/requests/export_request.rb +77 -0
- data/lib/ad_localize/requests/g_spreadsheet_options.rb +47 -0
- data/lib/ad_localize/requests/merge_policy.rb +28 -0
- data/lib/ad_localize/serializers/info_plist_serializer.rb +27 -0
- data/lib/ad_localize/serializers/json_serializer.rb +13 -0
- data/lib/ad_localize/serializers/localizable_strings_serializer.rb +27 -0
- data/lib/ad_localize/serializers/localizable_stringsdict_serializer.rb +35 -0
- data/lib/ad_localize/serializers/properties_serializer.rb +25 -0
- data/lib/ad_localize/serializers/strings_serializer.rb +33 -0
- data/lib/ad_localize/serializers/with_template.rb +19 -0
- data/lib/ad_localize/serializers/yaml_serializer.rb +13 -0
- data/lib/ad_localize/templates/android/strings.xml.erb +19 -0
- data/lib/ad_localize/templates/ios/InfoPlist.strings.erb +3 -0
- data/lib/ad_localize/templates/ios/Localizable.strings.erb +3 -0
- data/lib/ad_localize/templates/ios/Localizable.stringsdict.erb +41 -0
- data/lib/ad_localize/templates/properties/template.properties.erb +3 -0
- data/lib/ad_localize/version.rb +1 -1
- data/lib/ad_localize/view_models/translation_group_view_model.rb +15 -0
- data/lib/ad_localize/view_models/translation_view_model.rb +19 -0
- metadata +74 -11
- data/Makefile +0 -11
- data/lib/ad_localize/csv_file_manager.rb +0 -64
- data/lib/ad_localize/csv_parser.rb +0 -165
- data/lib/ad_localize/platform/android_formatter.rb +0 -70
- data/lib/ad_localize/platform/ios_formatter.rb +0 -143
- data/lib/ad_localize/platform/json_formatter.rb +0 -17
- data/lib/ad_localize/platform/platform_formatter.rb +0 -109
- data/lib/ad_localize/platform/yml_formatter.rb +0 -19
- data/lib/ad_localize/runner.rb +0 -55
@@ -0,0 +1,13 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
class JSONSerializer
|
4
|
+
def initialize
|
5
|
+
@locale_wording_to_hash = Mappers::LocaleWordingToHash.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def render(locale_wording:)
|
9
|
+
@locale_wording_to_hash.map(locale_wording: locale_wording).to_json
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
class LocalizableStringsSerializer
|
4
|
+
include WithTemplate
|
5
|
+
|
6
|
+
LOCALIZABLE_STRINGS_FILENAME = "Localizable.strings".freeze
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@translation_mapper = Mappers::IOSTranslationMapper.new
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def template_path
|
15
|
+
TEMPLATES_DIRECTORY + "/ios/#{LOCALIZABLE_STRINGS_FILENAME}.erb"
|
16
|
+
end
|
17
|
+
|
18
|
+
def hash_binding(locale_wording:)
|
19
|
+
{ translations: map_translations(translations: locale_wording.singulars) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def map_translations(translations:)
|
23
|
+
translations.map { |translation| @translation_mapper.map(translation: translation) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
class LocalizableStringsdictSerializer
|
4
|
+
include WithTemplate
|
5
|
+
|
6
|
+
LOCALIZABLE_STRINGSDICT_FILENAME = "Localizable.stringsdict".freeze
|
7
|
+
|
8
|
+
def initialize
|
9
|
+
@translation_mapper = Mappers::IOSTranslationMapper.new
|
10
|
+
@translation_group_mapper = Mappers::TranslationGroupMapper.new(translation_mapper: @translation_mapper)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def template_path
|
16
|
+
TEMPLATES_DIRECTORY + "/ios/#{LOCALIZABLE_STRINGSDICT_FILENAME}.erb"
|
17
|
+
end
|
18
|
+
|
19
|
+
def hash_binding(locale_wording:)
|
20
|
+
{
|
21
|
+
plurals: map_plurals(plurals: locale_wording.plurals),
|
22
|
+
adaptives: map_adaptives(adaptives: locale_wording.adaptives)
|
23
|
+
}
|
24
|
+
end
|
25
|
+
|
26
|
+
def map_plurals(plurals:)
|
27
|
+
plurals.map { |label, translations| @translation_group_mapper.map(label: label, translations: translations) }
|
28
|
+
end
|
29
|
+
|
30
|
+
def map_adaptives(adaptives:)
|
31
|
+
adaptives.map { |label, translations| @translation_group_mapper.map(label: label, translations: translations) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
class PropertiesSerializer
|
4
|
+
include WithTemplate
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@translation_mapper = Mappers::TranslationMapper.new
|
8
|
+
end
|
9
|
+
|
10
|
+
private
|
11
|
+
|
12
|
+
def template_path
|
13
|
+
TEMPLATES_DIRECTORY + "/properties/template.properties.erb"
|
14
|
+
end
|
15
|
+
|
16
|
+
def hash_binding(locale_wording:)
|
17
|
+
{ translations: map_translations(translations: locale_wording.singulars) }
|
18
|
+
end
|
19
|
+
|
20
|
+
def map_translations(translations:)
|
21
|
+
translations.map { |translation| @translation_mapper.map(translation: translation) }
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
class StringsSerializer
|
4
|
+
include WithTemplate
|
5
|
+
|
6
|
+
def initialize
|
7
|
+
@translation_mapper = Mappers::AndroidTranslationMapper.new
|
8
|
+
@translation_group_mapper = Mappers::TranslationGroupMapper.new(translation_mapper: @translation_mapper)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def template_path
|
14
|
+
TEMPLATES_DIRECTORY + "/android/strings.xml.erb"
|
15
|
+
end
|
16
|
+
|
17
|
+
def hash_binding(locale_wording:)
|
18
|
+
{
|
19
|
+
singulars: map_singulars(translations: locale_wording.singulars),
|
20
|
+
plurals: map_plurals(plurals: locale_wording.plurals)
|
21
|
+
}
|
22
|
+
end
|
23
|
+
|
24
|
+
def map_singulars(translations:)
|
25
|
+
translations.map { |translation| @translation_mapper.map(translation: translation) }
|
26
|
+
end
|
27
|
+
|
28
|
+
def map_plurals(plurals:)
|
29
|
+
plurals.map { |label, translations| @translation_group_mapper.map(label: label, translations: translations) }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
module WithTemplate
|
4
|
+
TEMPLATES_DIRECTORY = __dir__ + "/../templates"
|
5
|
+
|
6
|
+
def render(locale_wording:)
|
7
|
+
hash_binding = hash_binding(locale_wording: locale_wording)
|
8
|
+
return unless hash_binding
|
9
|
+
render_template(template_path: template_path, hash_binding: hash_binding)
|
10
|
+
end
|
11
|
+
|
12
|
+
def render_template(template_path:, hash_binding:)
|
13
|
+
template = File.read(template_path)
|
14
|
+
renderer = ERB.new(template, trim_mode: '-')
|
15
|
+
renderer.result_with_hash(hash_binding)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module Serializers
|
3
|
+
class YAMLSerializer
|
4
|
+
def initialize
|
5
|
+
@locale_wording_to_hash = Mappers::LocaleWordingToHash.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def render(locale_wording:)
|
9
|
+
@locale_wording_to_hash.map(locale_wording: locale_wording).to_yaml
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<resources>
|
3
|
+
<%- singulars.each do |single_translation| -%>
|
4
|
+
<%- if single_translation.comment -%>
|
5
|
+
<!-- <%= single_translation.comment %> -->
|
6
|
+
<%- end -%>
|
7
|
+
<string name="<%= single_translation.label %>"><%= single_translation.value %></string>
|
8
|
+
<%- end -%>
|
9
|
+
<%- plurals.each do |translation_group| -%>
|
10
|
+
<plurals name="<%= translation_group.label %>">
|
11
|
+
<%- translation_group.translation_view_models.each do |translation_view_model| -%>
|
12
|
+
<%- if translation_view_model.comment -%>
|
13
|
+
<!-- <%= translation_view_model.comment %> -->
|
14
|
+
<%- end -%>
|
15
|
+
<item quantity="<%= translation_view_model.key %>"><%= translation_view_model.value %></item>
|
16
|
+
<%- end -%>
|
17
|
+
</plurals>
|
18
|
+
<%- end -%>
|
19
|
+
</resources>
|
@@ -0,0 +1,41 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<plist>
|
3
|
+
<dict>
|
4
|
+
<%- plurals.each do |translation_group| -%>
|
5
|
+
<key><%= translation_group.label %></key>
|
6
|
+
<dict>
|
7
|
+
<key>NSStringLocalizedFormatKey</key>
|
8
|
+
<string>%#@key@</string>
|
9
|
+
<key>key</key>
|
10
|
+
<dict>
|
11
|
+
<key>NSStringFormatSpecTypeKey</key>
|
12
|
+
<string>NSStringPluralRuleType</string>
|
13
|
+
<key>NSStringFormatValueTypeKey</key>
|
14
|
+
<string>d</string>
|
15
|
+
<%- translation_group.translation_view_models.each do |translation_view_model| -%>
|
16
|
+
<%- if translation_view_model.comment -%>
|
17
|
+
<!-- <%= translation_view_model.comment %> -->
|
18
|
+
<%- end -%>
|
19
|
+
<key><%= translation_view_model.key -%></key>
|
20
|
+
<string><%= translation_view_model.value -%></string>
|
21
|
+
<%- end -%>
|
22
|
+
</dict>
|
23
|
+
</dict>
|
24
|
+
<%- end -%>
|
25
|
+
<%- adaptives.each do |translation_group| -%>
|
26
|
+
<key><%= translation_group.label %></key>
|
27
|
+
<dict>
|
28
|
+
<key>NSStringVariableWidthRuleType</key>
|
29
|
+
<dict>
|
30
|
+
<%- translation_group.translation_view_models.each do |translation_view_model| -%>
|
31
|
+
<%- if translation_view_model.comment -%>
|
32
|
+
<!-- <%= translation_view_model.comment %> -->
|
33
|
+
<%- end -%>
|
34
|
+
<key><%= translation_view_model.key %></key>
|
35
|
+
<string><%= translation_view_model.value %></string>
|
36
|
+
<%- end -%>
|
37
|
+
</dict>
|
38
|
+
</dict>
|
39
|
+
<%- end -%>
|
40
|
+
</dict>
|
41
|
+
</plist>
|
data/lib/ad_localize/version.rb
CHANGED
@@ -0,0 +1,15 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module ViewModels
|
3
|
+
class TranslationGroupViewModel
|
4
|
+
attr_reader(
|
5
|
+
:label,
|
6
|
+
:translation_view_models
|
7
|
+
)
|
8
|
+
|
9
|
+
def initialize(label:, translation_view_models:)
|
10
|
+
@label = label
|
11
|
+
@translation_view_models = translation_view_models
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module AdLocalize
|
2
|
+
module ViewModels
|
3
|
+
class TranslationViewModel
|
4
|
+
attr_reader(
|
5
|
+
:label,
|
6
|
+
:key,
|
7
|
+
:value,
|
8
|
+
:comment
|
9
|
+
)
|
10
|
+
|
11
|
+
def initialize(label:, key:, value:, comment:)
|
12
|
+
@label = label
|
13
|
+
@key = key
|
14
|
+
@value = value
|
15
|
+
@comment = comment
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ad_localize
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 4.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Edouard Siegel
|
@@ -160,6 +160,20 @@ dependencies:
|
|
160
160
|
- - "~>"
|
161
161
|
- !ruby/object:Gem::Version
|
162
162
|
version: '0.8'
|
163
|
+
- !ruby/object:Gem::Dependency
|
164
|
+
name: google-api-client
|
165
|
+
requirement: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - "~>"
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: '0.34'
|
170
|
+
type: :runtime
|
171
|
+
prerelease: false
|
172
|
+
version_requirements: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - "~>"
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0.34'
|
163
177
|
- !ruby/object:Gem::Dependency
|
164
178
|
name: googleauth
|
165
179
|
requirement: !ruby/object:Gem::Requirement
|
@@ -174,6 +188,20 @@ dependencies:
|
|
174
188
|
- - "~>"
|
175
189
|
- !ruby/object:Gem::Version
|
176
190
|
version: '0.12'
|
191
|
+
- !ruby/object:Gem::Dependency
|
192
|
+
name: mime-types
|
193
|
+
requirement: !ruby/object:Gem::Requirement
|
194
|
+
requirements:
|
195
|
+
- - "~>"
|
196
|
+
- !ruby/object:Gem::Version
|
197
|
+
version: '3.3'
|
198
|
+
type: :runtime
|
199
|
+
prerelease: false
|
200
|
+
version_requirements: !ruby/object:Gem::Requirement
|
201
|
+
requirements:
|
202
|
+
- - "~>"
|
203
|
+
- !ruby/object:Gem::Version
|
204
|
+
version: '3.3'
|
177
205
|
description: |-
|
178
206
|
AdLocalize produces localization files from platform agnostic wording.
|
179
207
|
Supported wording format : CSV. Supported export format: iOS, Android, JSON and YAML
|
@@ -194,7 +222,6 @@ files:
|
|
194
222
|
- Gemfile
|
195
223
|
- Gemfile.lock
|
196
224
|
- LICENSE.txt
|
197
|
-
- Makefile
|
198
225
|
- README.md
|
199
226
|
- Rakefile
|
200
227
|
- ad_localize.gemspec
|
@@ -204,17 +231,53 @@ files:
|
|
204
231
|
- exe/ad_localize
|
205
232
|
- lib/ad_localize.rb
|
206
233
|
- lib/ad_localize/ad_logger.rb
|
234
|
+
- lib/ad_localize/cli.rb
|
207
235
|
- lib/ad_localize/constant.rb
|
208
|
-
- lib/ad_localize/
|
209
|
-
- lib/ad_localize/
|
236
|
+
- lib/ad_localize/entities/key.rb
|
237
|
+
- lib/ad_localize/entities/locale_wording.rb
|
238
|
+
- lib/ad_localize/entities/translation.rb
|
239
|
+
- lib/ad_localize/entities/wording.rb
|
240
|
+
- lib/ad_localize/interactors/execute_export_request.rb
|
241
|
+
- lib/ad_localize/interactors/export_csv_files.rb
|
242
|
+
- lib/ad_localize/interactors/export_g_spreadsheet.rb
|
243
|
+
- lib/ad_localize/interactors/export_wording.rb
|
244
|
+
- lib/ad_localize/interactors/merge_wordings.rb
|
245
|
+
- lib/ad_localize/interactors/platforms/export_android_locale_wording.rb
|
246
|
+
- lib/ad_localize/interactors/platforms/export_ios_locale_wording.rb
|
247
|
+
- lib/ad_localize/interactors/platforms/export_json_locale_wording.rb
|
248
|
+
- lib/ad_localize/interactors/platforms/export_platform_factory.rb
|
249
|
+
- lib/ad_localize/interactors/platforms/export_properties_locale_wording.rb
|
250
|
+
- lib/ad_localize/interactors/platforms/export_yaml_locale_wording.rb
|
251
|
+
- lib/ad_localize/mappers/android_translation_mapper.rb
|
252
|
+
- lib/ad_localize/mappers/csv_path_to_wording.rb
|
253
|
+
- lib/ad_localize/mappers/ios_translation_mapper.rb
|
254
|
+
- lib/ad_localize/mappers/locale_wording_to_hash.rb
|
255
|
+
- lib/ad_localize/mappers/options_to_export_request.rb
|
256
|
+
- lib/ad_localize/mappers/translation_group_mapper.rb
|
257
|
+
- lib/ad_localize/mappers/translation_mapper.rb
|
258
|
+
- lib/ad_localize/mappers/value_range_to_wording.rb
|
210
259
|
- lib/ad_localize/option_handler.rb
|
211
|
-
- lib/ad_localize/
|
212
|
-
- lib/ad_localize/
|
213
|
-
- lib/ad_localize/
|
214
|
-
- lib/ad_localize/
|
215
|
-
- lib/ad_localize/
|
216
|
-
- lib/ad_localize/
|
260
|
+
- lib/ad_localize/repositories/file_system_repository.rb
|
261
|
+
- lib/ad_localize/repositories/g_sheets_repository.rb
|
262
|
+
- lib/ad_localize/requests/export_request.rb
|
263
|
+
- lib/ad_localize/requests/g_spreadsheet_options.rb
|
264
|
+
- lib/ad_localize/requests/merge_policy.rb
|
265
|
+
- lib/ad_localize/serializers/info_plist_serializer.rb
|
266
|
+
- lib/ad_localize/serializers/json_serializer.rb
|
267
|
+
- lib/ad_localize/serializers/localizable_strings_serializer.rb
|
268
|
+
- lib/ad_localize/serializers/localizable_stringsdict_serializer.rb
|
269
|
+
- lib/ad_localize/serializers/properties_serializer.rb
|
270
|
+
- lib/ad_localize/serializers/strings_serializer.rb
|
271
|
+
- lib/ad_localize/serializers/with_template.rb
|
272
|
+
- lib/ad_localize/serializers/yaml_serializer.rb
|
273
|
+
- lib/ad_localize/templates/android/strings.xml.erb
|
274
|
+
- lib/ad_localize/templates/ios/InfoPlist.strings.erb
|
275
|
+
- lib/ad_localize/templates/ios/Localizable.strings.erb
|
276
|
+
- lib/ad_localize/templates/ios/Localizable.stringsdict.erb
|
277
|
+
- lib/ad_localize/templates/properties/template.properties.erb
|
217
278
|
- lib/ad_localize/version.rb
|
279
|
+
- lib/ad_localize/view_models/translation_group_view_model.rb
|
280
|
+
- lib/ad_localize/view_models/translation_view_model.rb
|
218
281
|
homepage: https://github.com/applidium/ad_localize
|
219
282
|
licenses:
|
220
283
|
- MIT
|
@@ -234,7 +297,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
297
|
- !ruby/object:Gem::Version
|
235
298
|
version: '0'
|
236
299
|
requirements: []
|
237
|
-
rubygems_version: 3.
|
300
|
+
rubygems_version: 3.1.2
|
238
301
|
signing_key:
|
239
302
|
specification_version: 4
|
240
303
|
summary: AdLocalize helps with mobile and web applications wording
|
data/Makefile
DELETED
@@ -1,64 +0,0 @@
|
|
1
|
-
module AdLocalize
|
2
|
-
class CsvFileManager
|
3
|
-
CSV_CONTENT_TYPES = %w(text/csv text/plain)
|
4
|
-
|
5
|
-
class << self
|
6
|
-
def csv?(file)
|
7
|
-
!file.nil? && CSV_CONTENT_TYPES.include?(`file --brief --mime-type '#{file}'`.strip)
|
8
|
-
end
|
9
|
-
|
10
|
-
# Returns the downloaded file name (it is located in the current directory)
|
11
|
-
def download_from_drive(key, sheet, use_service_account=false)
|
12
|
-
LOGGER.log(:info, :green, "Downloading file from google drive...")
|
13
|
-
download_string_path = "./#{key}.csv"
|
14
|
-
begin
|
15
|
-
File.open(download_string_path, "wb") do |saved_file|
|
16
|
-
download_url = drive_download_url(key, sheet)
|
17
|
-
headers = {}
|
18
|
-
if use_service_account
|
19
|
-
LOGGER.log(:debug, :green, "Using a service account...")
|
20
|
-
token = service_account_access_token()
|
21
|
-
headers["Authorization"] = "#{token["token_type"]} #{token["access_token"]}"
|
22
|
-
end
|
23
|
-
# the following "open" is provided by open-uri
|
24
|
-
open(download_url, "rb", headers) do |read_file|
|
25
|
-
saved_file.write(read_file.read)
|
26
|
-
end
|
27
|
-
File.basename saved_file
|
28
|
-
end
|
29
|
-
rescue => e
|
30
|
-
delete_drive_file(download_string_path)
|
31
|
-
LOGGER.log(:error, :red, e.message)
|
32
|
-
exit
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def select_csvs(files)
|
37
|
-
files.select do |file|
|
38
|
-
LOGGER.log(:error, :red, "#{file} is not a csv. It will be ignored") unless self.csv?(file)
|
39
|
-
self.csv?(file)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
def delete_drive_file(file)
|
44
|
-
Pathname.new(file).delete unless file.nil?
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
def drive_download_url(key, sheet)
|
49
|
-
query_id = sheet ? "gid=#{sheet}" : "id=#{key}"
|
50
|
-
"https://docs.google.com/spreadsheets/d/#{key}/export?format=csv&#{query_id}"
|
51
|
-
end
|
52
|
-
private
|
53
|
-
def service_account_access_token()
|
54
|
-
scope = "https://www.googleapis.com/auth/drive.readonly"
|
55
|
-
json_key_io = StringIO.new ENV["GCLOUD_CLIENT_SECRET"]
|
56
|
-
authorizer = Google::Auth::ServiceAccountCredentials.make_creds(
|
57
|
-
json_key_io: json_key_io,
|
58
|
-
scope: scope
|
59
|
-
)
|
60
|
-
authorizer.fetch_access_token!
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|