singleton-client-test 0.7.7.5 → 0.7.7.6

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: 6ab2a482cbb7c83aca190392287160802cae5488a7356dfd24a79476817f40ac
4
- data.tar.gz: e691e2eca4f3c2a2f747970ff746b045e4c1cbaa3de23e73eeba84a7f812c2ec
3
+ metadata.gz: 3bd55bdad7f46bf9c0cffd42a710cfb6dcae97ecadbbb148af41d2b44be0afca
4
+ data.tar.gz: 026ef3a390e9a88691decb97b1131ac31e358c9ede7628a7b698d38899a80465
5
5
  SHA512:
6
- metadata.gz: 619fed54ca5da856c0d8933724e69bb286deba7aebd94a6c98d674dc8c532dc2937cbee650ebb0e337351a3d0b8515dd06f1884066412009098f85c09a4373fc
7
- data.tar.gz: 9500161a55b6e4a2c43f2955168895ddfbde82ae1fe68a6a2fcbe4aa1f1498bfaa99a3fe3f82962fac288f455f5a775e26c2193a945940eaaa4cdafbeca90fe8
6
+ metadata.gz: 5792e7d5f543603927732321cf440be37266fdc5a65fdd6075017068bb98fd9e5b8b4c52e8421fb0508166997993117d6c151921243b642ea448fb5ceea2e542
7
+ data.tar.gz: 7a8938ba9ffe0b6480a4b83e441b48d48eec98f5e2262019ff5db001e94fe50d404cc3c4277e464cab90ab7141076223b370d2c3a733058d95eecd940f8d8e88
@@ -43,7 +43,7 @@ module SgtnClient
43
43
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] key: #{key}, component: #{component}, locale: #{locale}, args: #{kwargs}" }
44
44
 
45
45
  begin
46
- best_match_locale = LocaleUtil.get_best_locale(locale || self.locale, component)
46
+ best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
47
47
  messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
48
48
  result = messages&.fetch(key, nil)
49
49
  rescue StandardError => e
@@ -69,7 +69,7 @@ module SgtnClient
69
69
  def get_translations(component, locale = nil)
70
70
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component: #{component}, locale: #{locale}" }
71
71
 
72
- best_match_locale = LocaleUtil.get_best_locale(locale || self.locale, component)
72
+ best_match_locale = LocaleUtil.get_best_locale(locale || SgtnClient.locale, component)
73
73
  messages, actual_locale = get_bundle_with_fallback(component, best_match_locale)
74
74
 
75
75
  { 'component' => component, 'locale' => actual_locale, 'messages' => messages } if messages
@@ -78,14 +78,6 @@ module SgtnClient
78
78
  nil
79
79
  end
80
80
 
81
- def locale
82
- RequestStore.store[:locale] ||= LocaleUtil.get_fallback_locale
83
- end
84
-
85
- def locale=(value)
86
- RequestStore.store[:locale] = value
87
- end
88
-
89
81
  private
90
82
 
91
83
  def get_bundle(component, locale)
@@ -18,8 +18,10 @@ module SgtnClient
18
18
  def logger
19
19
  @logger ||= if log_file
20
20
  puts "create log file: '#{log_file}', level: #{log_level}"
21
- require 'lumberjack'
22
- Lumberjack::Logger.new(log_file, level: log_level, max_size: '1M', keep: 4)
21
+ require 'logging'
22
+ logger = Logging.logger(log_file, 4, 1_048_576)
23
+ logger.level = log_level
24
+ logger
23
25
  else
24
26
  require 'logger'
25
27
  Logger.new(STDOUT, level: log_level || Logger::INFO)
@@ -15,7 +15,7 @@ module SgtnClient
15
15
 
16
16
  # get from cache, return expired data immediately
17
17
  def get_bundle(component, locale)
18
- SgtnClient.logger.debug { "[#{caller[2]}] component=#{component}, locale=#{locale}" }
18
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] component=#{component}, locale=#{locale}" }
19
19
 
