singleton-ruby 0.0.3 → 0.0.4

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
  SHA256:
3
- metadata.gz: 9948a8ed190dc317c552c9dd9ca2f2f1579aa7903765562b44fe8ec4684f7531
4
- data.tar.gz: 037a56320794f476e2aa4de9b09a50f52d348ce652838a30604a613b6cbe2f4b
3
+ metadata.gz: ea69ecdb18c928648d4f13c86c49c28eb06ac6debbff0a6b81d00a3aa22a470a
4
+ data.tar.gz: 5591f0fd49b0dd594b292fed1d4afce9bba3ab60bb23a3aa697a0b0c93957eb3
5
5
  SHA512:
6
- metadata.gz: 4d568de3d1de3df32358f2ae5232868beb28de9e59d41d7d470473ebf789a4d868e6d8a1ef10cff24d4e59375f907a46d622c3180e00b77d95af64ffc246f4ec
7
- data.tar.gz: 6f4a695dacfc078c449070fec32992f133c39abeca3a9a1a093258014f0e3db24d92da36635fde18869817541c356c89ef0ecdf697e75c24c3aa273691bbf904
6
+ metadata.gz: 10c04aa1bdd5d23a8057fabe04d4f9e9736d8a109b27042bb8f8133cbe6bbdce3036f4bf9c2e31b9773f48285ffde8c5e5bd590f01c71c72d6c2c9f522228c1a
7
+ data.tar.gz: 83393a302754efa12fcb739756b57fec8b4f957f6903357bb091a5f6497740d43544fbffa4bc7ce717c9fedd4eeeb1b041945fc0b0638eb5725f7ce07b489fdf
@@ -0,0 +1,65 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module SgtnClient
5
+
6
+ autoload :CacheUtil, "sgtn-client/util/cache-util"
7
+
8
+ class Source
9
+
10
+ def self.getSource(component, key)
11
+ locale = "en"
12
+ cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
13
+ items = SgtnClient::CacheUtil.get_cache(cache_key)
14
+ if items.nil?
15
+ items = getBundle(component)
16
+ SgtnClient.logger.debug "Putting sources items into cache with key: " + cache_key
17
+ SgtnClient::CacheUtil.write_cache(cache_key, items)
18
+ else
19
+ SgtnClient.logger.debug "Getting sources from cache with key: " + cache_key
20
+ end
21
+ if items.nil?
22
+ return key
23
+ end
24
+ str = items[locale][key]
25
+ return str
26
+ end
27
+
28
+ def self.loadBundles(locale)
29
+ env = SgtnClient::Config.default_environment
30
+ source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
31
+ SgtnClient.logger.debug "Loading bundles from path: " + source_bundle
32
+ Dir.children(source_bundle).each do |component|
33
+ yamlfile = File.join(source_bundle, component + "/" + locale + ".yml")
34
+ bundle = read_yml(yamlfile)
35
+ cachekey = SgtnClient::CacheUtil.get_cachekey(component, locale)
36
+ SgtnClient::CacheUtil.write_cache(cachekey,bundle)
37
+ end
38
+
39
+ end
40
+
41
+ private
42
+
43
+ def self.getBundle(component)
44
+ env = SgtnClient::Config.default_environment
45
+ source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
46
+ bundlepath = source_bundle + "/" + component + "/en.yml"
47
+ SgtnClient.logger.debug "Getting source from bundle: " + bundlepath
48
+ begin
49
+ bundle = read_yaml(bundlepath)
50
+ puts bundle
51
+ rescue => exception
52
+ SgtnClient.logger.error exception.message
53
+ end
54
+ return bundle
55
+ end
56
+
57
+ def self.read_yml(file_name)
58
+ erb = ERB.new(File.read(file_name))
59
+ erb.filename = file_name
60
+ YAML.load(erb.result)
61
+ end
62
+
63
+ end
64
+
65
+ end
@@ -5,25 +5,31 @@ module SgtnClient
5
5
  module Core
6
6
  autoload :Request, "sgtn-client/core/request"
7
7
  autoload :Cache, "sgtn-client/core/cache"
8
+ autoload :CacheUtil, "sgtn-client/util/cache-util"
8
9
  end
