Class: CopyTunerClient::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/copy_tuner_client/configuration.rb

Overview

Used to set up and modify settings for the client.

Constant Summary collapse

OPTIONS =

These options will be present in the Hash returned by #to_hash.

%i[api_key development_environments environment_name host
http_open_timeout http_read_timeout client_name client_url
client_version port protocol proxy_host proxy_pass
proxy_port proxy_user secure polling_delay sync_interval
sync_interval_staging sync_ignore_path_regex logger
framework middleware disable_middleware disable_test_translation
ca_file local_first_key_regexp s3_host locales ignored_keys ignored_key_handler
download_cache_dir].freeze
BUILTIN_LOCAL_FIRST_KEY_REGEXP =

NOTE: Rails 標準ロケールで非文字列値(precision: Integer, significant: Boolean, strip_insignificant_zeros: Boolean)を含むのは number.*.format 配下のみ。store_item が文字列しか保持できず lookup_in_tree_cache が tree cache をローカル YAML より優先するため、CopyTuner 経由だとprecision 等が欠落し number_to_currency 等が壊れる。これらの Rails 標準 format キーだけを常にローカル優先にする(number 全体ではなく、アプリ独自の number.* キーは対象外)。末尾 (.|z) でnumber.currency.format(ハッシュ lookup)と number.currency.format.precision(葉キー)の両方に対応する。

/\Anumber\.(format|currency\.format|percentage\.format|human\.format)(\.|\z)/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Instantiated from CopyTunerClient.configure. Sets defaults.



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/copy_tuner_client/configuration.rb', line 163

def initialize # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  self.client_name = 'CopyTuner Client'
  self.client_url = 'https://rubygems.org/gems/copy_tuner_client'
  self.client_version = VERSION
  self.development_environments = %w[development staging]
  self.host = 'copy-tuner.com'
  self.http_open_timeout = 5
  self.http_read_timeout = 5
  self.logger = Logger.new($stdout)
  self.polling_delay = 300
  self.sync_interval = 60
  self.sync_interval_staging = 0
  self.secure = true
  self.test_environments = %w[test cucumber]
  self.upload_disabled_environments = %w[production staging]
  self.s3_host = 'copy-tuner.sg-apps.com' # NOTE: cloudfront host
  self.disable_copyray_comment_injection = false
  self.ignored_keys = []
  self.ignored_key_handler = ->(e) { raise e }
  self.local_first_key_regexp = nil
  self.project_id = nil
  self.download_cache_dir = Pathname.new(Dir.pwd).join('tmp', 'cache', 'copy_tuner_client')

  @applied = false
end

Instance Attribute Details

#api_keyString

Returns The API key for your project, found on the project edit form.

Returns:

  • (String)

    The API key for your project, found on the project edit form.



40
41
42
# File 'lib/copy_tuner_client/configuration.rb', line 40

def api_key
  @api_key
end

#ca_fileString

Returns the path to a root certificate file used to verify ssl sessions. Default’s to the root certificate file for copy-tuner.com.

Returns:

  • (String)

    the path to a root certificate file used to verify ssl sessions. Default’s to the root certificate file for copy-tuner.com.



123
124
125
# File 'lib/copy_tuner_client/configuration.rb', line 123

def ca_file
  @ca_file
end

#cacheCache

Returns instance used internally to synchronize changes.

Returns:

  • (Cache)

    instance used internally to synchronize changes.



126
127
128
# File 'lib/copy_tuner_client/configuration.rb', line 126

def cache
  @cache
end

#clientClient

Returns instance used to communicate with a CopyTuner Server.

Returns:

  • (Client)

    instance used to communicate with a CopyTuner Server.



129
130
131
# File 'lib/copy_tuner_client/configuration.rb', line 129

def client
  @client
end

#client_nameString

Returns The name of the client library being used to send notifications (defaults to CopyTuner Client).

Returns:

  • (String)

    The name of the client library being used to send notifications (defaults to CopyTuner Client)



83
84
85
# File 'lib/copy_tuner_client/configuration.rb', line 83

def client_name
  @client_name
end

#client_urlString

Returns The url of the client library being used.

Returns:

  • (String)

    The url of the client library being used



