singleton-client 0.7.1 → 0.7.2

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
  SHA256:
3
- metadata.gz: a3d1aaec2ddcbbb860b477feb06dedcb64b5ec626864c2a89c31e38451e6069f
4
- data.tar.gz: c1d3e0125bed7151a337eb8f595a08e74424d2a04b0b6ce3b53d3aac8c66ae49
3
+ metadata.gz: 7b866f4160d99e04ac3140bf8da0e7f15abf5db1fac6360d2e5d8f8a6c392eb8
4
+ data.tar.gz: 7e4977b0480ef04e774a2891fafee6d6b76fc1abbafd436e718f9846c138d545
5
5
  SHA512:
6
- metadata.gz: 16da077e3ed50925a0cd0fbd6865c4a9ba05c66bf2dca1b5911a9c494f3669b823dc9653688a80be1b285b1d300dfa70a16524d45bfa9b0079c63c6ecf16f5bb
7
- data.tar.gz: 3079b1570e08d8b622013d5e7431ed98339d14f5be83c2c108a649f18f968e4a1d903459861ba4a3f4d29acd553ac706ed6c4d1274769cc9ed1c57c44c846da8
6
+ metadata.gz: e34e259dbf04c59adb5ab96e3fde9d73a3918f9a7d284e5a33313185c1e6afbd25e43c850e3eedabbdb8602c6b32bbdb3cb76efee3536d1d7d615ddd43e94316
7
+ data.tar.gz: 73f14c209b98942d919c6037b31dd5f14ac44350609e8ab42d7afdc75098a42072794b40266145c44080c936c1a90aa86b7cea4d6e2b85053d30bf2db7d22324
@@ -1,5 +1,3 @@
1
- require 'erb'
2
- require 'yaml'
3
1
 
4
2
  module SgtnClient
5
3
 
@@ -17,11 +15,14 @@ module SgtnClient
17
15
  else
18
16
  SgtnClient.logger.debug "Getting sources from cache with key: " + cache_key
19
17
  end
20
- if items.nil?
21
- return key
18
+ s = items[locale][key]
19
+ if items.nil? || s.nil?
20
+ SgtnClient.logger.debug "Source not found, return key: " + key
21
+ #return key
22
+ return nil
23
+ else
24
+ return s
22
25
  end
23
- str = items[locale][key]
24
- return str
25
26
  end
26
27
 
27
28
  def self.getSources(component, locale)
@@ -42,9 +43,10 @@ module SgtnClient
42
43
  SgtnClient::Config.configurations.default = locale
43
44
  source_bundle = SgtnClient::Config.configurations[env]["source_bundle"]
44
45
  SgtnClient.logger.debug "Loading [" + locale + "] source bundles from path: " + source_bundle
45
- Dir.children(source_bundle).each do |component|
46
+ Dir.foreach(source_bundle) do |component|
47
+ next if component == '.' || component == '..'
46
48
  yamlfile = File.join(source_bundle, component + "/" + locale + ".yml")
47
- bundle = read_yml(yamlfile)
49
+ bundle = SgtnClient::FileUtil.read_yml(yamlfile)
48
50
  cachekey = SgtnClient::CacheUtil.get_cachekey(component, locale)
49
51
  SgtnClient::CacheUtil.write_cache(cachekey,bundle)
50
52
  end
@@ -57,18 +59,13 @@ module SgtnClient
57
59
  bundlepath = source_bundle + "/" + component + "/" + locale + ".yml"
58
60
  SgtnClient.logger.debug "Getting source from bundle: " + bundlepath
59
61
  begin
60
- bundle = read_yml(bundlepath)
62
+ bundle = SgtnClient::FileUtil.read_yml(bundlepath)
61
63
  rescue => exception
62
64
  SgtnClient.logger.error exception.message
63
65
  end
64
66
  return bundle
65
67
  end
66
68
 
67
- def self.read_yml(file_name)
68
- erb = ERB.new(File.read(file_name))
69
- erb.filename = file_name
70
- YAML.load(erb.result)
71
- end
72
69
  end
73
70
 
74
71
  end
@@ -12,30 +12,25 @@ module SgtnClient
12
12
  class Translation
13
13
 
14
14
  def self.getString(component, key, locale)
