Class: Wovnrb::Store

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wovnrb/store.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStore

Returns a new instance of Store.



49
50
51
# File 'lib/wovnrb/store.rb', line 49

def initialize
  reset
end

Class Method Details

.default_settingsObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/wovnrb/store.rb', line 16

def self.default_settings
  Settings.new.merge(
    'project_token' => '',
    'log_path' => 'log/wovn_error.log',
    'ignore_paths' => [],
    'ignore_globs' => [],
    'url_pattern' => 'path',
    'url_pattern_reg' => "/(?<lang>[^/.?]+)",
    'lang_param_name' => 'wovn',
    'query' => [],
    'ignore_class' => [],
    'api_url' => 'https://wovn.global.ssl.fastly.net',
    'api_timeout_seconds' => 1.0,
    'api_timeout_search_engine_bots' => 5.0,
    'default_lang' => 'ja',
    'supported_langs' => %w[en ja],
    'test_mode' => false,
    'test_url' => '',
    'cache_megabytes' => nil,
    'ttl_seconds' => nil,
    'use_proxy' => false, # use env['HTTP_X_FORWARDED_HOST'] instead of env['HTTP_HOST'] and env['SERVER_NAME'] when this setting is true.
    'custom_lang_aliases' => {},
    'translate_fragment' => true,
    'widget_url' => 'https://j.wovn.io/1',
    'wovn_dev_mode' => false,
    'compress_api_requests' => true,
    'translate_canonical_tag' => true,
    'custom_domain_langs' => {},
    'insert_hreflangs' => true,
    'use_cookie_lang' => false
  )
end

Instance Method Details

#compress_api_requests?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/wovnrb/store.rb', line 194

def compress_api_requests?
  @settings['compress_api_requests']
end

#custom_domain_langsObject



210
211
212
# File 'lib/wovnrb/store.rb', line 210

def custom_domain_langs
  @custom_domain_langs ||= CustomDomainLangs.new(@settings['custom_domain_langs'])
end

#custom_lang_aliasesObject



177
178
179
# File 'lib/wovnrb/store.rb', line 177

def custom_lang_aliases
  @settings['custom_lang_aliases'] || {}
end

#default_langObject



181
182
183
# File 'lib/wovnrb/store.rb', line 181

def default_lang
  @settings['default_lang']
end

#default_lang_aliasObject



185
186
187
188
# File 'lib/wovnrb/store.rb', line 185

def default_lang_alias
  custom_alias = custom_lang_aliases[default_lang]
  custom_alias || default_lang
end

#dev_mode?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/wovnrb/store.rb', line 202

def dev_mode?
  @settings['wovn_dev_mode']
end

#format_settingsObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/wovnrb/store.rb', line 138

def format_settings
  if @settings.has_key?('custom_lang_aliases')
    stringify_keys! @settings['custom_lang_aliases']
  end

  @settings['default_lang'] = Lang.get_code(@settings['default_lang'])
  if !@settings.has_key?('supported_langs')
    @settings['supported_langs'] = [@settings['default_lang']]
  end

  if @settings.has_key?('user_token') && @settings['project_token'].empty?
    @settings['project_token'] = @settings['user_token']
  end
  @settings.delete('user_token')

  case @settings['url_pattern']
  when 'path'
    @settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
  when 'query'
    @settings['url_pattern_reg'] = "((\\?.*&)|\\?)#{@settings['lang_param_name']}=(?<lang>[^&]+)(&|$)"
  when 'subdomain'
    @settings['url_pattern_reg'] = '^(?<lang>[^.]+)\.'
  when 'custom_domain'
    # Do not use regex
  end

  @settings['test_mode'] = !(@settings['test_mode'] != true || @settings['test_mode'] != 'on')

  if @settings['wovn_dev_mode']
    if @settings['api_url'] == self.class.default_settings['api_url']
      @settings['api_url'] = 'http://dev-wovn.io:3001'
    end

    if @settings['api_timeout_seconds'] == self.class.default_settings['api_timeout_seconds']
      @settings['api_timeout_seconds'] = 3.0
    end
  end
