singleton-ruby 0.0.2 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (38) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +17 -0
  3. data/README.md +3 -0
  4. data/Rakefile +140 -0
  5. data/lib/generators/sgtnclient/USAGE +3 -0
  6. data/lib/generators/sgtnclient/install_generator.rb +15 -0
  7. data/lib/generators/sgtnclient/templates/sgtnclient.rb +2 -0
  8. data/lib/generators/sgtnclient/templates/sgtnclient.yml +33 -0
  9. data/lib/sgtn-client/api/source.rb +64 -0
  10. data/lib/sgtn-client/api/translation.rb +83 -0
  11. data/lib/sgtn-client/cldr/core_ext.rb +3 -0
  12. data/lib/sgtn-client/cldr/localized_date.rb +27 -0
  13. data/lib/sgtn-client/cldr/localized_datetime.rb +33 -0
  14. data/lib/sgtn-client/cldr/localized_time.rb +27 -0
  15. data/lib/sgtn-client/core/cache.rb +83 -0
  16. data/lib/sgtn-client/core/config.rb +173 -0
  17. data/lib/sgtn-client/core/exceptions.rb +112 -0
  18. data/lib/sgtn-client/core/logging.rb +50 -0
  19. data/lib/sgtn-client/core/request.rb +17 -0
  20. data/lib/{sgtn-client.rb → sgtn-client/sgtn-client.rb} +29 -1
  21. data/lib/sgtn-client/util/cache-util.rb +35 -0
  22. data/lib/sgtn-client/util/validate-util.rb +44 -0
  23. data/lib/singleton-ruby.rb +2 -0
  24. data/lib/version.rb +3 -0
  25. data/spec/config/locales/default/JAVA/en.yml +2 -0
  26. data/spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_en.json +1 -0
  27. data/spec/config/locales/l10n/bundles/logInsight/4.8.1/JAVA/messages_zh_CN.json +11 -0
  28. data/spec/config/sgtnclient-invalidate.yml +48 -0
  29. data/spec/config/sgtnclient.yml +11 -2
  30. data/spec/spec_helper.rb +2 -1
  31. data/spec/unit/cache_spec.rb +35 -0
  32. data/spec/unit/config_spec.rb +1 -1
  33. data/spec/unit/datetime_spec.rb +43 -0
  34. data/spec/unit/inconfig_spec.rb +17 -0
  35. data/spec/unit/logging_spec.rb +2 -3
  36. data/spec/unit/offclient_spec.rb +15 -0
  37. data/spec/unit/restclient_spec.rb +10 -1
  38. metadata +50 -3
@@ -0,0 +1,17 @@
1
+ require 'rest-client'
2
+ require 'multi_json'
3
+
4
+ module SgtnClient::Core
5
+ class Request
6
+ def self.get(url)
7
+ res = RestClient.get(url)
8
+ begin
9
+ obj = MultiJson.load(res)
10
+ rescue MultiJson::ParseError => exception
11
+ exception.data
12
+ exception.cause
13
+ end
14
+ return obj
15
+ end
16
+ end
17
+ end
@@ -1,9 +1,14 @@
1
1
  module SgtnClient
2
+ module Core
3
+ autoload :Cache, "sgtn-client/core/cache"
4
+ end
2
5
 
3
6
  autoload :Translation, "sgtn-client/api/translation"
7
+ autoload :Source, "sgtn-client/api/source"
4
8
  autoload :Config, "sgtn-client/core/config"
5
9
  autoload :Logging, "sgtn-client/core/logging"
6
10
  autoload :Exceptions, "sgtn-client/core/exceptions"
11
+ autoload :ValidateUtil, "sgtn-client/util/validate-util"
7
12
 
8
13
  class << self
9
14
  def configure(options = {}, &block)
@@ -12,8 +17,10 @@ module SgtnClient
12
17
 
13
18
  include Logging
14
19
  def load(*args)
20
+ # load configuration file
15
21
  begin
16
22
  SgtnClient::Config.load(args[0], args[1])