20
20
  result = @cache_hash[Common::BundleID.new(component, locale)] || load_bundle(component, locale)
21
21
  ensure
@@ -23,7 +23,7 @@ module SgtnClient
23
23
  end
24
24
 
25
25
  def available_bundles
26
- SgtnClient.logger.debug { "[#{caller[2]}]" }
26
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}]" }
27
27
 
28
28
  result = @cache_hash[CONSTS::AVAILABLE_BUNDLES_KEY] || super
29
29
  ensure
@@ -33,13 +33,13 @@ module SgtnClient
33
33
 
34
34
  module CacheFiller # :nodoc:
35
35
  def load_bundle(component, locale)
36
- SgtnClient.logger.debug { "[#{caller[2]}] component=#{component}, locale=#{locale}" }
36
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] CacheFiller, component=#{component}, locale=#{locale}" }
37
37
 
38
38
  @cache_hash[Common::BundleID.new(component, locale)] = Common::BundleData.new(super)
39
39
  end
40
40
 
41
41
  def available_bundles
42
- SgtnClient.logger.debug { "[#{caller[2]}]" }
42
+ SgtnClient.logger.debug { "[#{__FILE__}][#{__callee__}] CacheFiller" }
43
43
 
44
44
  item = super
45
45
  old_item = @cache_hash[CONSTS::AVAILABLE_BUNDLES_KEY]
@@ -9,27 +9,34 @@ require 'set'
9
9
 
10
10
  module SgtnClient
11
11
  module TranslationLoader
12
- class SgtnServer
12
+ class SgtnServer # :nodoc:
13
13
  ERROR_ILLEGAL_DATA = 'server returned illegal data.'
14
14
  ERROR_BUSINESS_ERROR = 'server returned business error.'
15
+ ERROR_PARTIAL_SUCCESS = 'the request to server was partially successful.'
16
+ ERROR_NO_DATA = 'no expected data in response from server. path: %s. Body is: %s'
15
17
 
16
18
  REQUEST_ARGUMENTS = { timeout: 10 }.freeze
17
19
 
18
20
  def initialize(config)
19
- @server_url = config.vip_server
20
-
21
- product_root = format('i18n/api/v2/translation/products/%s/versions/%s', config.product_name, config.version)
21
+ product_root = format('i18n/api/v2/translation/products/%<name>s/versions/%<version>s',
22
+ name: config.product_name, version: config.version)
22
23
 
23
24
  @bundle_url = "#{product_root}/locales/%s/components/%s"
24
25
  @locales_url = "#{product_root}/localelist"
25
26
  @components_url = "#{product_root}/componentlist"
27
+
28
+ @conn = Faraday.new(config.vip_server, request: REQUEST_ARGUMENTS) do |f|
29
+ f.response :json # decode response bodies as JSON
30
+ f.use :gzip
31
+ f.response :raise_error
32
+ f.response :logger, config.logger, { log_level: :debug, headers: false, bodies: true }
33
+ end
26
34
  end
27
35
 
28
36
  def load_bundle(component, locale)
29
37
  SgtnClient.logger.debug { "[#{method(__callee__).owner}.#{__callee__}] component=#{component}, locale=#{locale}" }
30
38
 
31
- messages = query_server(format(@bundle_url, locale, component), ['messages'])
32
- messages
39
+ query_server(format(@bundle_url, locale, component), ['messages'])
33
40
  end
34
41
 
35
42
  def available_bundles
@@ -54,23 +61,12 @@ module SgtnClient
54
61
  end
55
62
 
56
63
  def query_server(url, path_to_data = [], queries = nil, headers = nil)
