bisu 1.4.7 → 1.5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 897fab8a33b0d7172fd27278af5d6e2536fcbeaf
4
- data.tar.gz: d0dcc27429ec315c3eab84401beff368e20e4549
3
+ metadata.gz: e11ef1004e3778da14f8a9980b7c179af2675386
4
+ data.tar.gz: 1f135d1bd101a886067a802cd3e01476f5023bf4
5
5
  SHA512:
6
- metadata.gz: 9b968c726b181da600f37c3083418f57dbc67240f96a8978ca27797fd630f853056c63c9897cf06fdf763db6a3c133c4944ccce0a45f0dbf00329cab3073d70d
7
- data.tar.gz: f1b5bc7755dedbdde632e63f83e7072115118bfd12ad6ae398e29ed43d11786e2e130d92d0d9097b15bf97094df014e87bb8c594417cc43ee31fc60f58cb8e55
6
+ metadata.gz: 46b10ad409ba99c19189e1ebb41fd6867475287471215c02cd2d8787f3f756bc6120391618c3af51a20fbcc7be0f17f7e1c1fd5040f5772b6cfc5c33ab5da2ed
7
+ data.tar.gz: 51e31ad74b7e9e816966c52da0e42dee25e8305548aeea95a91de413573c11c97144a3e38da852893e3b000d89047d5255c27e6023473b28c0d12c677ef58a9b
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  `Bisu` adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [1.5.0](https://github.com/hole19/bisu/releases/tag/v1.5.0)
6
+ Released on 2019/01/19
7
+
8
+ #### Added
9
+ - Adds an option to skip downloading i18n dictionary from an external source and loads it directly from a local file in that format
10
+
5
11
  ## [1.4.7](https://github.com/hole19/bisu/releases/tag/v1.4.7)
6
12
  Released on 2018/07/24
7
13
 
data/lib/bisu.rb CHANGED
@@ -5,8 +5,8 @@ require 'fileutils'
5
5
  require 'bisu/logger'
6
6
  require 'bisu/object_extension'
7
7
  require 'bisu/config'
8
- require 'bisu/google_sheet'
9
- require 'bisu/one_sky'
8
+ require 'bisu/source/google_sheet'
9
+ require 'bisu/source/one_sky'
10
10
  require 'bisu/dictionary'
11
11
  require 'bisu/localizer'
12
12
  require 'bisu/version'
@@ -19,7 +19,7 @@ module Bisu
19
19
 
20
20
  if config_file = open_file("translatable.yml", "r", true)
21
21
  config = Bisu::Config.new(hash: YAML::load(config_file))
22
- dictionary = dictionary_for(config: config.dictionary, save_to_path: options[:dictionary_save_path])
22
+ dictionary = dictionary_for(config: config.dictionary, options: options)
23
23
  localizer = Bisu::Localizer.new(dictionary, config.type)
24
24
 
25
25
  config.localize_files do |in_path, out_path, language, locale|
@@ -37,24 +37,43 @@ module Bisu
37
37
 
38
38
  private
39
39
 
40
- def dictionary_for(config:, save_to_path:)
40
+ def dictionary_for(config:, options:)
41
+ source =
42
+ if from_file_path = options[:from_file_path]
43
+ i18n_from(path: from_file_path)
44
+ else
45
+ i18n_for(config: config, options: options)
46
+ end
47
+
48
+ Bisu::Dictionary.new(source)
49
+ end
50
+
51
+ def i18n_for(config:, options:)
41
52
  source =
42
53
  case config[:type]
43
54
  when "google_sheet"
44
- Bisu::GoogleSheet.new(config[:sheet_id], config[:keys_column])
55
+ Bisu::Source::GoogleSheet.new(config[:sheet_id], config[:keys_column])
45
56
  when "one_sky"
46
- Bisu::OneSky.new(config[:api_key], config[:api_secret], config[:project_id], config[:file_name])
57
+ Bisu::Source::OneSky.new(config[:api_key], config[:api_secret], config[:project_id], config[:file_name])
47
58
  end
48
59
 
49
60
  source = source.to_i18
50
61
 
62
+ save_to_path = options[:dictionary_save_path]
51
63
  if save_to_path && file = open_file(save_to_path, "w", false)
52
64
  file.write(source.to_json)
53
65
  file.flush
54
66
  file.close
55
67
  end
56
68
 
57
- Bisu::Dictionary.new(source)
69
+ source
70
+ end
71
+
72
+ def i18n_from(path:)
73
+ file = open_file(path, "r", true)
74
+ data = file.read
75
+ file.close
76
+ JSON.parse(data)
58
77
  end
59
78
 
60
79
  def command_line_options(options)
@@ -65,6 +84,10 @@ module Bisu
65
84
  opts_hash[:default_language] = language
66
85
  end
67
86
 
87
+ opts.on("--file PATH", "Loads i18n source directly from a local file") do |file|
88
+ opts_hash[:from_file_path] = file
89
+ end
90
+
68
91
  opts.on("--save-dictionary PATH", "Save downloaded dictionary locally at given path") do |path|
69
92
  opts_hash[:dictionary_save_path] = path
70
93
  end
@@ -0,0 +1,65 @@
1
+ require "net/https"
2
+ require "xmlsimple"
3
+
4
+ module Bisu
5
+ module Source
6
+ class GoogleSheet
7
+ def initialize(sheet_id, keys_column)
8
+ @sheet_id = sheet_id
9
+ @key_column = keys_column
10
+ end
11
+
12
+ def to_i18
13
+ raw = raw_data(@sheet_id)
14
+
15
+ Logger.info("Downloading dictionary from Google Sheet...")
16
+
17
+ non_language_columns = ["id", "updated", "category", "title", "content", "link", @key_column]
18
+
19
+ kb = {}
20
+ raw["entry"].each do |entry|
21
+ unless (key = entry[@key_column]) && key = key.first
22
+ raise "Cannot find key column '#{@key_column}'"
23
+ end
24
+
25
+ entry.select { |c| !non_language_columns.include?(c) }.each do |lang, texts|
26
+ kb[lang] ||= {}
27
+ kb[lang][key] = texts.first unless texts.first == {}
28
+ end
29
+ end
30
+
31
+ Logger.info("Google Sheet parsed successfully!")
32
+ Logger.info("Found #{kb.count} languages.")
33
+
34
+ kb
35
+ end
36
+
37
+ private
38
+
39
+ def xml_data(uri, headers=nil)
40
+ uri = URI.parse(uri)
41
+ http = Net::HTTP.new(uri.host, uri.port)
42
+ http.use_ssl = true
43
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
44
+ data = http.get(uri.path, headers)
45
+
46
+ unless data.code.to_i == 200
47
+ raise "Cannot access sheet at #{uri} - HTTP #{data.code}"
48
+ end
49
+
50
+ begin
51
+ XmlSimple.xml_in(data.body, 'KeyAttr' => 'name')
52
+ rescue
53
+ raise "Cannot parse. Expected XML at #{uri}"
54
+ end
55
+ end
56
+
57
+ def raw_data(sheet_id)
58
+ Logger.info("Downloading Google Sheet...")
59
+ sheet = xml_data("https://spreadsheets.google.com/feeds/worksheets/#{sheet_id}/public/full")
60
+ url = sheet["entry"][0]["link"][0]["href"]
61
+ xml_data(url)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,65 @@
1
+ require 'net/https'
2
+ require 'json'
3
+
4
+ module Bisu
5
+ module Source
6
+ class OneSky
7
+ def initialize(api_key, api_secret, project_id, file_name)
8
+ @api_key = api_key
9
+ @api_secret = api_secret
10
+ @project_id = project_id
11
+ @file_name = file_name
12
+ end
13
+
14
+ def to_i18
15
+ Logger.info("Downloading dictionary from OneSky...")
16
+
17
+ path = "https://platform.api.onesky.io/1/projects/#{@project_id}/translations/multilingual"
18
+ file = get(path, source_file_name: @file_name, file_format: "I18NEXT_MULTILINGUAL_JSON")
19
+
20
+ hash = JSON.parse(file)
21
+
22
+ hash.each do |lang, v|
23
+ hash[lang] = v["translation"]
24
+ hash[lang].each do |key, text|
25
+ hash[lang][key] = hash[lang][key].join("\\n") if hash[lang][key].is_a? Array
26
+ hash[lang][key] = hash[lang][key].gsub("\n", "\\n") # fixes the 'stupid newline bug'
27
+ hash[lang][key] = hash[lang][key].gsub("\\'", "'") # fixes the 'stupid single quote bug'
28
+ end
29
+ end
30
+
31
+ Logger.info("OneSky response parsed successfully!")
32
+ Logger.info("Found #{hash.count} languages.")
33
+
34
+ hash
35
+ end
36
+
37
+ private
38
+
39
+ def get(url, params)
40
+ uri = URI(url)
41
+ uri.query = URI.encode_www_form(authenticated_params(params))
42
+
43
+ http = Net::HTTP.new(uri.host, uri.port)
44
+ http.use_ssl = true
45
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
46
+
47
+ request = Net::HTTP::Get.new(uri.request_uri)
48
+ response = http.request(request)
49
+
50
+ raise "OneSky: Http Error #{JSON.parse(response.body)}" if response.code.to_i >= 400
51
+
52
+ response.body
53
+ end
54
+
55
+ def authenticated_params(params)
56
+ now = Time.now.to_i
57
+
58
+ { api_key: @api_key,
59
+ timestamp: now,
60
+ dev_hash: Digest::MD5.hexdigest(now.to_s + @api_secret)
61
+ }.merge(params)
62
+ end
63
+ end
64
+ end
65
+ end
data/lib/bisu/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Bisu
2
- VERSION = '1.4.7'
3
- VERSION_UPDATED_AT = '2018-07-24'
2
+ VERSION = '1.5.0'
3
+ VERSION_UPDATED_AT = '2019-01-19'
4
4
  end
@@ -1,5 +1,5 @@
1
- describe Bisu::GoogleSheet do
2
- subject(:to_i18) { Bisu::GoogleSheet.new(sheet_id, key_column).to_i18 }
1
+ describe Bisu::Source::GoogleSheet do
2
+ subject(:to_i18) { Bisu::Source::GoogleSheet.new(sheet_id, key_column).to_i18 }
3
3
 
4
4
  let(:sheet_id) { "abc1234567890" }
5
5
  let(:url_info) { "https://spreadsheets.google.com/feeds/worksheets/#{sheet_id}/public/full" }
@@ -1,5 +1,5 @@
1
- describe Bisu::OneSky do
2
- subject(:to_i18) { Bisu::OneSky.new(api_key, api_secret, project_id, file_name).to_i18 }
1
+ describe Bisu::Source::OneSky do
2
+ subject(:to_i18) { Bisu::Source::OneSky.new(api_key, api_secret, project_id, file_name).to_i18 }
3
3
 
4
4
  let(:api_key) { "a123" }
5
5
  let(:api_secret) { "b123" }
@@ -6,7 +6,7 @@ describe Bisu do
6
6
 
7
7
  before do
8
8
  allow(Bisu).to receive(:open_file).and_return(file)
9
- allow_any_instance_of(Bisu::GoogleSheet).to receive(:to_i18).and_return({
9
+ allow_any_instance_of(Bisu::Source::GoogleSheet).to receive(:to_i18).and_return({
10
10
  "english" => { "kKey" => "Value" }
11
11
  })
12
12
  allow(Bisu).to receive(:localize_file)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bisu
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.7
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - joaoffcosta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-07-24 00:00:00.000000000 Z
11
+ date: 2019-01-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: safe_yaml
@@ -87,11 +87,11 @@ files:
87
87
  - lib/bisu.rb
88
88
  - lib/bisu/config.rb
89
89
  - lib/bisu/dictionary.rb
90
- - lib/bisu/google_sheet.rb
91
90
  - lib/bisu/localizer.rb
92
91
  - lib/bisu/logger.rb
93
92
  - lib/bisu/object_extension.rb
94
- - lib/bisu/one_sky.rb
93
+ - lib/bisu/source/google_sheet.rb
94
+ - lib/bisu/source/one_sky.rb
95
95
  - lib/bisu/version.rb
96
96
  - spec/fixtures/sample.translatable.yml
97
97
  - spec/fixtures/sample_kb_public_info.html
@@ -1,63 +0,0 @@
1
- require "net/https"
2
- require "xmlsimple"
3
-
4
- module Bisu
5
- class GoogleSheet
6
- def initialize(sheet_id, keys_column)
7
- @sheet_id = sheet_id
8
- @key_column = keys_column
9
- end
10
-
11
- def to_i18
12
- raw = raw_data(@sheet_id)
13
-
14
- Logger.info("Downloading dictionary from Google Sheet...")
15
-
16
- non_language_columns = ["id", "updated", "category", "title", "content", "link", @key_column]
17
-
18
- kb = {}
19
- raw["entry"].each do |entry|
20
- unless (key = entry[@key_column]) && key = key.first
21
- raise "Cannot find key column '#{@key_column}'"
22
- end
23
-
24
- entry.select { |c| !non_language_columns.include?(c) }.each do |lang, texts|
25
- kb[lang] ||= {}
26
- kb[lang][key] = texts.first unless texts.first == {}
27
- end
28
- end
29
-
30
- Logger.info("Google Sheet parsed successfully!")
31
- Logger.info("Found #{kb.count} languages.")
32
-
33
- kb
34
- end
35
-
36
- private
37
-
38
- def xml_data(uri, headers=nil)
39
- uri = URI.parse(uri)
40
- http = Net::HTTP.new(uri.host, uri.port)
41
- http.use_ssl = true
42
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
43
- data = http.get(uri.path, headers)
44
-
45
- unless data.code.to_i == 200
46
- raise "Cannot access sheet at #{uri} - HTTP #{data.code}"
47
- end
48
-
49
- begin
50
- XmlSimple.xml_in(data.body, 'KeyAttr' => 'name')
51
- rescue
52
- raise "Cannot parse. Expected XML at #{uri}"
53
- end
54
- end
55
-
56
- def raw_data(sheet_id)
57
- Logger.info("Downloading Google Sheet...")
58
- sheet = xml_data("https://spreadsheets.google.com/feeds/worksheets/#{sheet_id}/public/full")
59
- url = sheet["entry"][0]["link"][0]["href"]
60
- xml_data(url)
61
- end
62
- end
63
- end
data/lib/bisu/one_sky.rb DELETED
@@ -1,63 +0,0 @@
1
- require 'net/https'
2
- require 'json'
3
-
4
- module Bisu
5
- class OneSky
6
- def initialize(api_key, api_secret, project_id, file_name)
7
- @api_key = api_key
8
- @api_secret = api_secret
9
- @project_id = project_id
10
- @file_name = file_name
11
- end
12
-
13
- def to_i18
14
- Logger.info("Downloading dictionary from OneSky...")
15
-
16
- path = "https://platform.api.onesky.io/1/projects/#{@project_id}/translations/multilingual"
17
- file = get(path, source_file_name: @file_name, file_format: "I18NEXT_MULTILINGUAL_JSON")
18
-
19
- hash = JSON.parse(file)
20
-
21
- hash.each do |lang, v|
22
- hash[lang] = v["translation"]
23
- hash[lang].each do |key, text|
24
- hash[lang][key] = hash[lang][key].join("\\n") if hash[lang][key].is_a? Array
25
- hash[lang][key] = hash[lang][key].gsub("\n", "\\n") # fixes the 'stupid newline bug'
26
- hash[lang][key] = hash[lang][key].gsub("\\'", "'") # fixes the 'stupid single quote bug'
27
- end
28
- end
29
-
30
- Logger.info("OneSky response parsed successfully!")
31
- Logger.info("Found #{hash.count} languages.")
32
-
33
- hash
34
- end
35
-
36
- private
37
-
38
- def get(url, params)
39
- uri = URI(url)
40
- uri.query = URI.encode_www_form(authenticated_params(params))
41
-
42
- http = Net::HTTP.new(uri.host, uri.port)
43
- http.use_ssl = true
44
- http.verify_mode = OpenSSL::SSL::VERIFY_NONE
45
-
46
- request = Net::HTTP::Get.new(uri.request_uri)
47
- response = http.request(request)
48
-
49
- raise "OneSky: Http Error #{JSON.parse(response.body)}" if response.code.to_i >= 400
50
-
51
- response.body
52
- end
53
-
54
- def authenticated_params(params)
55
- now = Time.now.to_i
56
-
57
- { api_key: @api_key,
58
- timestamp: now,
59
- dev_hash: Digest::MD5.hexdigest(now.to_s + @api_secret)
60
- }.merge(params)
61
- end
62
- end
63
- end