Class: CopyTunerClient::Configuration
- Inherits:
-
Object
- Object
- CopyTunerClient::Configuration
- 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
-
#api_key ⇒ String
The API key for your project, found on the project edit form.
-
#ca_file ⇒ String
The path to a root certificate file used to verify ssl sessions.
-
#cache ⇒ Cache
Instance used internally to synchronize changes.
-
#client ⇒ Client
Instance used to communicate with a CopyTuner Server.
-
#client_name ⇒ String
The name of the client library being used to send notifications (defaults to
CopyTuner Client). -
#client_url ⇒ String
The url of the client library being used.
-
#client_version ⇒ String
The version of the client library being used to send notifications (such as
1.0.2). -
#development_environments ⇒ Array<String>
A list of environments in which content should be editable.
-
#disable_copyray_comment_injection ⇒ Boolean
To disable Copyray marker injection, set true.
-
#disable_middleware ⇒ Boolean
Disable middleware setting.
-
#disable_test_translation ⇒ Boolean
Disable download translation for test enviroment.
-
#download_cache_dir ⇒ Pathname
The directory to cache downloaded files.
-
#environment_name ⇒ String
The name of the environment the application is running in.
-
#framework ⇒ String, NilClass
The framework notifications are being sent from, if any (such as
Rails 2.3.9). -
#host ⇒ String
The host to connect to (defaults to
copy-tuner.com). -
#http_open_timeout ⇒ Fixnum
The HTTP open timeout in seconds (defaults to
2). -
#http_read_timeout ⇒ Fixnum
The HTTP read timeout in seconds (defaults to
5). - #ignored_key_handler ⇒ Proc
-
#ignored_keys ⇒ Array<String>
A list of ignored keys.
-
#local_first_key_regexp ⇒ Regexp
Keys (without locale) matching this regexp bypass the copy_tuner cache and are looked up from local config/locales (I18n::Backend::Simple) first.
-
#locales ⇒ Array<Symbol>
Restrict blurb locales to upload.
-
#logger ⇒ Logger
Where to log messages.
-
#middleware ⇒ Object
The middleware stack, if any, which should respond to
use. -
#middleware_position ⇒ Object
OtherMiddleware or OtherMiddleware.
-
#poller ⇒ Object
Returns the value of attribute poller.
-
#polling_delay ⇒ Integer
The time, in seconds, in between each sync to the server.
- #port ⇒ Object
-
#project_id ⇒ Integer
The project id.
-
#proxy_host ⇒ String, NilClass
The hostname of your proxy server (if using a proxy).
-
#proxy_pass ⇒ String, NilClass
The password to use when logging into your proxy server (if using a proxy).
-
#proxy_port ⇒ String, Fixnum
The port of your proxy server (if using a proxy).
-
#proxy_user ⇒ String, NilClass
The username to use when logging into your proxy server (if using a proxy).
-
#s3_host ⇒ String
The S3 host to connect to (defaults to
copy-tuner-us.s3.amazonaws.com). -
#secure ⇒ Boolean
(also: #secure?)
truefor https connections,falsefor http connections. -
#sync_ignore_path_regex ⇒ Regex
Format ignore hook middleware sync.
-
#sync_interval ⇒ Object
Sync interval for Rack Middleware.
-
#sync_interval_staging ⇒ Integer
The time, in seconds, in between each sync to the server in development.
-
#test_environments ⇒ Array<String>
A list of environments in which the server should not be contacted.
-
#upload_disabled_environments ⇒ Array<String>
A list of environments in which the server should not be upload.
Class Method Summary collapse
-
.builtin_local_first_key?(key_without_locale) ⇒ Boolean
lookup 経路(Configuration#local_first_key?)と upload 抑止経路(Cache#[]=)で同じ組み込み判定を 共有する。判定を 1 箇所に集約することで number 以外を足す際の同期漏れを防ぐ。.
Instance Method Summary collapse
-
#[](option) ⇒ Object
Allows config options to be read like a hash.
-
#applied? ⇒ Boolean
Determines if the configuration has been applied (internal).
-
#apply ⇒ Object
Applies the configuration (internal).
-
#development? ⇒ Boolean
Determines if the content will be editable.
- #enable_middleware? ⇒ Boolean
-
#environment_info ⇒ String
For logging/debugging (internal).
-
#initialize ⇒ Configuration
constructor
Instantiated from configure.
-
#local_first_key?(key_without_locale) ⇒ Boolean
locale を除いたキーが local_first_key_regexp にマッチするかを返す。 マッチするキーはローカル config/locales(CopyTuner 管理外)で管理されるため、 オーバーレイマーカー注入やキャッシュ参照をスキップする必要がある。.
-
#merge(hash) ⇒ Hash
Returns a hash of all configurable options merged with
hash. -
#project_url ⇒ String
Current project url by project_id.
-
#protocol ⇒ String
The protocol that should be used when generating URLs to CopyTuner.
-
#public? ⇒ Boolean
Determines if the published or draft content will be used environment,
trueotherwise. -
#test? ⇒ Boolean
Determines if the content will fetched from the server.
-
#to_hash ⇒ Hash
Returns a hash of all configurable options.
- #upload_disabled? ⇒ Boolean
Constructor Details
#initialize ⇒ Configuration
Instantiated from CopyTunerClient.configure. Sets defaults.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/copy_tuner_client/configuration.rb', line 165 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') self.middleware_position = default_middleware_position @applied = false end |
Instance Attribute Details
#api_key ⇒ String
Returns The API key for your project, found on the project edit form.
42 43 44 |
# File 'lib/copy_tuner_client/configuration.rb', line 42 def api_key @api_key end |
#ca_file ⇒ String
Returns the path to a root certificate file used to verify ssl sessions. Default's to the root certificate file for copy-tuner.com.
125 126 127 |
# File 'lib/copy_tuner_client/configuration.rb', line 125 def ca_file @ca_file end |
#cache ⇒ Cache
Returns instance used internally to synchronize changes.
128 129 130 |
# File 'lib/copy_tuner_client/configuration.rb', line 128 def cache @cache end |
#client ⇒ Client
Returns instance used to communicate with a CopyTuner Server.
131 132 133 |
# File 'lib/copy_tuner_client/configuration.rb', line 131 def client @client end |
#client_name ⇒ String
Returns The name of the client library being used to send notifications (defaults to CopyTuner Client).
85 86 87 |
# File 'lib/copy_tuner_client/configuration.rb', line 85 def client_name @client_name end |
#client_url ⇒ String
Returns The url of the client library being used.
94 95 96 |
# File 'lib/copy_tuner_client/configuration.rb', line 94 def client_url @client_url end |
#client_version ⇒ String
Returns The version of the client library being used to send notifications (such as 1.0.2).
91 92 93 |
# File 'lib/copy_tuner_client/configuration.rb', line 91 def client_version @client_version end |
#development_environments ⇒ Array<String>
Returns A list of environments in which content should be editable.
73 74 75 |
# File 'lib/copy_tuner_client/configuration.rb', line 73 def development_environments @development_environments end |
#disable_copyray_comment_injection ⇒ Boolean
Returns To disable Copyray marker injection, set true.
145 146 147 |
# File 'lib/copy_tuner_client/configuration.rb', line 145 def disable_copyray_comment_injection @disable_copyray_comment_injection end |
#disable_middleware ⇒ Boolean
Returns disable middleware setting.
116 117 118 |
# File 'lib/copy_tuner_client/configuration.rb', line 116 def disable_middleware @disable_middleware end |
#disable_test_translation ⇒ Boolean
Returns disable download translation for test enviroment.
122 123 124 |
# File 'lib/copy_tuner_client/configuration.rb', line 122 def disable_test_translation @disable_test_translation end |
#download_cache_dir ⇒ Pathname
Returns The directory to cache downloaded files.
160 161 162 |
# File 'lib/copy_tuner_client/configuration.rb', line 160 def download_cache_dir @download_cache_dir end |
#environment_name ⇒ String
Returns The name of the environment the application is running in.
82 83 84 |
# File 'lib/copy_tuner_client/configuration.rb', line 82 def environment_name @environment_name end |
#framework ⇒ String, NilClass
Returns The framework notifications are being sent from, if any (such as Rails 2.3.9).
88 89 90 |
# File 'lib/copy_tuner_client/configuration.rb', line 88 def framework @framework end |
#host ⇒ String
Returns The host to connect to (defaults to copy-tuner.com).
45 46 47 |
# File 'lib/copy_tuner_client/configuration.rb', line 45 def host @host end |
#http_open_timeout ⇒ Fixnum
Returns The HTTP open timeout in seconds (defaults to 2).
55 56 57 |
# File 'lib/copy_tuner_client/configuration.rb', line 55 def http_open_timeout @http_open_timeout end |
#http_read_timeout ⇒ Fixnum
Returns The HTTP read timeout in seconds (defaults to 5).
58 59 60 |
# File 'lib/copy_tuner_client/configuration.rb', line 58 def http_read_timeout @http_read_timeout end |
#ignored_key_handler ⇒ Proc
154 155 156 |
# File 'lib/copy_tuner_client/configuration.rb', line 154 def ignored_key_handler @ignored_key_handler end |
#ignored_keys ⇒ Array<String>
Returns A list of ignored keys.
151 152 153 |
# File 'lib/copy_tuner_client/configuration.rb', line 151 def ignored_keys @ignored_keys end |
#local_first_key_regexp ⇒ Regexp
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.
139 140 141 |
# File 'lib/copy_tuner_client/configuration.rb', line 139 def local_first_key_regexp @local_first_key_regexp end |
#locales ⇒ Array<Symbol>
Returns Restrict blurb locales to upload.
148 149 150 |
# File 'lib/copy_tuner_client/configuration.rb', line 148 def locales @locales end |
#logger ⇒ Logger
Returns Where to log messages. Must respond to same interface as Logger.
110 111 112 |
# File 'lib/copy_tuner_client/configuration.rb', line 110 def logger @logger end |
#middleware ⇒ Object
Returns the middleware stack, if any, which should respond to use.
113 114 115 |
# File 'lib/copy_tuner_client/configuration.rb', line 113 def middleware @middleware end |
#middleware_position ⇒ Object
OtherMiddleware or OtherMiddleware
119 120 121 |
# File 'lib/copy_tuner_client/configuration.rb', line 119 def middleware_position @middleware_position end |
#poller ⇒ Object
Returns the value of attribute poller.
133 134 135 |
# File 'lib/copy_tuner_client/configuration.rb', line 133 def poller @poller end |
#polling_delay ⇒ Integer
Returns The time, in seconds, in between each sync to the server. Defaults to 300.
97 98 99 |
# File 'lib/copy_tuner_client/configuration.rb', line 97 def polling_delay @polling_delay end |
#port ⇒ Object
283 284 285 |
# File 'lib/copy_tuner_client/configuration.rb', line 283 def port @port || default_port end |
#project_id ⇒ Integer
Returns The project id.
157 158 159 |
# File 'lib/copy_tuner_client/configuration.rb', line 157 def project_id @project_id end |
#proxy_host ⇒ String, NilClass
Returns The hostname of your proxy server (if using a proxy).
61 62 63 |
# File 'lib/copy_tuner_client/configuration.rb', line 61 def proxy_host @proxy_host end |
#proxy_pass ⇒ String, NilClass
Returns The password to use when logging into your proxy server (if using a proxy).
70 71 72 |
# File 'lib/copy_tuner_client/configuration.rb', line 70 def proxy_pass @proxy_pass end |
#proxy_port ⇒ String, Fixnum
Returns The port of your proxy server (if using a proxy).
64 65 66 |
# File 'lib/copy_tuner_client/configuration.rb', line 64 def proxy_port @proxy_port end |
#proxy_user ⇒ String, NilClass
Returns The username to use when logging into your proxy server (if using a proxy).
67 68 69 |
# File 'lib/copy_tuner_client/configuration.rb', line 67 def proxy_user @proxy_user end |
#s3_host ⇒ String
Returns The S3 host to connect to (defaults to copy-tuner-us.s3.amazonaws.com).
142 143 144 |
# File 'lib/copy_tuner_client/configuration.rb', line 142 def s3_host @s3_host end |
#secure ⇒ Boolean Also known as: secure?
Returns true for https connections, false for http connections.
52 53 54 |
# File 'lib/copy_tuner_client/configuration.rb', line 52 def secure @secure end |
#sync_ignore_path_regex ⇒ Regex
Returns Format ignore hook middleware sync.
107 108 109 |
# File 'lib/copy_tuner_client/configuration.rb', line 107 def sync_ignore_path_regex @sync_ignore_path_regex end |
#sync_interval ⇒ Object
Sync interval for Rack Middleware
318 319 320 321 322 323 324 |
# File 'lib/copy_tuner_client/configuration.rb', line 318 def sync_interval if environment_name == 'staging' @sync_interval_staging else @sync_interval end end |
#sync_interval_staging ⇒ Integer
Returns The time, in seconds, in between each sync to the server in development. Defaults to 60.
104 105 106 |
# File 'lib/copy_tuner_client/configuration.rb', line 104 def sync_interval_staging @sync_interval_staging end |
#test_environments ⇒ Array<String>
Returns A list of environments in which the server should not be contacted.
76 77 78 |
# File 'lib/copy_tuner_client/configuration.rb', line 76 def test_environments @test_environments end |
#upload_disabled_environments ⇒ Array<String>
Returns A list of environments in which the server should not be upload.
79 80 81 |
# File 'lib/copy_tuner_client/configuration.rb', line 79 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 以外を足す際の同期漏れを防ぐ。
37 38 39 |
# File 'lib/copy_tuner_client/configuration.rb', line 37 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
196 197 198 |
# File 'lib/copy_tuner_client/configuration.rb', line 196 def [](option) public_send(option) end |
#applied? ⇒ Boolean
Determines if the configuration has been applied (internal)
247 248 249 |
# File 'lib/copy_tuner_client/configuration.rb', line 247 def applied? @applied end |
#apply ⇒ Object
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.
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/copy_tuner_client/configuration.rb', line 258 def apply # rubocop:disable Metrics/AbcSize # NOTE: project_id は必須。未設定なら apply 時点で明示的に失敗させる validate_project_id! self.locales ||= default_locales 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) setup_middleware @applied = true log_applied process_guard.start unless test? unless test? && disable_test_translation logger.info 'Download translation now' cache.download end end |
#development? ⇒ Boolean
Determines if the content will be editable
227 228 229 |
# File 'lib/copy_tuner_client/configuration.rb', line 227 def development? development_environments.include?(environment_name) end |
#enable_middleware? ⇒ Boolean
231 232 233 |
# File 'lib/copy_tuner_client/configuration.rb', line 231 def enable_middleware? middleware && development? && !disable_middleware end |
#environment_info ⇒ String
For logging/debugging (internal).
299 300 301 302 |
# File 'lib/copy_tuner_client/configuration.rb', line 299 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 管理外)で管理されるため、 オーバーレイマーカー注入やキャッシュ参照をスキップする必要がある。
340 341 342 343 344 345 346 347 348 |
# File 'lib/copy_tuner_client/configuration.rb', line 340 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
214 215 216 |
# File 'lib/copy_tuner_client/configuration.rb', line 214 def merge(hash) to_hash.merge(hash) end |
#project_url ⇒ String
Returns current project url by project_id.
327 328 329 330 331 332 |
# File 'lib/copy_tuner_client/configuration.rb', line 327 def project_url validate_project_id! path = "/projects/#{project_id}" URI::Generic.build(scheme: protocol, host:, port: port.to_i, path:).to_s end |
#protocol ⇒ String
The protocol that should be used when generating URLs to CopyTuner.
289 290 291 292 293 294 295 |
# File 'lib/copy_tuner_client/configuration.rb', line 289 def protocol if secure? 'https' else 'http' end end |
#public? ⇒ Boolean
Determines if the published or draft content will be used
environment, true otherwise.
221 222 223 |
# File 'lib/copy_tuner_client/configuration.rb', line 221 def public? !(development_environments + test_environments).include?(environment_name) end |
#test? ⇒ Boolean
Determines if the content will fetched from the server
237 238 239 |
# File 'lib/copy_tuner_client/configuration.rb', line 237 def test? test_environments.include?(environment_name) end |
#to_hash ⇒ Hash
Returns a hash of all configurable options
202 203 204 205 206 207 208 |
# File 'lib/copy_tuner_client/configuration.rb', line 202 def to_hash = { public: public?, upload_disabled: upload_disabled? } OPTIONS.inject() do |hash, option| hash.merge(option.to_sym => public_send(option)) end end |
#upload_disabled? ⇒ Boolean
241 242 243 |
# File 'lib/copy_tuner_client/configuration.rb', line 241 def upload_disabled? upload_disabled_environments.include?(environment_name) end |