57
- conn = Faraday.new(@server_url, request: REQUEST_ARGUMENTS) do |f|
58
- f.response :json # decode response bodies as JSON
59
- f.use :gzip
60
- f.response :raise_error
61
- f.response :logger, SgtnClient.logger, { log_level: :debug }
62
- end
63
- resp = conn.get(url, queries, headers)
64
+ resp = @conn.get(url, queries, headers)
64
65
 
65
66
  process_business_error(resp.body)
66
- extract_data(resp.body, path_to_data)
67
- end
68
-
69
- def extract_data(parsedbody, path_to_data)
70
- data = parsedbody.dig('data', *path_to_data)
71
- raise SingletonError, "no expected data in response. Body is: #{parsedbody}" unless data
72
67
 
73
- data
68
+ resp.body&.dig('data', *path_to_data) ||
69
+ (raise SingletonError, format(ERROR_NO_DATA, path_to_data, resp.body))
74
70
  end
75
71
 
76
72
  def process_business_error(parsedbody)
@@ -79,8 +75,8 @@ module SgtnClient
79
75
  raise SingletonError, "#{ERROR_BUSINESS_ERROR} #{parsedbody['response']}"
80
76
  end
81
77
 
82
- # 600 means a successful response, 6xx means partial successful.
83
- SgtnClient.logger.warn "#{ERROR_BUSINESS_ERROR} #{parsedbody['response']}" if b_code > 600
78
+ # 600/200 means a successful response, 6xx/2xx means partial successful.
79
+ SgtnClient.logger.warn "#{ERROR_PARTIAL_SUCCESS} #{parsedbody['response']}" if b_code != 600 && b_code != 200
84
80
  rescue TypeError, ArgumentError, NoMethodError => e
85
81
  raise SingletonError, "#{ERROR_ILLEGAL_DATA} #{e}. Body is: #{parsedbody}"
86
82
  end
@@ -38,5 +38,13 @@ module SgtnClient # :nodoc:
38
38
  config.update(config_hash)
39
39
  ValidateUtil.validate_config
40
40
  end
41
+
42
+ def locale
43
+ RequestStore.store[:locale] ||= LocaleUtil.get_fallback_locale
44
+ end
45
+
46
+ def locale=(value)
47
+ RequestStore.store[:locale] = value
48
+ end
41
49
  end
42
50
  end
@@ -29,7 +29,8 @@ module SgtnClient
29
29
  end
30
30
 
31
31
  component_result[locale] ||= begin
32
- # component_result.shift if component_result.size >= 50
32
+ component_result.delete(component_result.keys[Random.rand(component_result.size)]) if component_result.size >= 50
33
+
33
34
  if SgtnClient.config.available_locales(component).include?(locale)
34
35
  locale
35
36
  elsif locale.nil?
@@ -14,11 +14,26 @@ module Sgtn # :nodoc:
14
14
  end
15
15
 
16
16
  def_delegator SgtnClient::Config, :instance, :config
17
- delegate %i[translate t get_translations locale locale=] => SgtnClient::Translation,
17
+ delegate %i[translate t get_translations] => SgtnClient::Translation,
18
+ %i[locale locale=] => SgtnClient,
18
19
  %i[logger product_name version vip_server translation_bundle
19
20
  source_bundle cache_expiry_period log_file log_level].flat_map { |m|
20
21
  [m, "#{m}=".to_sym]
21
22
  } => :config
23
+
24
+ def with_locale(tmp_locale = nil)
25
+ if tmp_locale.nil?
26
+ yield
27
+ else
28
+ current_locale = locale
29
+ self.locale = tmp_locale
30
+ begin
31
+ yield
32
+ ensure
33
+ self.locale = current_locale
34
+ end
35
+ end
36
+ end
22
37
  end
23
38
 
24
39
  I18nBackend = SgtnClient::I18nBackend
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: singleton-client-test
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.7.5
4
+ version: 0.7.7.6
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: 2022-07-20 00:00:00.000000000 Z
11
+ date: 2022-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: concurrent-ruby
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: lumberjack
70
+ name: logging
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="