singleton-ruby 0.0.2 → 0.0.7

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.
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
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2eafb488acb5e9534e211a2757110e9768841fa62c6e49fc791b2751a7e3cc70
4
- data.tar.gz: cd8aa70499702d69ce4a8d5f936fd4ac6b41bc5500283e2514135cb5e9f10b77
3
+ metadata.gz: 52119541bb9992949228596f935b82a69f5b89c5f9f51df0361f3e2acde0c849
4
+ data.tar.gz: f2e05c046787bb4bf0f9abe662f4bd22dbbad01a1c617d16ef63e71b999a9640
5
5
  SHA512:
6
- metadata.gz: 39a6c18b36bd1850962a17517811589b1e3a16b7949980be4f0839bc4bbcc7e02025b1d4d0929dc6f18e2126fa77926bd374ad65dc48ab90c36c51bbea0da666
7
- data.tar.gz: cfb42233c159ffd9639dccfc73e953f43943c964702f7e0742fbb1fe3da078bfbffb6aee21123c1629810d13731b7dbe414e250c56deb2afad1b4a69e79aeb56
6
+ metadata.gz: cbfe258f95273006ec2e49560d590126269dc536e343ca5643cc8e5a0bc35d5e4c4b67ece04582fb09e869b31f228cea45e81ec90cebc9c64b896622df746fd7
7
+ data.tar.gz: fdf87a28dcf9301ae8e97e23194ef80fdac283444bf7197ac25b129f82e3e60214ddc9de987b994e0fa5b1322ed826f6bc4f122a1f1823f5f40dbc9e2538e002
data/Gemfile ADDED
@@ -0,0 +1,17 @@
1
+ source "https://rubygems.org"
2
+
3
+ if !!File::ALT_SEPARATOR
4
+ gemspec :name => 'singleton-ruby.windows'
5
+ else
6
+ gemspec :name => 'singleton-ruby'
7
+ end
8
+
9
+ gem 'rake', :require => false
10
+
11
+ group :test do
12
+ gem 'simplecov', :require => false
13
+ gem 'rspec'
14
+ gem 'webmock'
15
+ end
16
+
17
+ #gem 'releasinator', '~> 0.6'
data/README.md CHANGED
@@ -4,5 +4,8 @@
4
4
  - Ruby version: 3.0.0 or above
5
5
  - Bundler version: 2.2.3 or above
6
6
 
7
+ ## Run Unit Test
8
+ rake spec:unit
9
+
7
10
 
8
11
 