9
10
 
10
11
  class Translation
11
12
 
12
- include Logging
13
-
14
13
  def self.getString(component, key, locale)
15
- cache_key = get_cachekey(component, locale)
16
- SgtnClient::Core::Cache.initialize()
17
- items = get_cache(cache_key)
14
+ cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
15
+ items = SgtnClient::CacheUtil.get_cache(cache_key)
18
16
  if items.nil?
19
17
  items = getTranslations(component, locale)
20
- write_cache(cache_key, items)
18
+ SgtnClient::CacheUtil.write_cache(cache_key, items)
19
+ else
20
+ SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
21
21
  end
22
22
  if items.nil?
23
23
  return key
24
24
  end
25
25
  str = items["messages"][key]
26
- return str
26
+
27
+ if str.nil?
28
+ return SgtnClient::Source.getSource(component, key)
29
+ else
30
+ return str
31
+ end
32
+
27
33
  end
28
34
 
29
35
  private
@@ -42,9 +48,9 @@ module SgtnClient
42
48
  env = SgtnClient::Config.default_environment
43
49
  product_name = SgtnClient::Config.configurations[env]["product_name"]
44
50
  version = SgtnClient::Config.configurations[env]["version"]
45
- offline_bundle = SgtnClient::Config.configurations[env]["offline_bundle"]
46
- bundlepath = offline_bundle + "/l10n/bundles/" + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
47
- SgtnClient.logger.info "Getting translations from offline bundle: " + bundlepath
51
+ translation_bundle = SgtnClient::Config.configurations[env]["translation_bundle"]
52
+ bundlepath = translation_bundle + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
53
+ SgtnClient.logger.debug "Getting translations from offline bundle: " + bundlepath
48
54
  begin
49
55
  file = File.read(bundlepath)
50
56
  data_hash = JSON.parse(file)
@@ -58,7 +64,7 @@ module SgtnClient
58
64
  env = SgtnClient::Config.default_environment
59
65
  product_name = SgtnClient::Config.configurations[env]["product_name"]
60
66
  vip_server = SgtnClient::Config.configurations[env]["vip_server"]
61
- SgtnClient.logger.info "Getting translations from server: " + vip_server
67
+ SgtnClient.logger.debug "Getting translations from server: " + vip_server
62
68
  version = SgtnClient::Config.configurations[env]["version"]
63
69
  url = vip_server + "/i18n/api/v2/translation/products/" + product_name + "/versions/" + version + "/locales/" + locale + "/components/" + component+ "?checkTranslationStatus=false&machineTranslation=false&pseudo=false"
64
70
  begin
@@ -72,21 +78,6 @@ module SgtnClient
72
78
  return obj
73
79
  end
74
80
 
75
- def self.get_cache(cache_key)
76
- items = SgtnClient::Core::Cache.get(cache_key)
77
- return items
78
- end
79
-
80
- def self.write_cache(cache_key, items)
81
- SgtnClient::Core::Cache.put(cache_key, items, 144000)
82
- end
83
-
84
- def self.get_cachekey(component, locale)
85
- env = SgtnClient::Config.default_environment
86
- product_name = SgtnClient::Config.configurations[env]["product_name"]
87
- version = SgtnClient::Config.configurations[env]["version"]
88
- return product_name + "_" + version + "_" + component + "_" + locale
89
- end
90
81
  end
91
82
 
92
83
  end
@@ -23,7 +23,8 @@ module SgtnClient::Core
23
23
 
24
24
  def self.put(key, value, ttl=nil)
25
25
  ttl ||= @opts[:ttl]
26
- $data[key] = Entry.new(DateTime.new + Rational(ttl, 1440), value)
26
+ # hours from new
27
+ $data[key] = Entry.new(DateTime.new + Rational(ttl, 24), value)
27
28
  end
28
29
 
29
30
  def self.delete(key)
@@ -39,7 +39,8 @@ module SgtnClient
39
39
  :mode, :endpoint, :merchant_endpoint, :platform_endpoint, :ipn_endpoint,
40
40
  :rest_endpoint, :rest_token_endpoint, :client_id, :client_secret,
