copy_tuner_client 0.5.1 → 0.7.0

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.
data/Rakefile CHANGED
@@ -1,26 +1,16 @@
1
1
  require 'bundler/gem_tasks'
2
- require 'appraisal'
3
- require 'cucumber/rake/task'
4
2
  require 'rspec/core/rake_task'
5
3
  require 'yard'
6
4
 
7
- desc 'Default: run the specs and features.'
8
- task :default => [:spec, :cucumber]
5
+ desc 'Default: run the specs.'
6
+ task default: [:spec]
9
7
 
10
8
  desc 'Test the copy_tuner_client plugin.'
11
9
  RSpec::Core::RakeTask.new do |t|
12
- t.rspec_opts = ['--color', "--format progress"]
10
+ t.rspec_opts = ['--color', '--format progress']
13
11
  t.pattern = 'spec/copy_tuner_client/**/*_spec.rb'
14
12
  end
15
13
 
16
- desc "Run cucumber features"
17
- Cucumber::Rake::Task.new do |t|
18
- t.cucumber_opts = [
19
- '--tags', '~@wip',
20
- '--format', (ENV['CUCUMBER_FORMAT'] || 'progress')
21
- ]
22
- end
23
-
24
14
  YARD::Rake::YardocTask.new do |t|
25
- t.files = ['lib/**/*.rb']
15
+ t.files = ['lib/**/*.rb']
26
16
  end
@@ -3,15 +3,12 @@ $LOAD_PATH.push File.expand_path('../lib', __FILE__)
3
3
  require 'copy_tuner_client/version'
4
4
 
5
5
  Gem::Specification.new do |s|
6
- s.required_ruby_version = '>= 2.4.0'
6
+ s.required_ruby_version = '>= 2.5.0'
7
7
  s.add_dependency 'i18n', '>= 0.5.0'
8
8
  s.add_dependency 'json'
9
- s.add_development_dependency 'appraisal', '~> 2.1'
10
- s.add_development_dependency 'aruba', '~> 0.6.0'
11
- s.add_development_dependency 'cucumber', '~> 1.3.16'
12
- s.add_development_dependency 'rails', '~> 4.2.6'
9
+ s.add_development_dependency 'rails', '~> 5.2.4.2'
13
10
  s.add_development_dependency 'rake'
14
- s.add_development_dependency 'rspec'
11
+ s.add_development_dependency 'rspec', '3.8.0' # NOTE: 3.9.0だとundefined method `synchronize'でコケるテストがある
15
12
  s.add_development_dependency 'sham_rack'
16
13
  s.add_development_dependency 'sinatra'
17
14
  s.add_development_dependency 'sqlite3'
data/gemfiles/5.2.gemfile CHANGED
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "5.2.3"
5
+ gem "rails", "5.2.4.2"
6
6
 
7
7
  gemspec :path => "../"
@@ -2,6 +2,6 @@
2
2
 
3
3
  source "http://rubygems.org"
4
4
 
5
- gem "rails", "5.0.7.2"
5
+ gem "rails", "6.0.2.2"
6
6
 
7
7
  gemspec :path => "../"
@@ -1,4 +1,3 @@
1
- require 'thread'
2
1
  require 'copy_tuner_client/client'
3
2
  require 'copy_tuner_client/dotted_hash'
4
3
 
@@ -18,6 +17,7 @@ module CopyTunerClient
18
17
  @logger = options[:logger]
19
18
  @mutex = Mutex.new
20
19
  @exclude_key_regexp = options[:exclude_key_regexp]
20
+ @upload_disabled = options[:upload_disabled]
21
21
  @locales = Array(options[:locales]).map(&:to_s)
22
22
  # mutable states
23
23
  @blurbs = {}
@@ -40,9 +40,13 @@ module CopyTunerClient
40
40
  # @param value [String] the new contents of the blurb
41
41
  def []=(key, value)
42
42
  return if @exclude_key_regexp && key.match?(@exclude_key_regexp)
43
+ return unless key.include?('.')
43
44
  return if @locales.present? && !@locales.member?(key.split('.').first)
45
+ return if @upload_disabled
46
+
44
47
  lock do
45
48
  return if @blank_keys.member?(key) || @blurbs.key?(key)
49
+
46
50
  @queued[key] = value
47
51
  end
48
52
  end
@@ -61,17 +65,15 @@ module CopyTunerClient
61
65
 
62
66
  # Waits until the first download has finished.
63
67
  def wait_for_download
64
- if pending?
65
- logger.info 'Waiting for first download'
68
+ return unless pending?
66
69
 