15
- flocale = SgtnClient::LocaleUtil.fallback(locale)
16
- cache_key = SgtnClient::CacheUtil.get_cachekey(component, flocale)
17
- items = SgtnClient::CacheUtil.get_cache(cache_key)
18
- if items.nil?
19
- items = getTranslations(component, flocale)
20
- SgtnClient::CacheUtil.write_cache(cache_key, items)
21
- else
22
- SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
15
+ str = getTranslation(component, key, locale)
16
+ if str.nil?
17
+ str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
23
18
  end
19
+ str
20
+ end
24
21
 
25
- default = SgtnClient::Config.configurations.default
26
- if items.nil? || items["messages"] == nil
27
- return SgtnClient::Source.getSource(component, key, default)
28
- end
29
- str = items["messages"][key]
22
+ def self.getString_p(component, key, plural_args, locale)
23
+ str = getTranslation(component, key, locale)
30
24
  if str.nil?
31
- return SgtnClient::Source.getSource(component, key, default)
25
+ str = SgtnClient::Source.getSource(component, key, SgtnClient::Config.configurations.default)
26
+ str.to_plural_s(:en, plural_args)
32
27
  else
33
- return str
28
+ str.to_plural_s(locale, plural_args)
34
29
  end
35
- end
30
+ end
36
31
 
37
- def self.getString_f(component, key, args, locale)
38
- s = getString(component, key, locale)
32
+ def self.getString_f(component, key, args, locale, *optionals)
33
+ s = getString(component, key, locale, *optionals)
39
34
  if args.is_a?(Hash)
40
35
  args.each do |source, arg|
41
36
  s.gsub! "{#{source}}", arg
@@ -62,7 +57,7 @@ module SgtnClient
62
57
  items = {}
63
58
  s = SgtnClient::Source.getSources(component, default)
64
59
  default_component, value = s.first
65
- items["component"] = default_component
60
+ items["component"] = component
66
61
  items["messages"] = value
67
62
  items["locale"] = 'source'
68
63
  end
@@ -72,6 +67,28 @@ module SgtnClient
72
67
 
73
68
  private
74
69
 
70
+ def self.getTranslation(component, key, locale)
71
+ flocale = SgtnClient::LocaleUtil.fallback(locale)
72
+ cache_key = SgtnClient::CacheUtil.get_cachekey(component, flocale)
73
+ items = SgtnClient::CacheUtil.get_cache(cache_key)
74
+ if items.nil?
75
+ items = getTranslations(component, flocale)
76
+ if items.nil?
77
+ items = SgtnClient::Source.getSources(component, SgtnClient::Config.configurations.default)
78
+ SgtnClient::Core::Cache.put(cache_key, items, 60)
79
+ else
80
+ SgtnClient::CacheUtil.write_cache(cache_key, items)
81
+ end
82
+ else
83
+ SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
84
+ end
85
+ if items.nil? || items["messages"] == nil
86
+ nil
87
+ else
88
+ items["messages"][key]
89
+ end
90
+ end
91
+
75
92
  def self.getTranslations(component, locale)
76
93
  env = SgtnClient::Config.default_environment
77
94
  mode = SgtnClient::Config.configurations[env]["bundle_mode"]
@@ -89,13 +106,7 @@ module SgtnClient
89
106
  translation_bundle = SgtnClient::Config.configurations[env]["translation_bundle"]
90
107
  bundlepath = translation_bundle + "/" + product_name + "/" + version + "/" + component + "/messages_" + locale + ".json"
91
108
  SgtnClient.logger.debug "Getting translations from offline bundle: " + bundlepath
92
- begin
93
- file = File.read(bundlepath)
94
- data_hash = MultiJson.load(file)
95
- rescue => exception
96
- SgtnClient.logger.error exception.message
97
- end
98
- return data_hash
109
+ SgtnClient::FileUtil.read_json(bundlepath)
99
110
  end
100
111
 
101
112
  def self.get_server(component, locale)
@@ -1,27 +1,63 @@
1
1
  require 'date'
2
2
  require 'time'
3
3
 