data/Rakefile ADDED
@@ -0,0 +1,140 @@
1
+ # load `rake build/install/release tasks'
2
+ require 'bundler/setup'
3
+ require_relative './lib/version'
4
+
5
+ namespace :ruby do
6
+ Bundler::GemHelper.install_tasks(:name => 'singleton-ruby')
7
+ end
8
+
9
+ require "rspec/core/rake_task"
10
+
11
+ desc "Run all specs"
12
+ RSpec::Core::RakeTask.new('spec')
13
+
14
+ desc "Run unit specs"
15
+ RSpec::Core::RakeTask.new('spec:unit') do |t|
16
+ t.pattern = 'spec/unit/*_spec.rb'
17
+ end
18
+
19
+ desc "Run integration specs"
20
+ RSpec::Core::RakeTask.new('spec:integration') do |t|
21
+ t.pattern = 'spec/integration/*_spec.rb'
22
+ end
23
+
24
+ desc "Print specdocs"
25
+ RSpec::Core::RakeTask.new(:doc) do |t|
26
+ t.rspec_opts = ["--format", "specdoc", "--dry-run"]
27
+ t.pattern = 'spec/**/*_spec.rb'
28
+ end
29
+
30
+ desc "Run all examples with RCov"
31
+ RSpec::Core::RakeTask.new('rcov') do |t|
32
+ t.pattern = 'spec/*_spec.rb'
33
+ t.rcov = true
34
+ t.rcov_opts = ['--exclude', 'examples']
35
+ end
36
+
37
+ desc 'Regenerate authors file'
38
+ task :authors do
39
+ Dir.chdir(File.dirname(__FILE__)) do
40
+ File.open('AUTHORS', 'w') do |f|
41
+ f.write <<-EOM
42
+ The Ruby REST Client would not be what it is today without the help of
43
+ the following kind souls:
44
+
45
+ EOM
46
+ end
47
+
48
+ sh 'git shortlog -s | cut -f 2 >> AUTHORS'
49
+ end
50
+ end
51
+
52
+ task :default do
53
+ sh 'rake -T'
54
+ end
55
+
56
+ def alias_task(alias_task, original)
57
+ desc "Alias for rake #{original}"
58
+ task alias_task, Rake.application[original].arg_names => original
59
+ end
60
+ alias_task(:test, :spec)
61
+
62
+ ############################
63
+
64
+ WindowsPlatforms = %w{x86-mingw32 x64-mingw32 x86-mswin32}
65
+
66
+ namespace :all do
67
+
68
+ desc "Build rest-client #{VERSION} for all platforms"
69
+ task :build => ['ruby:build'] + \
70
+ WindowsPlatforms.map {|p| "windows:#{p}:build"}
71
+
72
+ desc "Create tag v#{VERSION} and for all platforms build and " \
73
+ "push rest-client #{VERSION} to Rubygems"
74
+ task :release => ['build', 'ruby:release'] + \
75
+ WindowsPlatforms.map {|p| "windows:#{p}:push"}
76
+
77
+ end
78
+
79
+ namespace :windows do
80
+ spec_path = File.join(File.dirname(__FILE__), 'rest-client.windows.gemspec')
81
+
82
+ WindowsPlatforms.each do |platform|
83
+ namespace platform do
84
+ gem_filename = "rest-client-#{VERSION}-#{platform}.gem"
85
+ base = File.dirname(__FILE__)
86
+ pkg_dir = File.join(base, 'pkg')
87
+ gem_file_path = File.join(pkg_dir, gem_filename)
88
+
89
+ desc "Build #{gem_filename} into the pkg directory"
90
+ task 'build' do
91
+ orig_platform = ENV['BUILD_PLATFORM']
92
+ begin
93
+ ENV['BUILD_PLATFORM'] = platform
94
+
95
+ sh("gem build -V #{spec_path}") do |ok, res|
96
+ if ok
97
+ FileUtils.mkdir_p(pkg_dir)
98
+ FileUtils.mv(File.join(base, gem_filename), pkg_dir)
99
+ Bundler.ui.confirm("rest-client #{VERSION} " \
100
+ "built to pkg/#{gem_filename}")
101
+ else
102
+ abort "Command `gem build` failed: #{res}"
103
+ end
104
+ end
105
+
106
+ ensure
107
+ ENV['BUILD_PLATFORM'] = orig_platform
108
+ end
109
+ end
110
+
111
+ desc "Push #{gem_filename} to Rubygems"
112
+ task 'push' do
113
+ sh("gem push #{gem_file_path}")
114
+ end
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ ############################
121
+
122
+ require 'rdoc/task'
123
+
124
+ Rake::RDocTask.new do |t|
125
+ t.rdoc_dir = 'rdoc'
126
+ t.title = "rest-client, fetch RESTful resources effortlessly"
127
+ t.options << '--line-numbers' << '--inline-source' << '-A cattr_accessor=object'
128
+ t.options << '--charset' << 'utf-8'
129
+ t.rdoc_files.include('README.md')
130
+ t.rdoc_files.include('lib/*.rb')
131
+ end
132
+
133
+ ############################
134
+
135
+ require 'rubocop/rake_task'
136
+
137
+ RuboCop::RakeTask.new(:rubocop) do |t|
138
+ t.options = ['--display-cop-names']
139
+ end
140
+ alias_task(:lint, :rubocop)
@@ -0,0 +1,3 @@
1
+ To copy a PayPal SDK default configuration and initializer to your Rails App.
2
+
3
+ rails g sgtnclient:install
@@ -0,0 +1,15 @@
1
+ module SgtnClient
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def copy_config_file
7
+ copy_file "sgtnclient.yml", "config/sgtnclient.yml"
8
+ end
9
+
10
+ def copy_initializer_file
11
+ copy_file "sgtnclient.rb", "config/initializers/sgtnclient.rb"
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,2 @@
1
+ SgtnClient.load("config/sgtnclient.yml", Rails.env)
2
+ PSgtnClient.logger = Rails.logger
@@ -0,0 +1,33 @@
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: sandbox
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.1
24
+
25
+ # # HTTP Proxy
26
+ vip_server: https://g11n-vip-dev-1.eng.vmware.com:8090
27
+
28
+ development:
29
+ <<: *default
30
+
31
+ production:
32
+ <<: *default
33
+ mode: live
@@ -0,0 +1,64 @@
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 [" + locale + "] 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_yml(bundlepath)
50
+ rescue => exception
51
+ SgtnClient.logger.error exception.message
52
+ end
53
+ return bundle
54
+ end
55
+
56
+ def self.read_yml(file_name)
57
+ erb = ERB.new(File.read(file_name))
58
+ erb.filename = file_name
59
+ YAML.load(erb.result)
60
+ end
61
+
62
+ end
63
+
64
+ end
@@ -0,0 +1,83 @@
1
+ require 'json'
2
+
3
+ module SgtnClient
4
+
5
+ module Core
6
+ autoload :Request, "sgtn-client/core/request"
7
+ autoload :Cache, "sgtn-client/core/cache"
8
+ autoload :CacheUtil, "sgtn-client/util/cache-util"
9
+ end
10
+
11
+ class Translation
12
+
13
+ def self.getString(component, key, locale)
14
+ cache_key = SgtnClient::CacheUtil.get_cachekey(component, locale)
15
+ items = SgtnClient::CacheUtil.get_cache(cache_key)
16
+ if items.nil?
17
+ items = getTranslations(component, locale)
18
+ SgtnClient::CacheUtil.write_cache(cache_key, items)
19
+ else
20
+ SgtnClient.logger.debug "Getting translations from cache with key: " + cache_key
21
+ end
22
+ if items.nil?
23
+ return SgtnClient::Source.getSource(component, key)
24
+ end
25
+
26
+ str = items["messages"][key]
27
+ if str.nil?
28
+ return SgtnClient::Source.getSource(component, key)
29
+ else
30
+ return str
31
+ end
32
+
33
+ end
34
+
35
+ private
36
+
37
+ def self.getTranslations(component, locale)
38
+ env = SgtnClient::Config.default_environment
39
+ mode = SgtnClient::Config.configurations[env]["bundle_mode"]
40
+ if mode == 'offline'
41
+ return get_offbundle(component, locale)
42
+ else
43
+ return get_server(component, locale)
44
+ end
45
+ end
46
+
47
+ def self.get_offbundle(component, locale)
48
+ env = SgtnClient::Config.default_environment
49
+ product_name = SgtnClient::Config.configurations[env]["product_name"]
50
+ version = SgtnClient::Config.configurations[env]["version"].to_s
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
54
+ begin
55
+ file = File.read(bundlepath)
56
+ data_hash = JSON.parse(file)
57
+ rescue => exception
58
+ SgtnClient.logger.error exception.message
59
+ end
60
+ return data_hash
61
+ end
62
+
63
+ def self.get_server(component, locale)
64
+ env = SgtnClient::Config.default_environment
65
+ product_name = SgtnClient::Config.configurations[env]["product_name"]
66
+ vip_server = SgtnClient::Config.configurations[env]["vip_server"]
67
+ SgtnClient.logger.debug "Getting translations from server: " + vip_server
68
+ version = SgtnClient::Config.configurations[env]["version"].to_s
69
+ url = vip_server + "/i18n/api/v2/translation/products/" + product_name + "/versions/" + version + "/locales/" + locale + "/components/" + component+ "?checkTranslationStatus=false&machineTranslation=false&pseudo=false"
70
+ begin
71
+ obj = SgtnClient::Core::Request.get(url)
72
+ rescue => exception
73
+ SgtnClient.logger.error exception.message
74
+ end
75
+ if obj != nil
76
+ obj = obj["data"]
77
+ end
78
+ return obj
79
+ end
80
+
81
+ end
82
+
83
+ end
@@ -0,0 +1,3 @@
1
+ require 'sgtn-client/cldr/localized_datetime'
2
+ require 'sgtn-client/cldr/localized_date'
3
+ require 'sgtn-client/cldr/localized_time'
@@ -0,0 +1,27 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ Date.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
5
+ def to_full_s(locale = TwitterCldr.locale)
6
+ self.to_datetime().to_date().to_full_s
7
+ end
8
+ LOCALIZE
9
+
10
+ Date.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
11
+ def to_long_s(locale = TwitterCldr.locale)
12
+ self.to_datetime().to_date().to_long_s
13
+ end
14
+ LOCALIZE
15
+
16
+ Date.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
17
+ def to_medium_s(locale = TwitterCldr.locale)
18
+ self.to_datetime().to_date().to_medium_s
19
+ end
20
+ LOCALIZE
21
+
22
+
23
+ Date.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
24
+ def to_short_s(locale = TwitterCldr.locale)
25
+ self.to_datetime().to_date().to_short_s
26
+ end
27
+ LOCALIZE
@@ -0,0 +1,33 @@
1
+ require 'date'
2
+ require 'time'
3
+
4
+ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
5
+ def to_full_s(locale = TwitterCldr.locale)
6
+ self.localize(locale).to_full_s
7
+ end
8
+ LOCALIZE
9
+
10
+ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
11
+ def to_long_s(locale = TwitterCldr.locale)
12
+ self.localize(locale).to_long_s
13
+ end
14
+ LOCALIZE
15
+
16
+ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
17
+ def to_medium_s(locale = TwitterCldr.locale)
18
+ self.localize(locale).to_medium_s
19
+ end
20
+ LOCALIZE
21
+
22
+
23
+ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
24
+ def to_short_s(locale = TwitterCldr.locale)
25
+ self.localize(locale).to_short_s
26
+ end
27
+ LOCALIZE
28
+
29
+ DateTime.class_eval <<-LOCALIZE, __FILE__, __LINE__ + 1
30
+ def to_date(locale = TwitterCldr.locale)
31
+ self.localize(locale).to_date
32
+ end
33
+ LOCALIZE