67
- if logger.respond_to? :flush
68
- logger.flush
69
- end
70
+ logger.info 'Waiting for first download'
70
71
 
71
- while pending?
72
- sleep 0.1
73
- end
72
+ if logger.respond_to? :flush
73
+ logger.flush
74
74
  end
75
+
76
+ sleep 0.1 while pending?
75
77
  end
76
78
 
77
79
  def flush
@@ -82,8 +84,8 @@ module CopyTunerClient
82
84
  @last_uploaded_at = Time.now.utc
83
85
 
84
86
  res
85
- rescue ConnectionError => error
86
- logger.error error.message
87
+ rescue ConnectionError => e
88
+ logger.error e.message
87
89
  end
88
90
 
89
91
  def download
@@ -100,8 +102,8 @@ module CopyTunerClient
100
102
  @last_downloaded_at = Time.now.utc
101
103
 
102
104
  res
103
- rescue ConnectionError => error
104
- logger.error error.message
105
+ rescue ConnectionError => e
106
+ logger.error e.message
105
107
  ensure
106
108
  @downloaded = true
107
109
  end
@@ -57,6 +57,9 @@ module CopyTunerClient
57
57
  # @return [Array<String>] A list of environments in which the server should not be contacted
58
58
  attr_accessor :test_environments
59
59
 
60
+ # @return [Array<String>] A list of environments in which the server should not be upload
61
+ attr_accessor :upload_disabled_environments
62
+
60
63
  # @return [String] The name of the environment the application is running in
61
64
  attr_accessor :environment_name
62
65
 
@@ -145,6 +148,7 @@ module CopyTunerClient
145
148
  self.sync_interval_staging = 0
146
149
  self.secure = true
147
150
  self.test_environments = %w(test cucumber)
151
+ self.upload_disabled_environments = []
148
152
  self.s3_host = 'copy-tuner-data-prod.s3.amazonaws.com'
149
153
  self.disable_copyray_comment_injection = false
150
154
  self.html_escape = false
@@ -163,7 +167,7 @@ module CopyTunerClient
163
167
  # Returns a hash of all configurable options
164
168
  # @return [Hash] configuration attributes
165
169
  def to_hash
166
- base_options = { :public => public? }
170
+ base_options = { public: public?, upload_disabled: upload_disabled? }
167
171
 
168
172
  OPTIONS.inject(base_options) do |hash, option|
169
173
  hash.merge option.to_sym => send(option)
@@ -198,7 +202,11 @@ module CopyTunerClient
198
202
  # Determines if the content will fetched from the server
199
203
  # @return [Boolean] Returns +true+ if in a test environment, +false+ otherwise.
200
204
  def test?
201
- test_environments.include? environment_name
205
+ test_environments.include?(environment_name)
206
+ end
207
+
208
+ def upload_disabled?
209
+ upload_disabled_environments.include?(environment_name)
202
210
  end
203
211
 
204
212
  # Determines if the configuration has been applied (internal)
@@ -13,7 +13,13 @@ module CopyTunerClient
13
13
  ActionView::Helpers::TranslationHelper.class_eval do
14
14
  def translate_with_copyray_comment(key, options = {})
15
15
  source = translate_without_copyray_comment(key, options)
16
- if !CopyTunerClient.configuration.disable_copyray_comment_injection && (options[:rescue_format] == :html || options[:rescue_format].nil?)
16
+ # TODO: 0.6.0以降で options[:rescue_format] == text を消す
17
+ if CopyTunerClient.configuration.disable_copyray_comment_injection || options[:rescue_format] == :text
18
+ if options[:rescue_format] == :text
19
+ ActiveSupport::Deprecation.warn('rescue_format option is deprecated in copy_tuner_client@0.6.0')
20
+ end
21
+ source
22
+ else
17
23
  separator = options[:separator] || I18n.default_separator
18
24
  scope = options[:scope]
19
25
  normalized_key =
@@ -23,8 +29,6 @@ module CopyTunerClient
23
29
  I18n.normalize_keys(nil, key, scope, separator).join(separator)
24
30
  end
25
31
  CopyTunerClient::Copyray.augment_template(source, normalized_key)
26
- else
27
- source
28
32
  end
29
33
  end
30
34
  if CopyTunerClient.configuration.enable_middleware?
@@ -19,12 +19,10 @@ module CopyTunerClient
19
19
  def self.install_hook
20
20
  I18n.class_eval do
21
21
  class << self