end

#load_settingsObject

Load Rails config.wovnrb



123
124
125
126
127
128
# File 'lib/wovnrb/store.rb', line 123

def load_settings
  if Object.const_defined?(:Rails) && Rails.configuration.respond_to?(:wovnrb)
    @config_loaded = true
    update_settings(Rails.configuration.wovnrb)
  end
end

#resetnil

Reset @settings and @config_loaded variables to default.

Returns:

  • (nil)


56
57
58
59
60
61
# File 'lib/wovnrb/store.rb', line 56

def reset
  @settings = Store.default_settings
  @custom_domain_langs = nil
  # When Store is initialized, the Rails.configuration object is not yet initialized
  @config_loaded = false
end

#settingsObject

Returns the settings object, pulling from Rails config the first time this is called



116
117
118
119
# File 'lib/wovnrb/store.rb', line 116

def settings
  load_settings unless @config_loaded
  @settings
end

#supported_langsObject



190
191
192
# File 'lib/wovnrb/store.rb', line 190

def supported_langs
  @settings['supported_langs'] || []
end

#update_settings(new_settings) ⇒ Object



130
131
132
133
134
135
136
# File 'lib/wovnrb/store.rb', line 130

def update_settings(new_settings)
  load_settings unless @config_loaded
  if !new_settings.nil?
    @settings.merge!(new_settings.stringify_keys)
    format_settings
  end
end

#url_patternObject



206
207
208
# File 'lib/wovnrb/store.rb', line 206

def url_pattern
  @settings['url_pattern']
end

#valid_settings?Boolean

Returns true or false based on whether the settings are valid or not, logs any invalid settings to ../error.log

Returns:

  • (Boolean)

    Returns true if the settings are valid, and false if they are not



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/wovnrb/store.rb', line 73

def valid_settings?
  valid = true
  errors = []
  # if valid_token?(!settings.has_key?('project_token') || settings['project_token'].length < 5 || settings['project_token'].length > 6
  if !valid_token?(settings['project_token'])
    errors.push("Project token #{settings['project_token']} is not valid.")
  end
  if settings.has_key?('ignore_paths') && !settings['ignore_paths'].kind_of?(Array)
    errors.push("Ignore Paths #{settings['ignore_paths']} should be Array.")
  end
  if !settings.has_key?('url_pattern') || settings['url_pattern'].length == 0
    errors.push("Url pattern #{settings['url_pattern']} is not valid.")
  end
  if !settings.has_key?('query') || !settings['query'].kind_of?(Array)
    errors.push("query config #{settings['query']} is not valid.")
  end
  if !settings.has_key?('ignore_class') || !settings['ignore_class'].kind_of?(Array)
    errors.push("ignore_class config #{settings['ignore_class']} should be Array.")
  end
  if !settings.has_key?('api_url') || settings['api_url'].length == 0
    errors.push("API URL is not configured.")
  end
  if !settings.has_key?('default_lang') || settings['default_lang'].nil?
    errors.push("Default lang #{settings['default_lang']} is not valid.")
  end
  if !settings.has_key?('supported_langs') || !settings['supported_langs'].kind_of?(Array) || settings['supported_langs'].size < 1
    errors.push("Supported langs configuration is not valid.")
  end
  if !settings.has_key?('custom_lang_aliases') || !settings['custom_lang_aliases'].kind_of?(Hash)
    errors.push("Custom lang aliases is not valid.")
  end
  # log errors
  if errors.length > 0
    valid = false
    errors.each do |e|
      WovnLogger.instance.error(e)
    end
  end
  valid
end

#valid_token?(token) ⇒ Boolean

Returns true or false based on whether the token is valid or not

Returns:

  • (Boolean)

    Returns true if the token is valid, and false if it is not



66
67
68
# File 'lib/wovnrb/store.rb', line 66

def valid_token?(token)
  !token.nil? && (token.length == 5 || token.length == 6)
end

#widget_urlObject



198
199
200
# File 'lib/wovnrb/store.rb', line 198

def widget_url
  @settings['widget_url'] || 'https://j.wovn.io/1'
end