23
+ SgtnClient::ValidateUtil.validate_config()
17
24
  rescue => exception
18
25
  file = File.open('./error.log', 'a')
19
26
  file.sync = true
@@ -21,6 +28,7 @@ module SgtnClient
21
28
  log.error exception.message
22
29
  end
23
30
 
31
+ # create log file
24
32
  file = './sgtnclient_d.log'
25
33
  if args[2] != nil
26
34
  file = args[2]
@@ -28,6 +36,24 @@ module SgtnClient
28
36
  file = File.open(file, 'a')
29
37
  file.sync = true
30
38
  SgtnClient.logger = Logger.new(file)
39
+
40
+ # Set log level for sandbox mode
41
+ env = SgtnClient::Config.default_environment
42
+ mode = SgtnClient::Config.configurations[env]["mode"]
43
+ SgtnClient.logger.info "Current mode is: " + mode
44
+ if mode == 'sandbox'
45
+ SgtnClient.logger.level = Logger::DEBUG
46
+ else
47
+ SgtnClient.logger.level = Logger::INFO
48
+ end
49
+
50
+ # initialize cache
51
+ disable_cache = SgtnClient::Config.configurations[env]["disable_cache"]
52
+ if disable_cache != nil
53
+ SgtnClient::Core::Cache.initialize(disable_cache)
54
+ else
55
+ SgtnClient::Core::Cache.initialize()
56
+ end
31
57
  end
32
58
 
33
59
  def logger
@@ -39,4 +65,6 @@ module SgtnClient
39
65
  end
40
66
  end
41
67
 