92
93
94
# File 'lib/copy_tuner_client/configuration.rb', line 92

def client_url
  @client_url
end

#client_versionString

Returns The version of the client library being used to send notifications (such as 1.0.2).

Returns:

  • (String)

    The version of the client library being used to send notifications (such as 1.0.2)



89
90
91
# File 'lib/copy_tuner_client/configuration.rb', line 89

def client_version
  @client_version
end

#development_environmentsArray<String>

Returns A list of environments in which content should be editable.

Returns:

  • (Array<String>)

    A list of environments in which content should be editable



71
72
73
# File 'lib/copy_tuner_client/configuration.rb', line 71

def development_environments
  @development_environments
end

#disable_copyray_comment_injectionBoolean

Returns To disable Copyray marker injection, set true.

Returns:

  • (Boolean)

    To disable Copyray marker injection, set true



143
144
145
# File 'lib/copy_tuner_client/configuration.rb', line 143

def disable_copyray_comment_injection
  @disable_copyray_comment_injection
end

#disable_middlewareBoolean

Returns disable middleware setting.

Returns:

  • (Boolean)

    disable middleware setting



114
115
116
# File 'lib/copy_tuner_client/configuration.rb', line 114

def disable_middleware
  @disable_middleware
end

#disable_test_translationBoolean

Returns disable download translation for test enviroment.

Returns:

  • (Boolean)

    disable download translation for test enviroment



120
121
122
# File 'lib/copy_tuner_client/configuration.rb', line 120

def disable_test_translation
  @disable_test_translation
end

#download_cache_dirPathname

Returns The directory to cache downloaded files.

Returns:

  • (Pathname)

    The directory to cache downloaded files



158
159
160
# File 'lib/copy_tuner_client/configuration.rb', line 158

def download_cache_dir
  @download_cache_dir
end

#environment_nameString

Returns The name of the environment the application is running in.

Returns:

  • (String)

    The name of the environment the application is running in



80
81
82
# File 'lib/copy_tuner_client/configuration.rb', line 80

def environment_name
  @environment_name
end

#frameworkString, NilClass

Returns The framework notifications are being sent from, if any (such as Rails 2.3.9).

Returns:

  • (String, NilClass)

    The framework notifications are being sent from, if any (such as Rails 2.3.9)



86
87
88
# File 'lib/copy_tuner_client/configuration.rb', line 86

def framework
  @framework
end

#hostString

Returns The host to connect to (defaults to copy-tuner.com).

Returns:

  • (String)

    The host to connect to (defaults to copy-tuner.com).



43
44
45
# File 'lib/copy_tuner_client/configuration.rb', line 43

def host
  @host
end

#http_open_timeoutFixnum

Returns The HTTP open timeout in seconds (defaults to 2).

Returns:

  • (Fixnum)

    The HTTP open timeout in seconds (defaults to 2).



53
54
55
# File 'lib/copy_tuner_client/configuration.rb', line 53

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutFixnum

Returns The HTTP read timeout in seconds (defaults to 5).

Returns:

  • (Fixnum)

    The HTTP read timeout in seconds (defaults to 5).



56
57
58
# File 'lib/copy_tuner_client/configuration.rb', line 56

def http_read_timeout
  @http_read_timeout
end

#ignored_key_handlerProc

Returns:

  • (Proc)


152
153
154
# File 'lib/copy_tuner_client/configuration.rb', line 152

def ignored_key_handler
  @ignored_key_handler
end

#ignored_keysArray<String>

Returns A list of ignored keys.

Returns:

  • (Array<String>)

    A list of ignored keys



149
150
151
# File 'lib/copy_tuner_client/configuration.rb', line 149

def ignored_keys
  @ignored_keys
end

#local_first_key_regexpRegexp

Returns Keys (without locale) matching this regexp bypass the copy_tuner cache and are looked up from local config/locales (I18n::Backend::Simple) first. Used for gradual migration from copy_tuner to local YAML.

Returns:

  • (Regexp)

    Keys (without locale) matching this regexp bypass the copy_tuner cache and are looked up from local config/locales (I18n::Backend::Simple) first. Used for gradual migration from copy_tuner to local YAML.



137
138
139
# File 'lib/copy_tuner_client/configuration.rb', line 137

