wovnrb 3.9.0 → 3.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.en.md +256 -249
- data/README.ja.md +218 -211
- data/docker/rails/TestSite/yarn.lock +7353 -7353
- data/lib/wovnrb/api_translator.rb +183 -178
- data/lib/wovnrb/headers.rb +192 -192
- data/lib/wovnrb/services/html_converter.rb +226 -226
- data/lib/wovnrb/services/html_replace_marker.rb +48 -48
- data/lib/wovnrb/store.rb +221 -220
- data/lib/wovnrb/version.rb +3 -3
- data/test/lib/api_translator_test.rb +217 -215
- data/test/lib/services/html_converter_test.rb +512 -492
- data/test/lib/services/html_replace_marker_test.rb +149 -151
- data/test/lib/wovnrb_test.rb +342 -342
- metadata +2 -2
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
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
#
|
53
|
-
#
|
54
|
-
|
55
|
-
|
56
|
-
@
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
#
|
63
|
-
#
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
#
|
70
|
-
#
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
if
|
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
|
-
|
113
|
-
#
|
114
|
-
|
115
|
-
|
116
|
-
@
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
#
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
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
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
custom_alias
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
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
|
data/lib/wovnrb/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Wovnrb
|
2
|
-
VERSION = '3.
|
3
|
-
end
|
1
|
+
module Wovnrb
|
2
|
+
VERSION = '3.10.1'.freeze
|
3
|
+
end
|