42
- end
68
+ end
69
+
70
+ require 'sgtn-client/cldr/core_ext'
@@ -0,0 +1,35 @@
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
+ env = SgtnClient::Config.default_environment
19
+ cache_expiry_period = SgtnClient::Config.configurations[env]["cache_expiry_period"]
20
+ # expired after 24 hours
21
+ if cache_expiry_period == nil
22
+ cache_expiry_period = 24*60
23
+ end
24
+ SgtnClient::Core::Cache.put(cache_key, items, cache_expiry_period)
25
+ end
26
+
27
+ def self.get_cachekey(component, locale)
28
+ env = SgtnClient::Config.default_environment
29
+ product_name = SgtnClient::Config.configurations[env]["product_name"]
30
+ version = SgtnClient::Config.configurations[env]["version"].to_s
31
+ return product_name + "_" + version + "_" + component + "_" + locale
32
+ end
33
+ end
34
+
35
+ end
@@ -0,0 +1,44 @@
1
+
2
+ module SgtnClient
3
+
4
+ class ValidateUtil
5
+
6
+ def self.validate_config()
7
+ SgtnClient.logger.debug "-----------Start to validate configuration's setting itmes-----------"
8
+ env = SgtnClient::Config.default_environment
9
+ messages = "\n"
10
+
11
+ mode = SgtnClient::Config.configurations[env]["mode"]
12
+ if mode != 'sandbox' && mode != 'live'
13
+ messages = messages + "Configuration[mode] has to be 'sandbox' or 'live'!\n"
14
+ end
15
+
16
+ bundle_mode = SgtnClient::Config.configurations[env]["bundle_mode"]
17
+ if bundle_mode != 'offline' && bundle_mode != 'online'
18
+ messages = messages + "Configuration[bundle_mode] has to be 'offline' or 'online'!\n"
19
+ end
20
+
21
+ #version = SgtnClient::Config.configurations[env]["version"]
22
+ #if version.is_a? Integer
23
+ #messages = messages + "Configuration[version] has to be standard as '#.#.#, e.g '1.0.0'!\n"
24
+ #end
25
+
26
+ cache_expiry_period = SgtnClient::Config.configurations[env]["cache_expiry_period"]
27
+ if cache_expiry_period != nil && (cache_expiry_period.is_a? Integer) == false
28
+ messages = messages + "Configuration[cache_expiry_period] has to be a number!\n"
29
+ end
30
+
31
+ disable_cache = SgtnClient::Config.configurations[env]["disable_cache"]
32
+ if disable_cache != nil && disable_cache != false && disable_cache != true
33
+ messages = messages + "Configuration[disable_cache] has to be a 'true' or 'false'!\n"
34
+ end
35
+
36
+ if messages != "\n"
37
+ raise SgtnClient::Exceptions::MissingConfig.new(messages)
38
+ end
39
+ SgtnClient.logger.debug "-----------End to validate configuration's setting itmes-----------"
40
+ end
41
+
42
+ end
43
+
44
+ end
@@ -0,0 +1,2 @@
1
+ require "sgtn-client/sgtn-client"
2
+
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+
2
+ VERSION_INFO = [0, 0, 7].freeze
3
+ VERSION = VERSION_INFO.map(&:to_s).join('.').freeze
@@ -0,0 +1,2 @@
1
+ en:
2
+ hello: "Hello world"
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "component" : "JAVA",
3
3
  "messages" : {
4
+ "hello": "@hello@",
4
5
  "com.vmware.loginsight.web.settings.stats.StatsTable.host" : "Host",
5
6
  "com.vmware.loginsight.web.settings.stats.StatsTableType.eventsIngestionRatePerSecond" : "Events Ingestion Rate (Per Second)",
6
7
  "com.vmware.loginsight.web.settings.stats.StatsTableType.eventsIngestionRatePerSecond.help" : "The actual ingestion rate (syslog + API) of each node after message rebalancing.<br />Log Insight nodes will rebalance messages to attempt uniform ingestion per node for balanced retention and performance.<br /><br />Note that the ingestion rate of each node must be below the ingestion rate configuration maximum for a node.<br /><br />For the actual rate of events being sent directly to each node prior to message rebalancing, see the Syslog Events Incoming Rate (Per Second) and API Events Incoming Rate (Per Second) tables below.",
@@ -0,0 +1,11 @@
1
+ {
2
+ "component" : "JAVA",
3
+ "messages" : {
4
+ "com.vmware.loginsight.web.settings.stats.StatsTable.host" : "主机",
5
+ "com.vmware.loginsight.web.settings.stats.StatsTableType.eventsIngestionRatePerSecond" : "事件载入速率 (每秒)",
6
+ "com.vmware.loginsight.web.settings.stats.StatsTableType.eventsIngestionRatePerSecond.help" : "消息再平衡之后每个节点的实际载入速率 (syslog + API)。<br />Log Insight 节点将再平衡消息以尝试每节点的统一载入,从而实现平衡保留和性能。<br /><br />请注意,每个节点的载入速率必须低于配置的最大节点载入速率。<br /><br />有关消息再平衡之前直接发送到每个节点的事件的实际速率,请参见下述“Syslog 事件入站速率 (每秒)”和“API 事件入站速率 (每秒)”表。",
7
+ "com.vmware.loginsight.web.settings.stats.StatsTableType.syslogEventsIncomingRatePerSecond" : "Syslog 事件入站速率 (每秒)",
8
+ "com.vmware.loginsight.web.utilities.EmailUtil.upgrade.body48" : "{0} 现在正在运行版本 {1}。下面是 {1} 版本中新增的一些出色功能。我们感谢您一如既往地支持,同时非常重视您通过 Twitter 针对我们的 {3} 和 {4} 提供的反馈。<br /><br /><b>vRealize Log Insight 服务器功能</b><br />• TODO 增加服务器功能。<br /><br /><b>vRealize Log Insight 代理功能</b><br />• TODO 增加代理功能。<br /><br />有关 {0} 的新功能的详细信息,请查看 {0} v{1} {2}。<br /><br />祝您使用愉快!<br />{0} 团队<br /><br />"
9
+ },
10
+ "locale" : "zh-Hans"
11
+ }
@@ -0,0 +1,48 @@
1
+ test: &default
2
+
3
+ # Credentials for REST APIs
4
+ client_id: tee
5
+ client_secret: 89987
6
+
7
+ # Mode can be 'live' or 'sandbox'
8
+ mode: sandbox1
9
+
10
+ # Credentials for Classic APIs
11
+ app_id: APP-80W284485P519543T
12
+ username: linr
13
+ password: fdasf
14
+ signature: sfds-RWy
15
+ # # With Certificate
16
+ # cert_path: "config/cert_key.pem"
17
+ sandbox_email_address: linr@vmware.com
18
+
19
+ # #Product Name
20
+ product_name: logInsight
21
+
22
+ # # bundle version
23
+ version: 4.8
24
+
25
+ # # HTTP Proxy
26
+ vip_server: https://g11n-vip-dev-1.eng.vmware.com:8090
27
+
28
+ # # mode of bundle: online/offline
29
+ bundle_mode: offline1www
30
+
31
+ # # translation bundle Path
32
+ translation_bundle: ./spec/config/locales/l10n/bundles
33
+
34
+ # # source bundle Path
35
+ source_bundle: ./spec/config/locales/default
36
+
37
+ # # memory cache's expration(minutes), default value is 24*60
38
+ cache_expiry_period: 36t
39
+
40
+ # # disable cache, it's optional setting
41
+ disable_cache: true1
42
+
43
+ development:
44
+ <<: *default
45
+
46
+ production:
47
+ <<: *default
48
+ mode: live
@@ -28,9 +28,18 @@ test: &default
28
28
  # # mode of bundle: online/offline
29
29
  bundle_mode: offline
30
30
 
31
- # # Offline Bundle Path
32
- offline_bundle: ./spec/config/locales
31
+ # # translation bundle Path
32
+ translation_bundle: ./spec/config/locales/l10n/bundles
33
33
 
34
+ # # source bundle Path
35
+ source_bundle: ./spec/config/locales/default
36
+
37
+ # # memory cache's expration(minutes), default value is 24*60
38
+ cache_expiry_period: 10
39
+
40
+ # # disable cache, it's optional setting
41
+ ##disable_cache: true
42
+
34
43
  development:
35
44
  <<: *default
36
45
 
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'bundler/setup'
2
- require_relative '../lib/sgtn-client.rb'
2
+ require_relative '../lib/sgtn-client/sgtn-client.rb'
3
+ require 'twitter_cldr'
3
4
 
4
5
  if ENV['COVERAGE']
5
6
  require 'simplecov'
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe SgtnClient do
4
+ describe "Cache" do
5
+
6
+ before :each do
7
+ env = SgtnClient::Config.default_environment
8
+ SgtnClient::Config.configurations[env]["bundle_mode"] = 'online'
9
+ SgtnClient::Config.configurations[env]["cache_expiry_period"] = 1
10
+ SgtnClient::Source.loadBundles("en")
11
+ end
12
+
13
+ it "GETTranslation" do
14
+
15
+ # get translation from server
16
+ SgtnClient.logger.debug "----------Start to get translation from server---------"
17
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
18
+
19
+ # get translation from cache
20
+ SgtnClient.logger.debug "----------Start to get translation from cache---------"
21
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
22
+
23
+ # get from server again after data is expired
24
+ SgtnClient.logger.debug "----------Sleep 70s---------"
25
+ puts Time.now
26
+ #sleep 70
27
+ puts Time.now
28
+ SgtnClient.logger.debug "----------Start to get translation from expired cache---------"
29
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh-Hans")).to eq '主机'
30
+
31
+ SgtnClient.logger.debug "----------End to get translation from server---------"
32
+ end
33
+ end
34
+
35
+ end
@@ -1,5 +1,5 @@
1
1
  require 'spec_helper'
2
- require_relative '../../lib/sgtn-client.rb'
2
+ require_relative '../../lib/sgtn-client/sgtn-client.rb'
3
3
 
4
4
  describe SgtnClient do
5
5
 
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe SgtnClient do
4
+ describe "DateTime" do
5
+
6
+ before :each do
7
+
8
+ end
9
+
10
+ it "DateTime" do
11
+ d = DateTime.new(2007,11,19,8,37,48,"-06:00")
12
+ expect(d.to_full_s(:es)).to eq 'lunes, 19 de noviembre de 2007, 14:37:48 (tiempo universal coordinado)'
13
+ expect(d.to_long_s(:es)).to eq '19 de noviembre de 2007, 14:37:48 UTC'
14
+ expect(d.to_medium_s(:es)).to eq '19 nov 2007 14:37:48'
15
+ expect(d.to_short_s(:es)).to eq '19/11/07 14:37'
16
+ end
17
+
18
+ it "Date0" do
19
+ d = Date.new(2001,2,3)
20
+ expect(d.to_full_s(:es)).to eq 'Saturday, February 3, 2001'
21
+ expect(d.to_long_s(:es)).to eq 'February 3, 2001'
22
+ expect(d.to_medium_s(:es)).to eq 'Feb 3, 2001'
23
+ expect(d.to_short_s(:es)).to eq '2/3/01'
24
+ end
25
+
26
+ it "Date" do
27
+ d = DateTime.new(2007,11,19,8,37,48,"-06:00")
28
+ expect(d.to_date(:es).to_full_s).to eq 'lunes, 19 de noviembre de 2007'
29
+ expect(d.to_date(:es).to_long_s).to eq '19 de noviembre de 2007'
30
+ expect(d.to_date(:es).to_medium_s).to eq '19 nov 2007'
31
+ expect(d.to_date(:es).to_short_s).to eq '19/11/07'
32
+ end
33
+
34
+ it "time" do
35
+ d = Time.new(2007,11,1,15,25,0, "+09:00")
36
+ expect(d.to_full_s(:es)).to eq '6:25:00 (tiempo universal coordinado)'
37
+ expect(d.to_long_s(:es)).to eq '6:25:00 UTC'
38
+ expect(d.to_medium_s(:es)).to eq '6:25:00'
39
+ expect(d.to_short_s(:es)).to eq '6:25'
40
+ end
41
+ end
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+ require_relative '../../lib/sgtn-client/sgtn-client.rb'
3
+
4
+ describe SgtnClient do
5
+
6
+ describe "loadinconfig" do
7
+
8
+ before :each do
9
+ #SgtnClient.load("./spec/config/sgtnclient-invalide.yml", "test", './sgtnclient_config.log')
10
+ end
11
+
12
+ it "validate_configuration" do
13
+ SgtnClient.load("./spec/config/sgtnclient-invalidate.yml", "test", './sgtnclient_config.log')
14
+ end
15
+
16
+ end
17
+ end
@@ -1,6 +1,6 @@
1
1
  #require 'spec_helper'
2
2
  require 'stringio'
3
- require_relative '../../lib/sgtn-client.rb'
3
+ require_relative '../../lib/sgtn-client/sgtn-client.rb'
4
4
 
5
5
  describe SgtnClient::Logging do
6
6
  #Logging = SgtnClient::Logging
@@ -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
@@ -6,10 +6,25 @@ describe SgtnClient do
6
6
  before :each do
7
7
  env = SgtnClient::Config.default_environment
8
8
  SgtnClient::Config.configurations[env]["bundle_mode"] = 'offline'
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 "GET_zh_CN" do
19
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh_CN")).to eq '主机'
20
+ # get from cache in 2nd time
21
+ expect(SgtnClient::Translation.getString("JAVA", "com.vmware.loginsight.web.settings.stats.StatsTable.host", "zh_CN")).to eq '主机'
22
+ end
23
+
24
+ it "NonExistingKey" do
25
+ expect(SgtnClient::Translation.getString("JAVA", "hello", "zh-Hans")).to eq 'Hello world'
26
+ # get from cache in 2nd time
27
+ expect(SgtnClient::Translation.getString("JAVA", "hello", "zh-Hans")).to eq 'Hello world'
13
28
  end
14
29
  end
15
30
 
@@ -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