22
- def translate_with_copy_tuner_hook(*args)
23
- key = args[0]
24
- options = args.last.is_a?(Hash) ? args.last : {}
22
+ def translate_with_copy_tuner_hook(key, **options)
25
23
  scope = options[:scope]
26
24
  scope = scope.dup if scope.is_a?(Array) || scope.is_a?(String)
27
- result = translate_without_copy_tuner_hook(*args)
25
+ result = translate_without_copy_tuner_hook(key, **options)
28
26
 
29
27
  if key.is_a?(Array)
30
28
  key.zip(result).each { |k, v| CopyTunerClient::TranslationLog.add(I18n.normalize_keys(nil, k, scope).join('.'), v) unless v.is_a?(Array) }
@@ -33,6 +31,7 @@ module CopyTunerClient
33
31
  end
34
32
  result
35
33
  end
34
+
36
35
  if CopyTunerClient.configuration.enable_middleware?
37
36
  alias_method :translate_without_copy_tuner_hook, :translate
38
37
  alias_method :translate, :translate_with_copy_tuner_hook
@@ -1,6 +1,6 @@
1
1
  module CopyTunerClient
2
2
  # Client version
3
- VERSION = '0.5.1'.freeze
3
+ VERSION = '0.7.0'.freeze
4
4
 
5
5
  # API version being used to communicate with the server
6
6
  API_VERSION = '2.0'.freeze
@@ -6,13 +6,13 @@ namespace :copy_tuner do
6
6
  end
7
7
 
8
8
  desc "Export CopyTuner blurbs to yaml."
9
- task :export => :environment do
9
+ task :export, %i[path] => :environment do |_, args|
10
+ args.with_defaults(path: "config/locales/copy_tuner.yml")
10
11
  CopyTunerClient.cache.sync
11
12
 
12
13
  if yml = CopyTunerClient.export
13
- path = "config/locales/copy_tuner.yml"
14
- File.new("#{Rails.root}/#{path}", 'w').write(yml)
15
- puts "Successfully exported blurbs to #{path}."
14
+ File.new("#{Rails.root}/#{args[:path]}", 'w').write(yml)
15
+ puts "Successfully exported blurbs to #{args[:path]}."
16
16
  else
17
17
  raise "No blurbs have been cached."
18
18
  end
@@ -9,7 +9,7 @@ describe CopyTunerClient::Cache do
9
9
  CopyTunerClient::Cache.new(client, default_config.update(config))
10
10
  end
11
11
 
12
- it "provides access to downloaded data" do
12
+ it 'provides access to downloaded data' do
13
13
  client['en.test.key'] = 'expected'
14
14
  client['en.test.other_key'] = 'expected'
15
15
 
@@ -18,17 +18,17 @@ describe CopyTunerClient::Cache do
18
18
  cache.download
19
19
 
20
20
  expect(cache['en.test.key']).to eq('expected')
21
- expect(cache.keys).to match_array(%w(en.test.key en.test.other_key))
21
+ expect(cache.keys).to match_array(%w[en.test.key en.test.other_key])
22
22
  end
23
23
 
24
- it "exclude data if exclude_key_regexp is set" do
24
+ it 'exclude data if exclude_key_regexp is set' do
25
25
  cache = build_cache(exclude_key_regexp: /^en\.test\.other_key$/)
26
26
  cache['en.test.key'] = 'expected'
27
27
  cache['en.test.other_key'] = 'expected'
28
28
 
29
29
  cache.download
30
30
 
31
- expect(cache.queued.keys).to match_array(%w(en.test.key))
31
+ expect(cache.queued.keys).to match_array(%w[en.test.key])
32
32
  end
33
33
 
34
34
  it "doesn't upload without changes" do
@@ -37,7 +37,15 @@ describe CopyTunerClient::Cache do
37
37
  expect(client).not_to be_uploaded
38
38
  end
39
39
 
40
- it "uploads changes when flushed" do
40
+ it "Don't upload incorrect key" do
41
+ cache = build_cache
42
+ cache['ja'] = 'incorrect key'
43
+
44
+ cache.flush
45
+ expect(client).not_to be_uploaded
46
+ end
47
+
48
+ it 'uploads changes when flushed' do
41
49
  cache = build_cache
42
50
  cache['test.key'] = 'test value'
43
51
 
@@ -46,7 +54,7 @@ describe CopyTunerClient::Cache do
46
54
  expect(client.uploaded).to eq({ 'test.key' => 'test value' })
47
55
  end
48
56
 
49
- it "uploads empties when nil is assigned" do
57
+ it 'uploads empties when nil is assigned' do
50
58
  cache = build_cache