41
41
  :openid_endpoint, :openid_redirect_uri, :openid_client_id, :openid_client_secret,
42
- :verbose_logging, :product_name, :version, :vip_server, :bundle_mode, :offline_bundle
42
+ :verbose_logging, :product_name, :version, :vip_server, :bundle_mode,
43
+ :translation_bundle, :source_bundle
43
44
 
44
45
 
45
46
  # Create Config object
@@ -38,7 +38,7 @@ module SgtnClient
38
38
  def logger=(logger)
39
39
  @logger = logger
40
40
  if Config.config.mode.eql? 'live' and @logger.level == Logger::DEBUG
41
- @logger.warn "DEBUG log level not allowed in sandbox mode for security of confidential information. Changing log level to INFO..."
41
+ @logger.warn "DEBUG log level not allowed in live mode for security of confidential information. Changing log level to INFO..."
42
42
  @logger.level = Logger::INFO
43
43
  end
44
44
  end
@@ -1,6 +1,9 @@
1
1
  module SgtnClient
2
-
2
+ module Core
3
+ autoload :Cache, "sgtn-client/core/cache"
4
+ end
3
5
  autoload :Translation, "sgtn-client/api/translation"
6
+ autoload :Source, "sgtn-client/api/source"
4
7
  autoload :Config, "sgtn-client/core/config"
5
8
  autoload :Logging, "sgtn-client/core/logging"
6
9
  autoload :Exceptions, "sgtn-client/core/exceptions"
@@ -12,6 +15,7 @@ module SgtnClient
12
15
 
13
16
  include Logging
14
17
  def load(*args)
18
+ # load configuration file
15
19
  begin
16
20
  SgtnClient::Config.load(args[0], args[1])
17
21
  rescue => exception
@@ -21,6 +25,7 @@ module SgtnClient
21
25
  log.error exception.message
22
26
  end
23
27
 
28
+ # create log file
24
29
  file = './sgtnclient_d.log'
25
30
  if args[2] != nil
26
31
  file = args[2]
@@ -28,6 +33,19 @@ module SgtnClient
28
33
  file = File.open(file, 'a')
29
34
  file.sync = true
30
35
  SgtnClient.logger = Logger.new(file)
36
+
37
+ # Set log level for sandbox mode
38
+ env = SgtnClient::Config.default_environment
39
+ mode = SgtnClient::Config.configurations[env]["mode"]
40
+ SgtnClient.logger.info "Current mode is: " + mode
41
+ if mode == 'sandbox'
42
+ SgtnClient.logger.level = Logger::DEBUG
43
+ else
44
+ SgtnClient.logger.level = Logger::INFO
45
+ end
46
+
47
+ # initialize cache
48
+ SgtnClient::Core::Cache.initialize()
31
49
  end
32
50
 
33
51
  def logger
@@ -0,0 +1,30 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module SgtnClient
5
+
6
+ module Core
7
+ autoload :Cache, "sgtn-client/core/cache"
8
+ end
9
+
10
+ class CacheUtil
11
+
12
+ def self.get_cache(cache_key)
13
+ items = SgtnClient::Core::Cache.get(cache_key)
14
+ return items
15
+ end
16
+
17
+ def self.write_cache(cache_key, items)
18
+ # expired after 24 hours
19
+ SgtnClient::Core::Cache.put(cache_key, items, 24)
20
+ end
21
+
22
+ def self.get_cachekey(component, locale)
23
+ env = SgtnClient::Config.default_environment
24
+ product_name = SgtnClient::Config.configurations[env]["product_name"]
25
+ version = SgtnClient::Config.configurations[env]["version"]
26
+ return product_name + "_" + version + "_" + component + "_" + locale
27
+ end
28
+ end
29
+
30
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
 
2
- VERSION_INFO = [0, 0, 3].freeze
2
+ VERSION_INFO = [0, 0, 4].freeze
3
3
  VERSION = VERSION_INFO.map(&:to_s).join('.').freeze
@@ -0,0 +1,2 @@
1
+ en:
2
+ hello: "Hello world"
@@ -29,7 +29,10 @@ test: &default
29
29
  bundle_mode: offline
30
30
 