4
- DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
5
- def l_full_s(locale = TwitterCldr.locale)
6
- self.localize(locale).to_full_s
4
+ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 2
5
+ def l_full_s(locale = TwitterCldr.locale, *args)
6
+ timezone = args[0]
7
+ display_name = args[1]
8
+ if timezone.nil?
9
+ self.localize(locale).to_full_s
10
+ elsif display_name.nil?
11
+ self.localize(locale).with_timezone(timezone).to_full_s
12
+ else
13
+ tz = TwitterCldr::Timezones::Timezone.instance(timezone, locale)
14
+ tz.display_name_for(self, display_name)
15
+ end
7
16
  end
8
17
  LOCALIZE
9
18
 
10
19
  DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
11
- def l_long_s(locale = TwitterCldr.locale)
12
- self.localize(locale).to_long_s
20
+ def l_long_s(locale = TwitterCldr.locale, *args)
21
+ timezone = args[0]
22
+ display_name = args[1]
23
+ if timezone.nil?
24
+ self.localize(locale).to_long_s
25
+ elsif display_name.nil?
26
+ self.localize(locale).with_timezone(timezone).to_long_s
27
+ else
28
+ tz = TwitterCldr::Timezones::Timezone.instance(timezone, locale)
29
+ tz.display_name_for(self, display_name)
30
+ end
13
31
  end
14
32
  LOCALIZE
15
33
 
16
34
  DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
17
- def l_medium_s(locale = TwitterCldr.locale)
18
- self.localize(locale).to_medium_s
35
+ def l_medium_s(locale = TwitterCldr.locale, *args)
36
+ timezone = args[0]
37
+ display_name = args[1]
38
+ if timezone.nil?
39
+ self.localize(locale).to_medium_s
40
+ elsif display_name.nil?
41
+ self.localize(locale).with_timezone(timezone).to_medium_s
42
+ else
43
+ tz = TwitterCldr::Timezones::Timezone.instance(timezone, locale)
44
+ tz.display_name_for(self, display_name)
45
+ end
19
46
  end
20
47
  LOCALIZE
21
48
 
22
49
 
23
50
  DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
24
- def l_short_s(locale = TwitterCldr.locale)
25
- self.localize(locale).to_short_s
51
+ def l_short_s(locale = TwitterCldr.locale, *args)
52
+ timezone = args[0]
53
+ display_name = args[1]
54
+ if timezone.nil?
55
+ self.localize(locale).to_short_s
56
+ elsif display_name.nil?
57
+ self.localize(locale).with_timezone(timezone).to_short_s
58
+ else
59
+ tz = TwitterCldr::Timezones::Timezone.instance(timezone, locale)
60
+ tz.display_name_for(self, display_name)
61
+ end
26
62
  end
27
63
  LOCALIZE
@@ -6,6 +6,7 @@ module SgtnClient::Core
6
6
 
7
7
  def self.initialize(disabled=false, opts={})
8
8
  $opts = opts
9
+ @mutex = Mutex.new
9
10
  SgtnClient.logger.debug "Initialize cache......"
10
11
  if disabled == false
11
12
  $data = Hash.new
@@ -41,42 +42,50 @@ module SgtnClient::Core
41
42
  end
42
43
 
43
44
  def self.put(key, value, ttl=nil)
44
- if $data == nil
45
- return nil
45
+ @mutex.synchronize do
46
+ if $data == nil
47
+ return nil
48
+ end
49
+ ttl ||= @opts[:ttl]
50
+ # hours from new
51
+ SgtnClient.logger.debug "Put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
52
+ $data[key] = Entry.new(Time.now + ttl*60, value)
46
53
  end
47
- ttl ||= @opts[:ttl]
48
- # hours from new
49
- SgtnClient.logger.debug "Put cache for key '" + key + "' with expired time at'" + (Time.now + ttl*60).to_s
50
- $data[key] = Entry.new(Time.now + ttl*60, value)
51
54
  end
52
55
 
53
56
  def self.delete(key)
54
- if $data == nil
55
- return nil
57
+ @mutex.synchronize do
58
+ if $data == nil
59
+ return nil
60
+ end
61
+ SgtnClient.logger.debug "Delete cache for key: " + key
62
+ $data.delete key
56
63
  end
57
- SgtnClient.logger.debug "Delete cache for key: " + key
58
- $data.delete key
59
64
  end