def local_first_key_regexp
  @local_first_key_regexp
end

#localesArray<Symbol>

Returns Restrict blurb locales to upload.

Returns:

  • (Array<Symbol>)

    Restrict blurb locales to upload



146
147
148
# File 'lib/copy_tuner_client/configuration.rb', line 146

def locales
  @locales
end

#loggerLogger

Returns Where to log messages. Must respond to same interface as Logger.

Returns:

  • (Logger)

    Where to log messages. Must respond to same interface as Logger.



108
109
110
# File 'lib/copy_tuner_client/configuration.rb', line 108

def logger
  @logger
end

#middlewareObject

Returns the middleware stack, if any, which should respond to use.

Returns:

  • the middleware stack, if any, which should respond to use



111
112
113
# File 'lib/copy_tuner_client/configuration.rb', line 111

def middleware
  @middleware
end

#middleware_positionObject

OtherMiddleware or OtherMiddleware



117
118
119
# File 'lib/copy_tuner_client/configuration.rb', line 117

def middleware_position
  @middleware_position
end

#pollerObject

Returns the value of attribute poller.



131
132
133
# File 'lib/copy_tuner_client/configuration.rb', line 131

def poller
  @poller
end

#polling_delayInteger

Returns The time, in seconds, in between each sync to the server. Defaults to 300.

Returns:

  • (Integer)

    The time, in seconds, in between each sync to the server. Defaults to 300.



95
96
97
# File 'lib/copy_tuner_client/configuration.rb', line 95

def polling_delay
  @polling_delay
end

#portObject



303
304
305
# File 'lib/copy_tuner_client/configuration.rb', line 303

def port
  @port || default_port
end

#project_idInteger

Returns The project id.

Returns:

  • (Integer)

    The project id



155
156
157
# File 'lib/copy_tuner_client/configuration.rb', line 155

def project_id
  @project_id
end

#proxy_hostString, NilClass

Returns The hostname of your proxy server (if using a proxy).

Returns:

  • (String, NilClass)

    The hostname of your proxy server (if using a proxy)



59
60
61
# File 'lib/copy_tuner_client/configuration.rb', line 59

def proxy_host
  @proxy_host
end

#proxy_passString, NilClass

Returns The password to use when logging into your proxy server (if using a proxy).

Returns:

  • (String, NilClass)

    The password to use when logging into your proxy server (if using a proxy)



68
69
70
# File 'lib/copy_tuner_client/configuration.rb', line 68

def proxy_pass
  @proxy_pass
end

#proxy_portString, Fixnum

Returns The port of your proxy server (if using a proxy).

Returns:

  • (String, Fixnum)

    The port of your proxy server (if using a proxy)



62
63
64
# File 'lib/copy_tuner_client/configuration.rb', line 62

def proxy_port
  @proxy_port
end

#proxy_userString, NilClass

Returns The username to use when logging into your proxy server (if using a proxy).

Returns:

  • (String, NilClass)

    The username to use when logging into your proxy server (if using a proxy)



65
66
67
# File 'lib/copy_tuner_client/configuration.rb', line 65

def proxy_user
  @proxy_user
end

#s3_hostString

Returns The S3 host to connect to (defaults to copy-tuner-us.s3.amazonaws.com).

Returns:

  • (String)

    The S3 host to connect to (defaults to copy-tuner-us.s3.amazonaws.com).



140
141
142
# File 'lib/copy_tuner_client/configuration.rb', line 140

def s3_host
  @s3_host
end

#secureBoolean Also known as: secure?

Returns true for https connections, false for http connections.

Returns:

  • (Boolean)

    true for https connections, false for http connections.



50
51
52
# File 'lib/copy_tuner_client/configuration.rb', line 50

def secure
  @secure
end

#sync_ignore_path_regexRegex

Returns Format ignore hook middleware sync.

Returns:

  • (Regex)

    Format ignore hook middleware sync



105
106
107
# File 'lib/copy_tuner_client/configuration.rb', line 105

def sync_ignore_path_regex
  @sync_ignore_path_regex
end

#sync_intervalObject

Sync interval for Rack Middleware



338
339
340
341
342
343
344
# File 'lib/copy_tuner_client/configuration.rb', line 338