31
31
  # # Offline Bundle Path
32
- offline_bundle: ./spec/config/locales
32
+ translation_bundle: ./spec/config/locales/l10n/bundles
33
+
34
+ # # Offline Bundle Path
35
+ source_bundle: ./spec/config/locales/default
33
36
 
34
37
  development:
35
38
  <<: *default
@@ -25,8 +25,7 @@ describe SgtnClient::Logging do
25
25
 
26
26
  it "write message to logger" do
27
27
  test_message = "Example log message!!!"
28
- SgtnClient.logger.info(test_message)
29
- SgtnClient.logger.info "Example log message!!!!"
28
+ SgtnClient.logger.debug(test_message)
30
29
  # @logger_file.rewind
31
30
  # expect(@logger_file.read).to match test_message
32
31
  end
@@ -5,11 +5,20 @@ describe SgtnClient do
5
5
 
6
6
  before :each do
7
7
  env = SgtnClient::Config.default_environment
8
- SgtnClient::Config.configurations[env]["bundle_mode"] = 'offline'
8
+ SgtnClient::Config.configurations[env]["bundle_mode"] = 'online'
9
+ SgtnClient::Source.loadBundles("en")
9
10
  end
10
11
 
11
12
  it "GET" do
12
13
  expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
14
+ # get from cache in 2nd time
15
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
16
+ end
17
+
18
+ it "NonExistingKey" do
19
+ expect(SgtnClient::Translation.getString("JAVA", "hello", "zh-Hans")).to eq 'Hello world'
20
+ # get from cache in 2nd time
21
+ expect(SgtnClient::Translation.getString("JAVA", "hello", "zh-Hans")).to eq 'Hello world'
13
22
  end
14
23
  end
15
24
 
@@ -1,15 +1,24 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe SgtnClient do
4
- describe "OnelineAPI" do
4
+ describe "OnlineAPI" do
5
5
 
6
6
  before :each do
7
7
  env = SgtnClient::Config.default_environment
8
8
  SgtnClient::Config.configurations[env]["bundle_mode"] = 'online'
9
+ SgtnClient::Source.loadBundles("en")
9
10
  end
10
11
 
11
12
  it "GET" do
12
13
  expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
14
+ # get from cache in 2nd time
15
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
16
+ end
17
+
18
+ it "NonExistingKey" do
19
+ expect(SgtnClient::Translation.getString("JAVA", "hello", "zh-Hans")).to eq 'Hello world'
20
+ # get from cache in 2nd time
21
+ expect(SgtnClient::Translation.getString("JAVA", "hello", "zh-Hans")).to eq 'Hello world'
13
22
  end
14
23
  end
15
24
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: singleton-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - VMware G11n Team
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-05-24 00:00:00.000000000 Z
11
+ date: 2021-05-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -216,6 +216,7 @@ files:
216
216
  - lib/generators/sgtnclient/install_generator.rb
217
217
  - lib/generators/sgtnclient/templates/sgtnclient.rb
218
218
  - lib/generators/sgtnclient/templates/sgtnclient.yml
219
+ - lib/sgtn-client/api/source.rb
219
220
  - lib/sgtn-client/api/translation.rb
220
221
  - lib/sgtn-client/core/cache.rb
221
222
  - lib/sgtn-client/core/config.rb
@@ -223,8 +224,10 @@ files:
223
224
  - lib/sgtn-client/core/logging.rb
224
225
  - lib/sgtn-client/core/request.rb
225
226
  - lib/sgtn-client/sgtn-client.rb
227
+ - lib/sgtn-client/util/cache-util.rb
226
228
  - lib/singleton-ruby.rb
227
229
  - lib/version.rb
230
+ - spec/config/locales/default/JAVA/en.yml
228
231
  - spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_de.json
229
232
  - spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_en.json
230
233
  - spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_latest.json
@@ -266,6 +269,7 @@ signing_key:
266
269
  specification_version: 4
267
270
  summary: Singleton client for Ruby.
268
271
  test_files:
272
+ - spec/config/locales/default/JAVA/en.yml
269
273
  - spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_de.json
270
274
  - spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_en.json
271
275
  - spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_latest.json