51
59
  cache['test.key'] = nil
52
60
 
@@ -62,7 +70,7 @@ describe CopyTunerClient::Cache do
62
70
 
63
71
  cache.flush
64
72
 
65
- expect(client.uploaded).to eq({'en.test.key' => 'uploaded en', 'ja.test.key' => 'uploaded ja'})
73
+ expect(client.uploaded).to eq({ 'en.test.key' => 'uploaded en', 'ja.test.key' => 'uploaded ja' })
66
74
  end
67
75
 
68
76
  it 'upload with locale filter' do
@@ -72,10 +80,10 @@ describe CopyTunerClient::Cache do
72
80
 
73
81
  cache.flush
74
82
 
75
- expect(client.uploaded).to eq({'en.test.key' => 'uploaded'})
83
+ expect(client.uploaded).to eq({ 'en.test.key' => 'uploaded' })
76
84
  end
77
85
 
78
- it "downloads changes" do
86
+ it 'downloads changes' do
79
87
  client['test.key'] = 'test value'
80
88
  cache = build_cache
81
89
 
@@ -84,7 +92,7 @@ describe CopyTunerClient::Cache do
84
92
  expect(cache['test.key']).to eq('test value')
85
93
  end
86
94
 
87
- it "downloads and uploads when synced" do
95
+ it 'downloads and uploads when synced' do
88
96
  cache = build_cache
89
97
  client['test.key'] = 'test value'
90
98
  cache['other.key'] = 'other value'
@@ -95,7 +103,7 @@ describe CopyTunerClient::Cache do
95
103
  expect(cache['test.key']).to eq('test value')
96
104
  end
97
105
 
98
- it "download included empty keys" do
106
+ it 'download included empty keys' do
99
107
  client['en.test.key'] = 'test value'
100
108
  client['en.test.empty'] = ''
101
109
  cache = build_cache
@@ -109,7 +117,7 @@ describe CopyTunerClient::Cache do
109
117
  expect(cache.queued).to be_empty
110
118
  end
111
119
 
112
- it "Do not upload downloaded keys" do
120
+ it 'Do not upload downloaded keys' do
113
121
  client['en.test.key'] = 'test value'
114
122
  cache = build_cache
115
123
 
@@ -119,11 +127,11 @@ describe CopyTunerClient::Cache do
119
127
  expect(cache.queued).to be_empty
120
128
  end
121
129
 
122
- it "handles connection errors when flushing" do
123
- failure = "server is napping"
130
+ it 'handles connection errors when flushing' do
131
+ failure = 'server is napping'
124
132
  logger = FakeLogger.new
125
133
  expect(client).to receive(:upload).and_raise(CopyTunerClient::ConnectionError.new(failure))
126
- cache = build_cache(:logger => logger)
134
+ cache = build_cache(logger: logger)
127
135
  cache['upload.key'] = 'upload'
128
136
 
129
137
  cache.flush
@@ -131,22 +139,22 @@ describe CopyTunerClient::Cache do
131
139
  expect(logger).to have_entry(:error, failure)
132
140
  end
133
141
 
134
- it "handles connection errors when downloading" do
135
- failure = "server is napping"
142
+ it 'handles connection errors when downloading' do
143
+ failure = 'server is napping'
136
144
  logger = FakeLogger.new
137
145
  expect(client).to receive(:download).and_raise(CopyTunerClient::ConnectionError.new(failure))
138
- cache = build_cache(:logger => logger)
146
+ cache = build_cache(logger: logger)
139
147
 
140
148
  cache.download
141
149
 
142
150
  expect(logger).to have_entry(:error, failure)
143
151
  end
144
152
 
145
- it "blocks until the first download is complete" do
153
+ it 'blocks until the first download is complete' do
146
154
  logger = FakeLogger.new
147
155
  expect(logger).to receive(:flush)
148
156
  client.delay = true
149
- cache = build_cache(:logger => logger)
157
+ cache = build_cache(logger: logger)
150
158
 
151
159
  t_download = Thread.new { cache.download }
152
160
  sleep 0.1 until cache.pending?
@@ -154,7 +162,7 @@ describe CopyTunerClient::Cache do
154
162
  t_wait = Thread.new do
155
163
  cache.wait_for_download
156
164
  end
157
- sleep 0.1 until logger.has_entry?(:info, "Waiting for first download")
165
+ sleep 0.1 until logger.has_entry?(:info, 'Waiting for first download')
158
166
  client.go
159
167
  expect(t_download.join(1)).not_to be_nil