def sync_interval
  if environment_name == 'staging'
    @sync_interval_staging
  else
    @sync_interval
  end
end

#sync_interval_stagingInteger

Returns The time, in seconds, in between each sync to the server in development. Defaults to 60.

Returns:

  • (Integer)

    The time, in seconds, in between each sync to the server in development. Defaults to 60.



102
103
104
# File 'lib/copy_tuner_client/configuration.rb', line 102

def sync_interval_staging
  @sync_interval_staging
end

#test_environmentsArray<String>

Returns A list of environments in which the server should not be contacted.

Returns:

  • (Array<String>)

    A list of environments in which the server should not be contacted



74
75
76
# File 'lib/copy_tuner_client/configuration.rb', line 74

def test_environments
  @test_environments
end

#upload_disabled_environmentsArray<String>

Returns A list of environments in which the server should not be upload.

Returns:

  • (Array<String>)

    A list of environments in which the server should not be upload



77
78
79
# File 'lib/copy_tuner_client/configuration.rb', line 77

def upload_disabled_environments
  @upload_disabled_environments
end

Class Method Details

.builtin_local_first_key?(key_without_locale) ⇒ Boolean

lookup 経路(Configuration#local_first_key?)と upload 抑止経路(Cache#[]=)で同じ組み込み判定を共有する。判定を 1 箇所に集約することで number 以外を足す際の同期漏れを防ぐ。

Returns:

  • (Boolean)


35
36
37
# File 'lib/copy_tuner_client/configuration.rb', line 35

def self.builtin_local_first_key?(key_without_locale)
  key_without_locale.to_s.match?(BUILTIN_LOCAL_FIRST_KEY_REGEXP)
end

Instance Method Details

#[](option) ⇒ Object

Allows config options to be read like a hash

Parameters:

  • option (Symbol)

    Key for a given attribute

Returns:

  • (Object)

    the given attribute



193
194
195
# File 'lib/copy_tuner_client/configuration.rb', line 193

def [](option)
  public_send(option)
end

#applied?Boolean

Determines if the configuration has been applied (internal)

Returns:

  • (Boolean)

    Returns true if applied, false otherwise.



244
245
246
# File 'lib/copy_tuner_client/configuration.rb', line 244

def applied?
  @applied
end

#applyObject

Applies the configuration (internal).

Called automatically when CopyTunerClient.configure is called in the application.

This creates the I18nBackend and puts them together.

When #test? returns false, the poller will be started.



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
# File 'lib/copy_tuner_client/configuration.rb', line 255

def apply # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
  # NOTE: project_id は必須。未設定なら apply 時点で明示的に失敗させる
  validate_project_id!

  self.locales ||= self.locales = if defined?(::Rails)
                                    ::Rails.application.config.i18n.available_locales.presence || Array(::Rails.application.config.i18n.default_locale)
                                  else
                                    [:en]
                                  end

  self.client ||= Client.new(to_hash)
  self.cache ||= Cache.new(client, to_hash)
  @poller = Poller.new(cache, to_hash)
  process_guard = ProcessGuard.new(cache, @poller, to_hash)
  I18n.backend = I18nBackend.new(cache)

  if enable_middleware?
    logger.info 'Using copytuner sync middleware'
    request_sync_options = { poller: @poller, cache:, interval: sync_interval, ignore_regex: sync_ignore_path_regex }
    if middleware_position.is_a?(Hash) && middleware_position[:before]
      middleware.insert_before middleware_position[:before], RequestSync, request_sync_options
      middleware.insert_before middleware_position[:before], CopyTunerClient::CopyrayMiddleware
    elsif middleware_position.is_a?(Hash) && middleware_position[:after]
      middleware.insert_after middleware_position[:after], RequestSync, request_sync_options
      middleware.insert_after middleware_position[:after], CopyTunerClient::CopyrayMiddleware
    else
      middleware.use RequestSync, request_sync_options
      middleware.use CopyTunerClient::CopyrayMiddleware
    end
  else
    logger.info '[[[Warn]]] Not using copytuner sync middleware' unless middleware
  end

  @applied = true
  logger.info "Client #{VERSION} ready (s3_download)"
  logger.info "Environment Info: #{environment_info}"
  logger.info "Available locales: #{self.locales.join(' ')}"

  unless test?
    process_guard.start
  end

  unless test? && disable_test_translation
    logger.info 'Download translation now'
    cache.download
  end
end

#development?Boolean

Determines if the content will be editable

Returns:

  • (Boolean)

    Returns true if in a development environment, false otherwise.



224
225
226
# File 'lib/copy_tuner_client/configuration.rb', line 224

def development?
  development_environments.include? environment_name
end

#enable_middleware?Boolean

Returns:

  • (Boolean)


228
229
230
# File 'lib/copy_tuner_client/configuration.rb', line 228

def enable_middleware?
  middleware && development? && !disable_middleware
end

#environment_infoString

For logging/debugging (internal).

Returns:

  • (String)

    a description of the environment in which this configuration was built.



319
320
321
322
# File 'lib/copy_tuner_client/configuration.rb', line 319

def environment_info
  parts = ["Ruby: #{RUBY_VERSION}", framework, "Env: #{environment_name}"]
  parts.compact.map { |part| "[#{part}]" }.join(' ')
end

#local_first_key?(key_without_locale) ⇒ Boolean

locale を除いたキーが local_first_key_regexp にマッチするかを返す。マッチするキーはローカル config/locales(CopyTuner 管理外)で管理されるため、オーバーレイマーカー注入やキャッシュ参照をスキップする必要がある。

Parameters:

  • key_without_locale (String, Symbol, nil)

    locale prefix を除いたキー(例: “views.foo.bar”)

Returns:

  • (Boolean)


360
361
362
363
364
365
366
367
368
# File 'lib/copy_tuner_client/configuration.rb', line 360

def local_first_key?(key_without_locale)
  return false if key_without_locale.nil?

  normalized = key_without_locale.to_s
  return true if self.class.builtin_local_first_key?(normalized)
  return false if local_first_key_regexp.nil?

  normalized.match?(local_first_key_regexp)
end

#merge(hash) ⇒ Hash

Returns a hash of all configurable options merged with hash

Parameters:

  • hash (Hash)

    A set of configuration options that will take precedence over the defaults

Returns:

  • (Hash)

    the merged configuration hash



211
212
213
# File 'lib/copy_tuner_client/configuration.rb', line 211

def merge(hash)
  to_hash.merge hash
end

#project_urlString

Returns current project url by project_id.

Returns:

  • (String)

    current project url by project_id



347
348
349
350
351
352
# File 'lib/copy_tuner_client/configuration.rb', line 347

def project_url
  validate_project_id!

  path = "/projects/#{project_id}"
  URI::Generic.build(scheme: self.protocol, host: self.host, port: self.port.to_i, path:).to_s
end

#protocolString

The protocol that should be used when generating URLs to CopyTuner.

Returns:

  • (String)

    https if #secure? returns true, http otherwise.



309
310
311
312
313
314
315
# File 'lib/copy_tuner_client/configuration.rb', line 309

def protocol
  if secure?
    'https'
  else
    'http'
  end
end

#public?Boolean

Determines if the published or draft content will be used environment, true otherwise.

Returns:

  • (Boolean)

    Returns false if in a development or test



218
219
220
# File 'lib/copy_tuner_client/configuration.rb', line 218

def public?
  !(development_environments + test_environments).include?(environment_name)
end

#test?Boolean

Determines if the content will fetched from the server

Returns:

  • (Boolean)

    Returns true if in a test environment, false otherwise.



234
235
236
# File 'lib/copy_tuner_client/configuration.rb', line 234

def test?
  test_environments.include?(environment_name)
end

#to_hashHash

Returns a hash of all configurable options

Returns:

  • (Hash)

    configuration attributes



199
200
201
202
203
204
205
# File 'lib/copy_tuner_client/configuration.rb', line 199

def to_hash
  base_options = { public: public?, upload_disabled: upload_disabled? }

  OPTIONS.inject(base_options) do |hash, option|
    hash.merge option.to_sym => public_send(option)
  end
end

#upload_disabled?Boolean

Returns:

  • (Boolean)


238
239
240
# File 'lib/copy_tuner_client/configuration.rb', line 238

def upload_disabled?
  upload_disabled_environments.include?(environment_name)
end