wovnrb 3.9.0 → 3.10.1

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wovnrb/store.rb CHANGED
@@ -1,220 +1,221 @@
1
- require 'net/http'
2
- require 'uri'
3
- require 'cgi'
4
- require 'singleton'
5
- require 'wovnrb/custom_domain/custom_domain_langs'
6
- require 'wovnrb/services/wovn_logger'
7
- require 'wovnrb/services/glob'
8
- require 'wovnrb/settings'
9
- require 'active_support'
10
- require 'active_support/core_ext'
11
-
12
- module Wovnrb
13
- class Store
14
- include Singleton
15
-
16
- def self.default_settings
17
- Settings.new.merge(
18
- 'project_token' => '',
19
- 'log_path' => 'log/wovn_error.log',
20
- 'ignore_paths' => [],
21
- 'ignore_globs' => [],
22
- 'url_pattern' => 'path',
23
- 'url_pattern_reg' => "/(?<lang>[^/.?]+)",
24
- 'lang_param_name' => 'wovn',
25
- 'query' => [],
26
- 'ignore_class' => [],
27
- 'api_url' => 'https://wovn.global.ssl.fastly.net',
28
- 'api_timeout_seconds' => 1.0,
29
- 'api_timeout_search_engine_bots' => 5.0,
30
- 'default_lang' => 'ja',
31
- 'supported_langs' => %w[en ja],
32
- 'test_mode' => false,
33
- 'test_url' => '',
34
- 'cache_megabytes' => nil,
35
- 'ttl_seconds' => nil,
36
- 'use_proxy' => false, # use env['HTTP_X_FORWARDED_HOST'] instead of env['HTTP_HOST'] and env['SERVER_NAME'] when this setting is true.
37
- 'custom_lang_aliases' => {},
38
- 'translate_fragment' => true,
39
- 'widget_url' => 'https://j.wovn.io/1',
40
- 'wovn_dev_mode' => false,
41
- 'compress_api_requests' => true,
42
- 'translate_canonical_tag' => true,
43
- 'custom_domain_langs' => {}
44
- )
45
- end
46
-
47
- def initialize
48
- reset
49
- end
50
-
51
- # Reset @settings and @config_loaded variables to default.
52
- #
53
- # @return [nil]
54
- def reset
55
- @settings = Store.default_settings
56
- @custom_domain_langs = nil
57
- # When Store is initialized, the Rails.configuration object is not yet initialized
58
- @config_loaded = false
59
- end
60
-
61
- # Returns true or false based on whether the token is valid or not
62
- #
63
- # @return [Boolean] Returns true if the token is valid, and false if it is not
64
- def valid_token?(token)
65
- !token.nil? && (token.length == 5 || token.length == 6)
66
- end
67
-
68
- # Returns true or false based on whether the settings are valid or not, logs any invalid settings to ../error.log
69
- #
70
- # @return [Boolean] Returns true if the settings are valid, and false if they are not
71
- def valid_settings?
72
- valid = true
73
- errors = []
74
- # if valid_token?(!settings.has_key?('project_token') || settings['project_token'].length < 5 || settings['project_token'].length > 6
75
- if !valid_token?(settings['project_token'])
76
- errors.push("Project token #{settings['project_token']} is not valid.")
77
- end
78
- if settings.has_key?('ignore_paths') && !settings['ignore_paths'].kind_of?(Array)
79
- errors.push("Ignore Paths #{settings['ignore_paths']} should be Array.")
80
- end
81
- if !settings.has_key?('url_pattern') || settings['url_pattern'].length == 0
82
- errors.push("Url pattern #{settings['url_pattern']} is not valid.")
83
- end
84
- if !settings.has_key?('query') || !settings['query'].kind_of?(Array)
85
- errors.push("query config #{settings['query']} is not valid.")
86
- end
87
- if !settings.has_key?('ignore_class') || !settings['ignore_class'].kind_of?(Array)
88
- errors.push("ignore_class config #{settings['ignore_class']} should be Array.")
89
- end
90
- if !settings.has_key?('api_url') || settings['api_url'].length == 0
91
- errors.push("API URL is not configured.")
92
- end
93
- if !settings.has_key?('default_lang') || settings['default_lang'].nil?
94
- errors.push("Default lang #{settings['default_lang']} is not valid.")
95
- end
96
- if !settings.has_key?('supported_langs') || !settings['supported_langs'].kind_of?(Array) || settings['supported_langs'].size < 1
97
- errors.push("Supported langs configuration is not valid.")
98
- end
99
- if !settings.has_key?('custom_lang_aliases') || !settings['custom_lang_aliases'].kind_of?(Hash)
100
- errors.push("Custom lang aliases is not valid.")
101
- end
102
- # log errors
103
- if errors.length > 0
104
- valid = false
105
- errors.each do |e|
106
- WovnLogger.instance.error(e)
107
- end
108
- end
109
- valid
110
- end
111
-
112
- # Returns the settings object, pulling from Rails config the first time this is called
113
- #
114
- def settings
115
- load_settings unless @config_loaded
116
- @settings
117
- end
118
-
119
- # Load Rails config.wovnrb
120
- #
121
- def load_settings
122
- if Object.const_defined?(:Rails) && Rails.configuration.respond_to?(:wovnrb)
123
- @config_loaded = true
124
- update_settings(Rails.configuration.wovnrb)
125
- end
126
- end
127
-
128
- def update_settings(new_settings)
129
- load_settings unless @config_loaded
130
- if !new_settings.nil?
131
- @settings.merge!(new_settings.stringify_keys)
132
- format_settings
133
- end
134
- end
135
-
136
- def format_settings
137
- if @settings.has_key?('custom_lang_aliases')
138
- stringify_keys! @settings['custom_lang_aliases']
139
- end
140
-
141
- @settings['default_lang'] = Lang.get_code(@settings['default_lang'])
142
- if !@settings.has_key?('supported_langs')
143
- @settings['supported_langs'] = [@settings['default_lang']]
144
- end
145
-
146
- if @settings.has_key?('user_token') && @settings['project_token'].empty?
147
- @settings['project_token'] = @settings['user_token']
148
- end
149
- @settings.delete('user_token')
150
-
151
- case @settings['url_pattern']
152
- when 'path'
153
- @settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
154
- when 'query'
155
- @settings['url_pattern_reg'] = "((\\?.*&)|\\?)#{@settings['lang_param_name']}=(?<lang>[^&]+)(&|$)"
156
- when 'subdomain'
157
- @settings['url_pattern_reg'] = '^(?<lang>[^.]+)\.'
158
- when 'custom_domain'
159
- # Do not use regex
160
- end
161
-
162
- @settings['test_mode'] = !(@settings['test_mode'] != true || @settings['test_mode'] != 'on')
163
-
164
- if @settings['wovn_dev_mode']
165
- if @settings['api_url'] == self.class.default_settings['api_url']
166
- @settings['api_url'] = 'http://dev-wovn.io:3001'
167
- end
168
-
169
- if @settings['api_timeout_seconds'] == self.class.default_settings['api_timeout_seconds']
170
- @settings['api_timeout_seconds'] = 3.0
171
- end
172
- end
173
- end
174
-
175
- def custom_lang_aliases
176
- @settings['custom_lang_aliases'] || {}
177
- end
178
-
179
- def default_lang
180
- @settings['default_lang']
181
- end
182
-
183
- def default_lang_alias
184
- custom_alias = custom_lang_aliases[default_lang]
185
- custom_alias || default_lang
186
- end
187
-
188
- def supported_langs
189
- @settings['supported_langs'] || []
190
- end
191
-
192
- def compress_api_requests?
193
- @settings['compress_api_requests']
194
- end
195
-
196
- def widget_url
197
- @settings['widget_url'] || 'https://j.wovn.io/1'
198
- end
199
-
200
- def dev_mode?
201
- @settings['wovn_dev_mode']
202
- end
203
-
204
- def url_pattern
205
- @settings['url_pattern']
206
- end
207
-
208
- def custom_domain_langs
209
- @custom_domain_langs ||= CustomDomainLangs.new(@settings['custom_domain_langs'])
210
- end
211
-
212
- private
213
-
214
- def stringify_keys!(hash)
215
- hash.keys.each do |k|
216
- hash[k.to_s] = hash.delete(k)
217
- end
218
- end
219
- end
220
- end
1
+ require 'net/http'
2
+ require 'uri'
3
+ require 'cgi'
4
+ require 'singleton'
5
+ require 'wovnrb/custom_domain/custom_domain_langs'
6
+ require 'wovnrb/services/wovn_logger'
7
+ require 'wovnrb/services/glob'
8
+ require 'wovnrb/settings'
9
+ require 'active_support'
10
+ require 'active_support/core_ext'
11
+
12
+ module Wovnrb
13
+ class Store
14
+ include Singleton
15
+
16
+ def self.default_settings
17
+ Settings.new.merge(
18
+ 'project_token' => '',
19
+ 'log_path' => 'log/wovn_error.log',
20
+ 'ignore_paths' => [],
21
+ 'ignore_globs' => [],
22
+ 'url_pattern' => 'path',
23
+ 'url_pattern_reg' => "/(?<lang>[^/.?]+)",
24
+ 'lang_param_name' => 'wovn',
25
+ 'query' => [],
26
+ 'ignore_class' => [],
27
+ 'api_url' => 'https://wovn.global.ssl.fastly.net',
28
+ 'api_timeout_seconds' => 1.0,
29
+ 'api_timeout_search_engine_bots' => 5.0,
30
+ 'default_lang' => 'ja',
31
+ 'supported_langs' => %w[en ja],
32
+ 'test_mode' => false,
33
+ 'test_url' => '',
34
+ 'cache_megabytes' => nil,
35
+ 'ttl_seconds' => nil,
36
+ 'use_proxy' => false, # use env['HTTP_X_FORWARDED_HOST'] instead of env['HTTP_HOST'] and env['SERVER_NAME'] when this setting is true.
37
+ 'custom_lang_aliases' => {},
38
+ 'translate_fragment' => true,
39
+ 'widget_url' => 'https://j.wovn.io/1',
40
+ 'wovn_dev_mode' => false,
41
+ 'compress_api_requests' => true,
42
+ 'translate_canonical_tag' => true,
43
+ 'custom_domain_langs' => {},
44
+ 'insert_hreflangs' => true
45
+ )
46
+ end
47
+
48
+ def initialize
49
+ reset
50
+ end
51
+
52
+ # Reset @settings and @config_loaded variables to default.
53
+ #
54
+ # @return [nil]
55
+ def reset
56
+ @settings = Store.default_settings
57
+ @custom_domain_langs = nil
58
+ # When Store is initialized, the Rails.configuration object is not yet initialized
59
+ @config_loaded = false
60
+ end
61
+
62
+ # Returns true or false based on whether the token is valid or not
63
+ #
64
+ # @return [Boolean] Returns true if the token is valid, and false if it is not
65
+ def valid_token?(token)
66
+ !token.nil? && (token.length == 5 || token.length == 6)
67
+ end
68
+
69
+ # Returns true or false based on whether the settings are valid or not, logs any invalid settings to ../error.log
70
+ #
71
+ # @return [Boolean] Returns true if the settings are valid, and false if they are not
72
+ def valid_settings?
73
+ valid = true
74
+ errors = []
75
+ # if valid_token?(!settings.has_key?('project_token') || settings['project_token'].length < 5 || settings['project_token'].length > 6
76
+ if !valid_token?(settings['project_token'])
77
+ errors.push("Project token #{settings['project_token']} is not valid.")
78
+ end
79
+ if settings.has_key?('ignore_paths') && !settings['ignore_paths'].kind_of?(Array)
80
+ errors.push("Ignore Paths #{settings['ignore_paths']} should be Array.")
81
+ end
82
+ if !settings.has_key?('url_pattern') || settings['url_pattern'].length == 0
83
+ errors.push("Url pattern #{settings['url_pattern']} is not valid.")
84
+ end
85
+ if !settings.has_key?('query') || !settings['query'].kind_of?(Array)
86
+ errors.push("query config #{settings['query']} is not valid.")
87
+ end
88
+ if !settings.has_key?('ignore_class') || !settings['ignore_class'].kind_of?(Array)
89
+ errors.push("ignore_class config #{settings['ignore_class']} should be Array.")
90
+ end
91
+ if !settings.has_key?('api_url') || settings['api_url'].length == 0
92
+ errors.push("API URL is not configured.")
93
+ end
94
+ if !settings.has_key?('default_lang') || settings['default_lang'].nil?
95
+ errors.push("Default lang #{settings['default_lang']} is not valid.")
96
+ end
97
+ if !settings.has_key?('supported_langs') || !settings['supported_langs'].kind_of?(Array) || settings['supported_langs'].size < 1
98
+ errors.push("Supported langs configuration is not valid.")
99
+ end
100
+ if !settings.has_key?('custom_lang_aliases') || !settings['custom_lang_aliases'].kind_of?(Hash)
101
+ errors.push("Custom lang aliases is not valid.")
102
+ end
103
+ # log errors
104
+ if errors.length > 0
105
+ valid = false
106
+ errors.each do |e|
107
+ WovnLogger.instance.error(e)
108
+ end
109
+ end
110
+ valid
111
+ end
112
+
113
+ # Returns the settings object, pulling from Rails config the first time this is called
114
+ #
115
+ def settings
116
+ load_settings unless @config_loaded
117
+ @settings
118
+ end
119
+
120
+ # Load Rails config.wovnrb
121
+ #
122
+ def load_settings
123
+ if Object.const_defined?(:Rails) && Rails.configuration.respond_to?(:wovnrb)
124
+ @config_loaded = true
125
+ update_settings(Rails.configuration.wovnrb)
126
+ end
127
+ end
128
+
129
+ def update_settings(new_settings)
130
+ load_settings unless @config_loaded
131
+ if !new_settings.nil?
132
+ @settings.merge!(new_settings.stringify_keys)
133
+ format_settings
134
+ end
135
+ end
136
+
137
+ def format_settings
138
+ if @settings.has_key?('custom_lang_aliases')
139
+ stringify_keys! @settings['custom_lang_aliases']
140
+ end
141
+
142
+ @settings['default_lang'] = Lang.get_code(@settings['default_lang'])
143
+ if !@settings.has_key?('supported_langs')
144
+ @settings['supported_langs'] = [@settings['default_lang']]
145
+ end
146
+
147
+ if @settings.has_key?('user_token') && @settings['project_token'].empty?
148
+ @settings['project_token'] = @settings['user_token']
149
+ end
150
+ @settings.delete('user_token')
151
+
152
+ case @settings['url_pattern']
153
+ when 'path'
154
+ @settings['url_pattern_reg'] = "/(?<lang>[^/.?]+)"
155
+ when 'query'
156
+ @settings['url_pattern_reg'] = "((\\?.*&)|\\?)#{@settings['lang_param_name']}=(?<lang>[^&]+)(&|$)"
157
+ when 'subdomain'
158
+ @settings['url_pattern_reg'] = '^(?<lang>[^.]+)\.'
159
+ when 'custom_domain'
160
+ # Do not use regex
161
+ end
162
+
163
+ @settings['test_mode'] = !(@settings['test_mode'] != true || @settings['test_mode'] != 'on')
164
+
165
+ if @settings['wovn_dev_mode']
166
+ if @settings['api_url'] == self.class.default_settings['api_url']
167
+ @settings['api_url'] = 'http://dev-wovn.io:3001'
168
+ end
169
+
170
+ if @settings['api_timeout_seconds'] == self.class.default_settings['api_timeout_seconds']
171
+ @settings['api_timeout_seconds'] = 3.0
172
+ end
173
+ end
174
+ end
175
+
176
+ def custom_lang_aliases
177
+ @settings['custom_lang_aliases'] || {}
178
+ end
179
+
180
+ def default_lang
181
+ @settings['default_lang']
182
+ end
183
+
184
+ def default_lang_alias
185
+ custom_alias = custom_lang_aliases[default_lang]
186
+ custom_alias || default_lang
187
+ end
188
+
189
+ def supported_langs
190
+ @settings['supported_langs'] || []
191
+ end
192
+
193
+ def compress_api_requests?
194
+ @settings['compress_api_requests']
195
+ end
196
+
197
+ def widget_url
198
+ @settings['widget_url'] || 'https://j.wovn.io/1'
199
+ end
200
+
201
+ def dev_mode?
202
+ @settings['wovn_dev_mode']
203
+ end
204
+
205
+ def url_pattern
206
+ @settings['url_pattern']
207
+ end
208
+
209
+ def custom_domain_langs
210
+ @custom_domain_langs ||= CustomDomainLangs.new(@settings['custom_domain_langs'])
211
+ end
212
+
213
+ private
214
+
215
+ def stringify_keys!(hash)
216
+ hash.keys.each do |k|
217
+ hash[k.to_s] = hash.delete(k)
218
+ end
219
+ end
220
+ end
221
+ end
@@ -1,3 +1,3 @@
1
- module Wovnrb
2
- VERSION = '3.9.0'.freeze
3
- end
1
+ module Wovnrb
2
+ VERSION = '3.10.1'.freeze
3
+ end