160
168
  expect(cache.pending?).to be_falsey
@@ -163,7 +171,7 @@ describe CopyTunerClient::Cache do
163
171
 
164
172
  it "doesn't block if the first download fails" do
165
173
  client.delay = true
166
- client.error = StandardError.new("Failure")
174
+ client.error = StandardError.new('Failure')
167
175
  cache = build_cache
168
176
 
169
177
  error = nil
@@ -183,12 +191,12 @@ describe CopyTunerClient::Cache do
183
191
  expect(t_download.join(1)).not_to be_nil
184
192
  expect(error).to be_kind_of(StandardError)
185
193
  expect(t_wait.join(1)).not_to be_nil
186
- expect { cache.download }.to raise_error(StandardError, "Failure")
194
+ expect { cache.download }.to raise_error(StandardError, 'Failure')
187
195
  end
188
196
 
189
197
  it "doesn't block before downloading" do
190
198
  logger = FakeLogger.new
191
- cache = build_cache(:logger => logger)
199
+ cache = build_cache(logger: logger)
192
200
 
193
201
  finished = false
194
202
  Thread.new do
@@ -199,7 +207,7 @@ describe CopyTunerClient::Cache do
199
207
  sleep(1)
200
208
 
201
209
  expect(finished).to eq(true)
202
- expect(logger).not_to have_entry(:info, "Waiting for first download")
210
+ expect(logger).not_to have_entry(:info, 'Waiting for first download')
203
211
  end
204
212
 
205
213
  it "doesn't return blank copy" do
@@ -211,13 +219,13 @@ describe CopyTunerClient::Cache do
211
219
  expect(cache['en.test.key']).to be_nil
212
220
  end
213
221
 
214
- describe "given locked mutex" do
222
+ describe 'given locked mutex' do
215
223
  RSpec::Matchers.define :finish_after_unlocking do |mutex|
216
224
  match do |thread|
217
225
  sleep(0.1)
218
226
 
219
227
  if thread.status === false
220
- violated("finished before unlocking")
228
+ violated('finished before unlocking')
221
229
  else
222
230
  mutex.unlock
223
231
  sleep(0.1)
@@ -225,7 +233,7 @@ describe CopyTunerClient::Cache do
225
233
  if thread.status === false
226
234
  true
227
235
  else
228
- violated("still running after unlocking")
236
+ violated('still running after unlocking')
229
237
  end
230
238
  end
231
239
  end
@@ -248,20 +256,20 @@ describe CopyTunerClient::Cache do
248
256
  allow(Mutex).to receive(:new).and_return(mutex)
249
257
  end
250
258
 
251
- it "synchronizes read access to keys between threads" do
259
+ it 'synchronizes read access to keys between threads' do
252
260
  expect(Thread.new { cache['test.key'] }).to finish_after_unlocking(mutex)
253
261
  end
254
262
 
255
- it "synchronizes read access to the key list between threads" do
263
+ it 'synchronizes read access to the key list between threads' do
256
264
  expect(Thread.new { cache.keys }).to finish_after_unlocking(mutex)
257
265
  end
258
266
 
259
- it "synchronizes write access to keys between threads" do
267
+ it 'synchronizes write access to keys between threads' do
260
268
  expect(Thread.new { cache['test.key'] = 'value' }).to finish_after_unlocking(mutex)
261
269
  end
262
270
  end
263
271
 
264
- it "flushes from the top level" do
272
+ it 'flushes from the top level' do
265
273
  cache = build_cache
266
274
  CopyTunerClient.configure do |config|
267
275
  config.cache = cache
@@ -271,7 +279,7 @@ describe CopyTunerClient::Cache do
271
279
  CopyTunerClient.flush
272
280
  end
273
281
 
274
- describe "#export" do
282
+ describe '#export' do
275
283
  subject { cache.export }
276
284
 
277
285
  let(:cache) do
@@ -280,7 +288,7 @@ describe CopyTunerClient::Cache do
280
288
  cache
281
289
  end
282
290
 
283
- it "can be invoked from the top-level constant" do
291
+ it 'can be invoked from the top-level constant' do
284
292
  CopyTunerClient.configure do |config|
285
293
  config.cache = cache
286
294
  end
@@ -292,7 +300,7 @@ describe CopyTunerClient::Cache do
292
300
  is_expected.to eq nil
293
301
  end
294
302
 
295
- context "with single-level blurb keys" do
303
+ context 'with single-level blurb keys' do
296
304
  before do
297
305
  client['key'] = 'test value'
298
306
  client['other_key'] = 'other test value'