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.

[: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].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Instantiated from CopyTunerClient.configure. Sets defaults.



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/copy_tuner_client/configuration.rb', line 140

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.s3_host = 'copy-tuner-data-prod.s3.amazonaws.com'
  self.disable_copyray_comment_injection = false

  @applied = false
end

Instance Attribute Details

#api_keyString



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

def api_key
  @api_key
end

#ca_fileString



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

def ca_file
  @ca_file
end

#cacheCache



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

def cache
  @cache
end

#clientClient



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

def client
  @client
end

#client_nameString



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

def client_name
  @client_name
end

#client_urlString



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

def client_url
  @client_url
end

#client_versionString



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

def client_version
  @client_version
end

#copyray_js_injection_regexp_for_debugRegexp



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

def copyray_js_injection_regexp_for_debug
  @copyray_js_injection_regexp_for_debug
end

#copyray_js_injection_regexp_for_precompiledRegexp



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

def copyray_js_injection_regexp_for_precompiled
  @copyray_js_injection_regexp_for_precompiled
end

#development_environmentsArray<String>



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

def development_environments
  @development_environments
end

#disable_copyray_comment_injectionBoolean



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

def disable_copyray_comment_injection
  @disable_copyray_comment_injection
end

#disable_middlewareBoolean



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

def disable_middleware
  @disable_middleware
end

#disable_test_translationBoolean



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

def disable_test_translation
  @disable_test_translation
end

#environment_nameString



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

def environment_name
  @environment_name
end

#exclude_key_regexpRegexp



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

def exclude_key_regexp
  @exclude_key_regexp
end

#frameworkString, NilClass



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

def framework
  @framework
end

#hostString



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

def host
  @host
end

#http_open_timeoutFixnum



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

def http_open_timeout
  @http_open_timeout
end

#http_read_timeoutFixnum



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

def http_read_timeout
  @http_read_timeout
end

#inline_translationBoolean



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

def inline_translation
  @inline_translation
end

#localesArray<Symbol>



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

def locales
  @locales
end

#loggerLogger



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

def logger
  @logger
end

#middlewareObject



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

def middleware
  @middleware
end

#polling_delayInteger



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

def polling_delay
  @polling_delay
end

#portFixnum



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

def port
  @port
end

#proxy_hostString, NilClass



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

def proxy_host
  @proxy_host
end

#proxy_passString, NilClass



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

def proxy_pass
  @proxy_pass
end

#proxy_portString, Fixnum



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

def proxy_port
  @proxy_port
end

#proxy_userString, NilClass



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

def proxy_user
  @proxy_user
end

#s3_hostString



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

def s3_host
  @s3_host
end

#secureBoolean Also known as: secure?



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

def secure
  @secure
end

#sync_ignore_path_regexRegex



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

def sync_ignore_path_regex
  @sync_ignore_path_regex
end

#sync_intervalObject

Sync interval for Rack Middleware



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

def sync_interval
  @sync_interval
end

#sync_interval_stagingInteger



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

def sync_interval_staging
  @sync_interval_staging
end

#test_environmentsArray<String>



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

def test_environments
  @test_environments
end

Instance Method Details

#[](option) ⇒ Object

Allows config options to be read like a hash



164
165
166
# File 'lib/copy_tuner_client/configuration.rb', line 164

def [](option)
  send(option)
end

#applied?Boolean

Determines if the configuration has been applied (internal)



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

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.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/copy_tuner_client/configuration.rb', line 222

def apply
  self.locales ||= begin
    if defined?(::Rails)
      self.locales = ::Rails.application.config.i18n.available_locales.presence || Array(::Rails.application.config.i18n.default_locale)
    else
      self.locales = [:en]
    end
  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"
    middleware.use RequestSync, :cache => cache, :interval => sync_interval, :ignore_regex => sync_ignore_path_regex
    middleware.use CopyTunerClient::CopyrayMiddleware
  else
    logger.info "[[[Warn]]] Not useing 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

  if test? and !disable_test_translation
    logger.info "Download translation now"
    cache.download
  end
end

#development?Boolean

Determines if the content will be editable



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

def development?
  development_environments.include? environment_name
end

#enable_middleware?Boolean



199
200
201
# File 'lib/copy_tuner_client/configuration.rb', line 199

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

#environment_infoString

For logging/debugging (internal).



276
277
278
279
# File 'lib/copy_tuner_client/configuration.rb', line 276

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



182
183
184
# File 'lib/copy_tuner_client/configuration.rb', line 182

def merge(hash)
  to_hash.merge hash
end

#project_urlString



298
299
300
# File 'lib/copy_tuner_client/configuration.rb', line 298

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.



266
267
268
269
270
271
272
# File 'lib/copy_tuner_client/configuration.rb', line 266

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

#public?Boolean

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



189
190
191
# File 'lib/copy_tuner_client/configuration.rb', line 189

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

#test?Boolean

Determines if the content will fetched from the server



205
206
207
# File 'lib/copy_tuner_client/configuration.rb', line 205

def test?
  test_environments.include? environment_name
end

#to_hashHash

Returns a hash of all configurable options



170
171
172
173
174
175
176
# File 'lib/copy_tuner_client/configuration.rb', line 170

def to_hash
  base_options = { :public => public? }

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