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 exclude_key_regexp s3_host locales ignored_keys ignored_key_handler].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Instantiated from CopyTunerClient.configure. Sets defaults.



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/copy_tuner_client/configuration.rb', line 142

def initialize
  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
  # TODO: 0.18.0以降のバージョンで初期値をtrueにしたい
  self.html_escape = nil
  self.ignored_keys = []
  self.ignored_key_handler = ->(e) { raise e }

  @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.



24
25
26
# File 'lib/copy_tuner_client/configuration.rb', line 24

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.



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

def ca_file
  @ca_file
end

#cacheCache

Returns instance used internally to synchronize changes.

Returns:

  • (Cache)

    instance used internally to synchronize changes.



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

def cache
  @cache
end

#clientClient

Returns instance used to communicate with a CopyTuner Server.

Returns:

  • (Client)

    instance used to communicate with a CopyTuner Server.



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

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)



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

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



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

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)



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

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



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

def development_environments
  @development_environments
end

#disable_copyray_comment_injectionBoolean

Returns To disable Copyray comment injection, set true.

Returns:

  • (Boolean)

    To disable Copyray comment injection, set true



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

def disable_copyray_comment_injection
  @disable_copyray_comment_injection
end

#disable_middlewareBoolean

Returns disable middleware setting.

Returns:

  • (Boolean)

    disable middleware setting



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

def disable_middleware
  @disable_middleware
end

#disable_test_translationBoolean

Returns disable download translation for test enviroment.

Returns:

  • (Boolean)

    disable download translation for test enviroment



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

def disable_test_translation
  @disable_test_translation
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



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

def environment_name
  @environment_name
end

#exclude_key_regexpRegexp

Returns Regular expression to exclude keys.

Returns:

  • (Regexp)

    Regular expression to exclude keys.



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

def exclude_key_regexp
  @exclude_key_regexp
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)



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

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).



27
28
29
# File 'lib/copy_tuner_client/configuration.rb', line 27

def host
  @host
end

#html_escapeBoolean

Returns Html escape.

Returns:

  • (Boolean)

    Html escape



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

def html_escape
  @html_escape
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).



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

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).



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

def http_read_timeout
  @http_read_timeout
end

#ignored_key_handlerProc

Returns:

  • (Proc)


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

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



134
135
136
# File 'lib/copy_tuner_client/configuration.rb', line 134

def ignored_keys
  @ignored_keys
end

#inline_translationBoolean

Returns To enable inline-translation-mode, set true.

Returns:

  • (Boolean)

    To enable inline-translation-mode, set true.



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

def inline_translation
  @inline_translation
end

#localesArray<Symbol>

Returns Restrict blurb locales to upload.

Returns:

  • (Array<Symbol>)

    Restrict blurb locales to upload



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

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.



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

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



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

def middleware
  @middleware
end

#middleware_positionObject

OtherMiddleware or OtherMiddleware



99
100
101
# File 'lib/copy_tuner_client/configuration.rb', line 99

def middleware_position
  @middleware_position
end

#pollerObject

Returns the value of attribute poller.



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

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.



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

def polling_delay
  @polling_delay
end

#portFixnum

Returns The port on which your CopyTuner server runs (defaults to 443 for secure connections, 80 for insecure connections).

Returns:

  • (Fixnum)

    The port on which your CopyTuner server runs (defaults to 443 for secure connections, 80 for insecure connections).



30
31
32
# File 'lib/copy_tuner_client/configuration.rb', line 30

def port
  @port
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)



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

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)



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

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)



45
46
47
# File 'lib/copy_tuner_client/configuration.rb', line 45

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)



48
49
50
# File 'lib/copy_tuner_client/configuration.rb', line 48

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).



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

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.



33
34
35
# File 'lib/copy_tuner_client/configuration.rb', line 33

def secure
  @secure
end

#sync_ignore_path_regexRegex

Returns Format ignore hook middleware sync.

Returns:

  • (Regex)

    Format ignore hook middleware sync



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

def sync_ignore_path_regex
  @sync_ignore_path_regex
end

#sync_intervalObject

Sync interval for Rack Middleware



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

def sync_interval
  @sync_interval
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.



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

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



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

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



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

def upload_disabled_environments
  @upload_disabled_environments
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



177
178
179
# File 'lib/copy_tuner_client/configuration.rb', line 177

def [](option)
  send(option)
end

#applied?Boolean

Determines if the configuration has been applied (internal)

Returns:

  • (Boolean)

    Returns true if applied, false otherwise.



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

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.



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
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
# File 'lib/copy_tuner_client/configuration.rb', line 239

def apply
  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.



208
209
210
# File 'lib/copy_tuner_client/configuration.rb', line 208

def development?
  development_environments.include? environment_name
end

#enable_middleware?Boolean

Returns:

  • (Boolean)


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

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.



300
301
302
303
# File 'lib/copy_tuner_client/configuration.rb', line 300

def environment_info
  parts = ["Ruby: #{RUBY_VERSION}", framework, "Env: #{environment_name}"]
  parts.compact.map { |part| "[#{part}]" }.join(' ')
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



195
196
197
# File 'lib/copy_tuner_client/configuration.rb', line 195

def merge(hash)
  to_hash.merge hash
end

#project_urlString

Returns current project url by api_key.

Returns:

  • (String)

    current project url by api_key



322
323
324
# File 'lib/copy_tuner_client/configuration.rb', line 322

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

#protocolString

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

Returns:

  • (String)

    https if #secure? returns true, http otherwise.



290
291
292
293
294
295
296
# File 'lib/copy_tuner_client/configuration.rb', line 290

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



202
203
204
# File 'lib/copy_tuner_client/configuration.rb', line 202

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.



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

def test?
  test_environments.include?(environment_name)
end

#to_hashHash

Returns a hash of all configurable options

Returns:

  • (Hash)

    configuration attributes



183
184
185
186
187
188
189
# File 'lib/copy_tuner_client/configuration.rb', line 183

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

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

#upload_disabled?Boolean

Returns:

  • (Boolean)


222
223
224
# File 'lib/copy_tuner_client/configuration.rb', line 222

def upload_disabled?
  upload_disabled_environments.include?(environment_name)
end