60
65
 
61
66
  def self.clear
62
- if $data == nil
63
- return nil
67
+ @mutex.synchronize do
68
+ if $data == nil
69
+ return nil
70
+ end
71
+ SgtnClient.logger.debug "Clear cache!"
72
+ $data = Hash.new
64
73
  end
65
- SgtnClient.logger.debug "Clear cache!"
66
- $data = Hash.new
67
74
  end
68
75
 
69
76
  def self.invalidate
70
- if $data == nil
71
- return nil
77
+ @mutex.synchronize do
78
+ if $data == nil
79
+ return nil
80
+ end
81
+ SgtnClient.logger.debug "Invalidating expired cache......"
82
+ now = Time.now
83
+ $data.each {
84
+ |k, v|
85
+ SgtnClient.logger.debug "Checking cache: key=#{k}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
86
+ }
87
+ $data.delete_if {|k, v| v[:expiry] < now}
72
88
  end
73
- SgtnClient.logger.debug "Invalidating expired cache......"
74
- now = Time.now
75
- $data.each {
76
- |k, v|
77
- SgtnClient.logger.debug "Checking cache: key=#{k}, expiredtime=#{v[:expiry]}, now=#{now}, expired=#{(v[:expiry] < now)}"
78
- }
79
- $data.delete_if {|k, v| v[:expiry] < now}
80
89
  end
81
90
  end
82
91
 
@@ -4,7 +4,11 @@ require 'multi_json'
4
4
  module SgtnClient::Core
5
5
  class Request
6
6
  def self.get(url)
7
- res = RestClient.get(url)
7
+ #res = RestClient.get(url)
8
+ res = RestClient::Resource.new(
9
+ url,
10
+ :verify_ssl => false
11
+ ).get
8
12
  begin
9
13
  obj = MultiJson.load(res)
10
14
  rescue MultiJson::ParseError => exception
@@ -11,6 +11,7 @@ module SgtnClient
11
11
  autoload :Exceptions, "sgtn-client/core/exceptions"
12
12
  autoload :ValidateUtil, "sgtn-client/util/validate-util"
13
13
  autoload :LocaleUtil, "sgtn-client/util/locale-util"
14
+ autoload :FileUtil, "sgtn-client/util/file-util"
14
15
 
15
16
  module Formatters
16
17
  autoload :PluralFormatter, "sgtn-client/formatters/plurals/plural_formatter"
@@ -0,0 +1,32 @@
1
+ require 'erb'
2
+ require 'yaml'
3
+
4
+ module SgtnClient
5
+
6
+ class FileUtil
7
+
8
+ @mutex = Mutex.new
9
+
10
+ def self.read_json(bundlepath)
11
+ @mutex.synchronize do
12
+ data_hash = nil
13
+ begin
14
+ file = File.read(bundlepath)
15
+ data_hash = MultiJson.load(file)
16
+ rescue => exception
17
+ SgtnClient.logger.error exception.message
18
+ end
19
+ return data_hash
20
+ end
21
+ end
22
+
23
+ def self.read_yml(file_name)
24
+ @mutex.synchronize do
25
+ erb = ERB.new(File.read(file_name))
26
+ erb.filename = file_name
27
+ YAML.load(erb.result)
28
+ end
29
+ end
30
+ end
31
+
32
+ end
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
 
2
- VERSION_INFO = [0, 1, 8].freeze
2
+ VERSION_INFO = [0, 2, 1].freeze
3
3
  VERSION = VERSION_INFO.map(&:to_s).join('.').freeze
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: singleton-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.1
4
+ version: 0.7.2
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-11-25 00:00:00.000000000 Z
11
+ date: 2022-01-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rest-client
@@ -282,6 +282,7 @@ files:
282
282
  - lib/sgtn-client/formatters/plurals/plural_formatter.rb
283
283
  - lib/sgtn-client/sgtn-client.rb
284
284
  - lib/sgtn-client/util/cache-util.rb
285
+ - lib/sgtn-client/util/file-util.rb
285
286
  - lib/sgtn-client/util/locale-util.rb
286
287
  - lib/sgtn-client/util/validate-util.rb
287
288
  - lib/singleton-ruby.rb