wovnrb 3.0.1 → 3.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.circleci/config.yml +21 -10
- data/.rubocop_todo.yml +9 -44
- data/Gemfile +0 -5
- data/README.en.md +21 -16
- data/README.ja.md +6 -6
- data/docker/docker-compose.yml +5 -0
- data/docker/rails/TestSite/Gemfile +1 -1
- data/docker/rails/TestSite/app/controllers/custom_response_controller.rb +16 -0
- data/docker/rails/TestSite/config/routes.rb +1 -0
- data/docker/rails/TestSite/start.sh +1 -0
- data/docker/rails/TestSite/yarn.lock +12 -12
- data/lib/wovnrb/api_translator.rb +39 -17
- data/lib/wovnrb/headers.rb +30 -39
- data/lib/wovnrb/helpers/nokogumbo_helper.rb +7 -9
- data/lib/wovnrb/lang.rb +15 -14
- data/lib/wovnrb/railtie.rb +3 -1
- data/lib/wovnrb/services/html_converter.rb +8 -8
- data/lib/wovnrb/services/wovn_logger.rb +9 -2
- data/lib/wovnrb/store.rb +16 -17
- data/lib/wovnrb/version.rb +1 -1
- data/lib/wovnrb.rb +2 -4
- data/test/lib/api_translator_test.rb +48 -9
- data/test/lib/headers_test.rb +2 -2
- data/test/lib/lang_test.rb +5 -136
- data/test/lib/services/html_converter_test.rb +14 -14
- data/test/lib/services/wovn_logger_test.rb +5 -3
- data/test/lib/store_test.rb +22 -16
- data/test/lib/wovnrb_test.rb +16 -16
- data/wovnrb.gemspec +2 -1
- metadata +10 -10
- data/.travis.yml +0 -14
- data/lib/wovnrb/text_caches/cache_base.rb +0 -53
- data/lib/wovnrb/text_caches/memory_cache.rb +0 -51
- data/test/lib/text_caches/cache_base_test.rb +0 -31
- data/test/lib/text_caches/memory_cache_test.rb +0 -91
@@ -6,7 +6,7 @@ module Wovnrb
|
|
6
6
|
converter = prepare_html_converter('<html><body><a class="test">hello</a></body></html>', supported_langs: %w[en vi])
|
7
7
|
converted_html, = converter.build_api_compatible_html
|
8
8
|
|
9
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
9
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a class=\"test\">hello</a></body></html>"
|
10
10
|
assert_equal(expected_html, converted_html)
|
11
11
|
end
|
12
12
|
|
@@ -19,16 +19,16 @@ module Wovnrb
|
|
19
19
|
converter = prepare_html_converter('<html><body><a class="test">hello</a></body></html>', settings)
|
20
20
|
converted_html, = converter.build_api_compatible_html
|
21
21
|
|
22
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
22
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=lang&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?lang=vi\"></head><body><a class=\"test\">hello</a></body></html>"
|
23
23
|
assert_equal(expected_html, converted_html)
|
24
24
|
end
|
25
25
|
|
26
26
|
def test_build_api_compatible_html_not_fail_for_big_content
|
27
27
|
long_string = 'a' * 60_000
|
28
|
-
converter = prepare_html_converter(
|
28
|
+
converter = prepare_html_converter("<html><body><p>#{long_string}</p></body></html>", supported_langs: %w[en vi])
|
29
29
|
converted_html, = converter.build_api_compatible_html
|
30
30
|
|
31
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
31
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><p>#{long_string}</p></body></html>"
|
32
32
|
assert_equal(expected_html, converted_html)
|
33
33
|
end
|
34
34
|
|
@@ -45,7 +45,7 @@ module Wovnrb
|
|
45
45
|
converter = prepare_html_converter(html, ignore_class: ['ignore-me'])
|
46
46
|
converted_html, = converter.build_api_compatible_html
|
47
47
|
|
48
|
-
expected_convert_html = "<html lang=\"en\"><head><script src=\"
|
48
|
+
expected_convert_html = "<html lang=\"en\"><head><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><p>Hello <span wovn-ignore=\"\"><!-- __wovn-backend-ignored-key-0 --></span></p><p></p><p>Hello <span data-wovn-ignore=\"\"><!-- __wovn-backend-ignored-key-1 --></span></p><p></p><div><span class=\"ignore-me\"><!-- __wovn-backend-ignored-key-2 --></span></div><span>Have a nice day!</span></body></html>"
|
49
49
|
assert_equal(expected_convert_html, converted_html)
|
50
50
|
end
|
51
51
|
|
@@ -62,7 +62,7 @@ module Wovnrb
|
|
62
62
|
converter = prepare_html_converter(html, ignore_class: [])
|
63
63
|
converted_html, = converter.build_api_compatible_html
|
64
64
|
|
65
|
-
expected_convert_html = "<html lang=\"en\"><head><script src=\"
|
65
|
+
expected_convert_html = "<html lang=\"en\"><head><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><form action=\"/test\" method=\"POST\"><!-- __wovn-backend-ignored-key-0 --></form></body></html>"
|
66
66
|
assert_equal(expected_convert_html, converted_html)
|
67
67
|
end
|
68
68
|
|
@@ -81,7 +81,7 @@ module Wovnrb
|
|
81
81
|
|
82
82
|
expected_convert_html = [
|
83
83
|
'<html lang="en"><head>',
|
84
|
-
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
84
|
+
"<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"fr\" href=\"http://my-site.com/?wovn=fr\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body>",
|
85
85
|
'<input id="user-id" type="hidden" value="__wovn-backend-ignored-key-0">',
|
86
86
|
'<input id="password" type="hidden" value="__wovn-backend-ignored-key-1">',
|
87
87
|
'<input id="something" type="hidden" value="__wovn-backend-ignored-key-2">',
|
@@ -95,7 +95,7 @@ module Wovnrb
|
|
95
95
|
converter = prepare_html_converter('<html><body><a>hello</a></body></html>', supported_langs: %w[en vi])
|
96
96
|
translated_html = converter.build
|
97
97
|
|
98
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
98
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a>hello</a></body></html>"
|
99
99
|
assert_equal(expected_html, translated_html)
|
100
100
|
end
|
101
101
|
|
@@ -103,7 +103,7 @@ module Wovnrb
|
|
103
103
|
converter = prepare_html_converter('<html><body><a>hello</a></body></html>', supported_langs: [])
|
104
104
|
translated_html = converter.build
|
105
105
|
|
106
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
106
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script></head><body><a>hello</a></body></html>"
|
107
107
|
assert_equal(expected_html, translated_html)
|
108
108
|
end
|
109
109
|
|
@@ -111,7 +111,7 @@ module Wovnrb
|
|
111
111
|
converter = prepare_html_converter('<html><head><title>TITLE</title></head><body><a>hello</a></body></html>', supported_langs: %w[en vi])
|
112
112
|
translated_html = converter.build
|
113
113
|
|
114
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
114
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><title>TITLE</title><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body><a>hello</a></body></html>"
|
115
115
|
assert_equal(expected_html, translated_html)
|
116
116
|
end
|
117
117
|
|
@@ -119,7 +119,7 @@ module Wovnrb
|
|
119
119
|
converter = prepare_html_converter('<html>hello<a>world</a></html>', supported_langs: [])
|
120
120
|
translated_html = converter.build
|
121
121
|
|
122
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
122
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script></head><body>hello<a>world</a></body></html>"
|
123
123
|
assert_equal(expected_html, translated_html)
|
124
124
|
end
|
125
125
|
|
@@ -134,7 +134,7 @@ module Wovnrb
|
|
134
134
|
converter = HtmlConverter.new(dom, store, headers)
|
135
135
|
translated_html = converter.build
|
136
136
|
|
137
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
137
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/?wovn=ja\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/?wovn=vi\"></head><body>hello<a>world</a></body></html>"
|
138
138
|
assert_equal(expected_html, translated_html)
|
139
139
|
end
|
140
140
|
|
@@ -149,7 +149,7 @@ module Wovnrb
|
|
149
149
|
converter = HtmlConverter.new(dom, store, headers)
|
150
150
|
translated_html = converter.build
|
151
151
|
|
152
|
-
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
152
|
+
expected_html = "<html lang=\"en\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=path&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://my-site.com/\"><link rel=\"alternate\" hreflang=\"ja\" href=\"http://my-site.com/ja/\"><link rel=\"alternate\" hreflang=\"vi\" href=\"http://my-site.com/vi/\"></head><body>hello<a>world</a></body></html>"
|
153
153
|
assert_equal(expected_html, translated_html)
|
154
154
|
end
|
155
155
|
|
@@ -163,7 +163,7 @@ module Wovnrb
|
|
163
163
|
</head></html>')
|
164
164
|
converter.send(:replace_snippet)
|
165
165
|
|
166
|
-
expected_html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
166
|
+
expected_html = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=en&defaultLang=en&urlPattern=query&langCodeAliases={}&langParamName=wovn&version=WOVN.rb_#{VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><script src=\"/a\"></script></head><body></body></html>"
|
167
167
|
assert_equal(expected_html.gsub(/\s+/, ''), converter.send(:html).gsub(/\s+/, ''))
|
168
168
|
end
|
169
169
|
|
@@ -31,9 +31,11 @@ module Wovnrb
|
|
31
31
|
end
|
32
32
|
|
33
33
|
def test_error
|
34
|
-
|
35
|
-
|
36
|
-
|
34
|
+
SecureRandom.stub(:uuid, 'a') do
|
35
|
+
mock = LogMock.mock_log
|
36
|
+
WovnLogger.instance.error('aaa')
|
37
|
+
assert_equal(['[a] aaa'], mock.errors)
|
38
|
+
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
data/test/lib/store_test.rb
CHANGED
@@ -78,12 +78,14 @@ module Wovnrb
|
|
78
78
|
end
|
79
79
|
|
80
80
|
def test_invalid_settings
|
81
|
-
|
82
|
-
|
83
|
-
|
81
|
+
SecureRandom.stub(:uuid, 'a') do
|
82
|
+
mock = LogMock.mock_log
|
83
|
+
store = Wovnrb::Store.instance
|
84
|
+
valid = store.valid_settings?
|
84
85
|
|
85
|
-
|
86
|
-
|
86
|
+
assert_equal(false, valid)
|
87
|
+
assert_equal(['[a] Project token is not valid.'], mock.errors)
|
88
|
+
end
|
87
89
|
end
|
88
90
|
|
89
91
|
def test_settings_ignore_paths
|
@@ -110,12 +112,14 @@ module Wovnrb
|
|
110
112
|
end
|
111
113
|
|
112
114
|
def test_settings_invalid_ignore_paths
|
113
|
-
|
114
|
-
|
115
|
-
|
115
|
+
SecureRandom.stub(:uuid, 'a') do
|
116
|
+
mock = LogMock.mock_log
|
117
|
+
store = Wovnrb::Store.instance
|
118
|
+
store.update_settings('ignore_paths' => 'aaaa')
|
116
119
|
|
117
|
-
|
118
|
-
|
120
|
+
assert_equal(false, store.valid_settings?)
|
121
|
+
assert_equal(['[a] Project token is not valid.', '[a] Ignore Paths aaaa should be Array.'], mock.errors)
|
122
|
+
end
|
119
123
|
end
|
120
124
|
|
121
125
|
def test_settings_ignore_glob_injection
|
@@ -126,15 +130,17 @@ module Wovnrb
|
|
126
130
|
assert_equal([], s.settings['ignore_globs'])
|
127
131
|
end
|
128
132
|
|
129
|
-
def
|
133
|
+
def test_widget_url_settings
|
130
134
|
store = Wovnrb::Store.instance
|
131
135
|
|
132
|
-
store.update_settings('
|
136
|
+
store.update_settings('widget_url' => nil)
|
137
|
+
assert_equal('https://j.wovn.io/1', store.widget_url)
|
133
138
|
|
134
|
-
|
135
|
-
assert_equal('
|
136
|
-
|
137
|
-
|
139
|
+
store.update_settings('widget_url' => 'https://j.wovn.io/1')
|
140
|
+
assert_equal('https://j.wovn.io/1', store.widget_url)
|
141
|
+
|
142
|
+
store.update_settings('widget_url' => 'http://j.dev-wovn.io:3000/1')
|
143
|
+
assert_equal('http://j.dev-wovn.io:3000/1', store.widget_url)
|
138
144
|
end
|
139
145
|
|
140
146
|
def test_dev_mode_not_overriding_settings
|
data/test/lib/wovnrb_test.rb
CHANGED
@@ -16,7 +16,7 @@ class WovnrbTest < Minitest::Test
|
|
16
16
|
|
17
17
|
expected_body = [
|
18
18
|
'<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
|
19
|
-
"<script src=\"
|
19
|
+
"<script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
|
20
20
|
'<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
|
21
21
|
'<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
|
22
22
|
'<body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>',
|
@@ -24,7 +24,7 @@ class WovnrbTest < Minitest::Test
|
|
24
24
|
'</body></html>'
|
25
25
|
].join
|
26
26
|
|
27
|
-
assert_switch_lang('en', 'ja', body, expected_body, true)
|
27
|
+
assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
|
28
28
|
end
|
29
29
|
|
30
30
|
def test_switch_lang_with_input_tags
|
@@ -40,7 +40,7 @@ class WovnrbTest < Minitest::Test
|
|
40
40
|
|
41
41
|
expected_body = [
|
42
42
|
'<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
|
43
|
-
"<script src=\"
|
43
|
+
"<script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true¤tLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script>",
|
44
44
|
'<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
|
45
45
|
'<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
|
46
46
|
'<body>',
|
@@ -52,23 +52,23 @@ class WovnrbTest < Minitest::Test
|
|
52
52
|
'</body></html>'
|
53
53
|
].join
|
54
54
|
|
55
|
-
assert_switch_lang('en', 'ja', body, expected_body, true)
|
55
|
+
assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
|
56
56
|
end
|
57
57
|
|
58
58
|
def test_switch_lang_of_html_fragment_with_japanese_translations
|
59
59
|
bodies = ['<span>Hello</span>'].join
|
60
60
|
expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
|
61
61
|
|
62
|
-
assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
|
62
|
+
assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true)
|
63
63
|
end
|
64
64
|
|
65
65
|
def test_switch_lang_splitted_body
|
66
66
|
bodies = ['<html><body><h1>Mr. Belvedere Fan Club</h1>',
|
67
67
|
'<div><p>Hello</p></div>',
|
68
68
|
'</body></html>'].join
|
69
|
-
expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
69
|
+
expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&backend=true&currentLang=ja&defaultLang=en&urlPattern=subdomain&langCodeAliases={}&version=WOVN.rb_#{Wovnrb::VERSION}\" data-wovnio-type=\"fallback_snippet\"></script><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>"].join
|
70
70
|
|
71
|
-
assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
|
71
|
+
assert_switch_lang('en', 'ja', bodies, expected_bodies, api_expected: true)
|
72
72
|
end
|
73
73
|
|
74
74
|
def test_switch_lang_of_html_fragment_in_splitted_body
|
@@ -76,19 +76,19 @@ class WovnrbTest < Minitest::Test
|
|
76
76
|
'<option value="2">2</option></select>'].join
|
77
77
|
expected_body = ['<select name="test"><option value="1">1</option><option value="2">2</option></select>'].join
|
78
78
|
|
79
|
-
assert_switch_lang('en', 'ja', body, expected_body, true)
|
79
|
+
assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
|
80
80
|
end
|
81
81
|
|
82
82
|
def test_switch_lang_missing_values
|
83
83
|
body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
|
84
84
|
<div><p>Hello</p></div>
|
85
85
|
</body></html>"
|
86
|
-
expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"
|
86
|
+
expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&backend=true&currentLang=ja&defaultLang=en&urlPattern=path&langCodeAliases={}&version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
|
87
87
|
<div><p>Hello</p></div>
|
88
88
|
</body></html>
|
89
89
|
"
|
90
90
|
|
91
|
-
assert_switch_lang('en', 'ja', body, expected_body, true)
|
91
|
+
assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
|
92
92
|
end
|
93
93
|
|
94
94
|
def test_switch_lang_on_fragment_with_translate_fragment_false
|
@@ -96,7 +96,7 @@ class WovnrbTest < Minitest::Test
|
|
96
96
|
<div><p>Hello</p></div>"
|
97
97
|
|
98
98
|
Wovnrb::Store.instance.settings['translate_fragment'] = false
|
99
|
-
assert_switch_lang('en', 'ja', body, body, false)
|
99
|
+
assert_switch_lang('en', 'ja', body, body, api_expected: false)
|
100
100
|
end
|
101
101
|
|
102
102
|
def test_switch_lang_on_fragment_with_translate_fragment_true
|
@@ -106,7 +106,7 @@ class WovnrbTest < Minitest::Test
|
|
106
106
|
<div><p><!--wovn-src:Hello-->こんにちは</p></div>"
|
107
107
|
|
108
108
|
Wovnrb::Store.instance.settings['translate_fragment'] = true
|
109
|
-
assert_switch_lang('en', 'ja', body, expected_body, true)
|
109
|
+
assert_switch_lang('en', 'ja', body, expected_body, api_expected: true)
|
110
110
|
end
|
111
111
|
|
112
112
|
def test_switch_lang_ignores_amp
|
@@ -120,7 +120,7 @@ class WovnrbTest < Minitest::Test
|
|
120
120
|
</html>
|
121
121
|
HTML
|
122
122
|
|
123
|
-
assert_switch_lang('en', 'ja', body, body, false)
|
123
|
+
assert_switch_lang('en', 'ja', body, body, api_expected: false)
|
124
124
|
end
|
125
125
|
|
126
126
|
def test_switch_lang_ignores_amp_defined_with_symbol_attribute
|
@@ -133,7 +133,7 @@ HTML
|
|
133
133
|
</html>
|
134
134
|
HTML
|
135
135
|
|
136
|
-
assert_switch_lang('en', 'ja', body, body, false)
|
136
|
+
assert_switch_lang('en', 'ja', body, body, api_expected: false)
|
137
137
|
end
|
138
138
|
|
139
139
|
def test_call_without_path_ignored_should_change_environment
|
@@ -249,7 +249,7 @@ HTML
|
|
249
249
|
assert_equal(unaffected_env != app_mock.env, affected)
|
250
250
|
end
|
251
251
|
|
252
|
-
def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected
|
252
|
+
def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected: true)
|
253
253
|
subdomain = target_lang == original_lang ? '' : "#{target_lang}."
|
254
254
|
interceptor = Wovnrb::Interceptor.new(get_app)
|
255
255
|
|
@@ -303,7 +303,7 @@ HTML
|
|
303
303
|
|
304
304
|
def initialize(opts = {})
|
305
305
|
@params = {}
|
306
|
-
if opts.key?(:params) && opts[:params].
|
306
|
+
if opts.key?(:params) && opts[:params].instance_of?(Hash)
|
307
307
|
opts[:params].each do |key, val|
|
308
308
|
@params[key] = val
|
309
309
|
end
|
data/wovnrb.gemspec
CHANGED
@@ -11,6 +11,7 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.description = 'Ruby gem for WOVN backend on Rack.'
|
12
12
|
spec.homepage = 'https://wovn.io'
|
13
13
|
spec.license = 'MIT'
|
14
|
+
spec.required_ruby_version = '>= 2.5'
|
14
15
|
|
15
16
|
files = `git ls-files -z`.split("\x0")
|
16
17
|
files.delete('BEFORE_PUSHING')
|
@@ -22,7 +23,7 @@ Gem::Specification.new do |spec|
|
|
22
23
|
spec.add_dependency 'activesupport'
|
23
24
|
spec.add_dependency 'addressable'
|
24
25
|
spec.add_dependency 'lz4-ruby'
|
25
|
-
spec.add_dependency 'nokogiri', '>= 1.8.1'
|
26
|
+
spec.add_dependency 'nokogiri', '>= 1.8.1', '<= 1.11'
|
26
27
|
spec.add_dependency 'nokogumbo', '>= 1.4.0', '< 2.0.0'
|
27
28
|
spec.add_dependency 'rack'
|
28
29
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: wovnrb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.
|
4
|
+
version: 3.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Wovn Technologies, Inc.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -59,6 +59,9 @@ dependencies:
|
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 1.8.1
|
62
|
+
- - "<="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '1.11'
|
62
65
|
type: :runtime
|
63
66
|
prerelease: false
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -66,6 +69,9 @@ dependencies:
|
|
66
69
|
- - ">="
|
67
70
|
- !ruby/object:Gem::Version
|
68
71
|
version: 1.8.1
|
72
|
+
- - "<="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.11'
|
69
75
|
- !ruby/object:Gem::Dependency
|
70
76
|
name: nokogumbo
|
71
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -405,7 +411,6 @@ files:
|
|
405
411
|
- ".gitignore"
|
406
412
|
- ".rubocop.yml"
|
407
413
|
- ".rubocop_todo.yml"
|
408
|
-
- ".travis.yml"
|
409
414
|
- Gemfile
|
410
415
|
- LICENSE.txt
|
411
416
|
- PULL_REQUEST_TEMPLATE
|
@@ -428,6 +433,7 @@ files:
|
|
428
433
|
- docker/rails/TestSite/app/channels/application_cable/connection.rb
|
429
434
|
- docker/rails/TestSite/app/controllers/application_controller.rb
|
430
435
|
- docker/rails/TestSite/app/controllers/concerns/.keep
|
436
|
+
- docker/rails/TestSite/app/controllers/custom_response_controller.rb
|
431
437
|
- docker/rails/TestSite/app/controllers/pages_controller.rb
|
432
438
|
- docker/rails/TestSite/app/controllers/redirects_controller.rb
|
433
439
|
- docker/rails/TestSite/app/helpers/application_helper.rb
|
@@ -527,8 +533,6 @@ files:
|
|
527
533
|
- lib/wovnrb/services/wovn_logger.rb
|
528
534
|
- lib/wovnrb/settings.rb
|
529
535
|
- lib/wovnrb/store.rb
|
530
|
-
- lib/wovnrb/text_caches/cache_base.rb
|
531
|
-
- lib/wovnrb/text_caches/memory_cache.rb
|
532
536
|
- lib/wovnrb/version.rb
|
533
537
|
- makefile
|
534
538
|
- test/fixtures/html/test.html
|
@@ -541,8 +545,6 @@ files:
|
|
541
545
|
- test/lib/services/html_replace_marker_test.rb
|
542
546
|
- test/lib/services/wovn_logger_test.rb
|
543
547
|
- test/lib/store_test.rb
|
544
|
-
- test/lib/text_caches/cache_base_test.rb
|
545
|
-
- test/lib/text_caches/memory_cache_test.rb
|
546
548
|
- test/lib/wovnrb_test.rb
|
547
549
|
- test/test_helper.rb
|
548
550
|
- wovnrb.gemspec
|
@@ -558,7 +560,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
558
560
|
requirements:
|
559
561
|
- - ">="
|
560
562
|
- !ruby/object:Gem::Version
|
561
|
-
version: '
|
563
|
+
version: '2.5'
|
562
564
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
563
565
|
requirements:
|
564
566
|
- - ">="
|
@@ -580,7 +582,5 @@ test_files:
|
|
580
582
|
- test/lib/services/html_replace_marker_test.rb
|
581
583
|
- test/lib/services/wovn_logger_test.rb
|
582
584
|
- test/lib/store_test.rb
|
583
|
-
- test/lib/text_caches/cache_base_test.rb
|
584
|
-
- test/lib/text_caches/memory_cache_test.rb
|
585
585
|
- test/lib/wovnrb_test.rb
|
586
586
|
- test/test_helper.rb
|
data/.travis.yml
DELETED
@@ -1,14 +0,0 @@
|
|
1
|
-
language: ruby
|
2
|
-
cache: bundler
|
3
|
-
rvm:
|
4
|
-
# Supported versions are
|
5
|
-
# > Ruby version:2.2 or later
|
6
|
-
# https://wovn.io/support/ruby-library/
|
7
|
-
- 2.5.0
|
8
|
-
- 2.4.3
|
9
|
-
- 2.3.6
|
10
|
-
- 2.2.9
|
11
|
-
# There is no support for 2.1 officially, but test for now.
|
12
|
-
- 2.1.10
|
13
|
-
before_install:
|
14
|
-
- gem update bundler --no-document
|
@@ -1,53 +0,0 @@
|
|
1
|
-
require 'active_support/inflector'
|
2
|
-
|
3
|
-
module Wovnrb
|
4
|
-
class CacheBase
|
5
|
-
@@strategy_map = {
|
6
|
-
memory: :memory_cache
|
7
|
-
}
|
8
|
-
|
9
|
-
@@default_base_config = {
|
10
|
-
strategy: :memory
|
11
|
-
}
|
12
|
-
|
13
|
-
@@singleton_cache = nil
|
14
|
-
def self.get_single
|
15
|
-
raise 'cache is not initialized' unless @@singleton_cache
|
16
|
-
|
17
|
-
@@singleton_cache
|
18
|
-
end
|
19
|
-
|
20
|
-
def self.set_single(config)
|
21
|
-
@@singleton_cache = build(config)
|
22
|
-
end
|
23
|
-
|
24
|
-
def self.reset_cache
|
25
|
-
@@singleton_cache = nil
|
26
|
-
end
|
27
|
-
|
28
|
-
def self.build(config)
|
29
|
-
@config = @@default_base_config.merge config
|
30
|
-
|
31
|
-
strategy = @@strategy_map[@config[:strategy]]
|
32
|
-
raise "Invalid strategy: #{strategy}" unless strategy
|
33
|
-
|
34
|
-
strategy_sym = strategy.to_sym
|
35
|
-
begin
|
36
|
-
require "wovnrb/text_caches/#{strategy_sym}"
|
37
|
-
rescue LoadError => e
|
38
|
-
raise "Could not find #{strategy_sym} (#{e})"
|
39
|
-
end
|
40
|
-
|
41
|
-
strategy_class = Wovnrb.const_get(ActiveSupport::Inflector.camelize(strategy_sym))
|
42
|
-
strategy_class.new(config)
|
43
|
-
end
|
44
|
-
|
45
|
-
def put(key, value)
|
46
|
-
raise NotImplementedError.new('put is not defined')
|
47
|
-
end
|
48
|
-
|
49
|
-
def get(key)
|
50
|
-
raise NotImplementedError.new('put is not defined')
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
@@ -1,51 +0,0 @@
|
|
1
|
-
require 'active_support/cache'
|
2
|
-
require 'lz4-ruby'
|
3
|
-
|
4
|
-
module Wovnrb
|
5
|
-
class MemoryCache < CacheBase
|
6
|
-
@@default_memory_cache_config = {
|
7
|
-
cache_megabytes: 200,
|
8
|
-
ttl_seconds: 300
|
9
|
-
}
|
10
|
-
|
11
|
-
def initialize(config)
|
12
|
-
@config = merge_setting(@@default_memory_cache_config, config)
|
13
|
-
cache_size = @config[:cache_megabytes].to_f
|
14
|
-
ttl = @config[:ttl_seconds].to_i
|
15
|
-
@cache_store = ActiveSupport::Cache::MemoryStore.new(expires_in: ttl.seconds, size: cache_size.megabytes)
|
16
|
-
end
|
17
|
-
|
18
|
-
def put(key, value)
|
19
|
-
@cache_store.write(key, compress(value))
|
20
|
-
end
|
21
|
-
|
22
|
-
def get(key)
|
23
|
-
stored_value =@cache_store.fetch(key)
|
24
|
-
decompress(stored_value) if stored_value
|
25
|
-
end
|
26
|
-
|
27
|
-
def options
|
28
|
-
@cache_store.options.clone
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
def merge_setting(original_config, merging_config)
|
33
|
-
config = original_config.clone
|
34
|
-
config.keys.each do |key|
|
35
|
-
key_string = key.to_s
|
36
|
-
if merging_config.has_key?(key_string) && merging_config[key_string].present?
|
37
|
-
config[key] = merging_config[key_string]
|
38
|
-
end
|
39
|
-
end
|
40
|
-
config
|
41
|
-
end
|
42
|
-
|
43
|
-
def compress(value)
|
44
|
-
LZ4.compress(value)
|
45
|
-
end
|
46
|
-
|
47
|
-
def decompress(value)
|
48
|
-
LZ4.decompress(value, value.bytesize, 'UTF-8')
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
@@ -1,31 +0,0 @@
|
|
1
|
-
require 'wovnrb/text_caches/cache_base'
|
2
|
-
require 'minitest/autorun'
|
3
|
-
|
4
|
-
class CacheBaseTest < Minitest::Test
|
5
|
-
def setup
|
6
|
-
Wovnrb::CacheBase.reset_cache
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_build
|
10
|
-
cache = Wovnrb::CacheBase.build({})
|
11
|
-
assert_equal('Wovnrb::MemoryCache', cache.class.name)
|
12
|
-
end
|
13
|
-
|
14
|
-
def test_build_with_invalid_strategy
|
15
|
-
assert_raises RuntimeError do
|
16
|
-
Wovnrb::CacheBase.build(strategy: :invalid)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_set_and_get_single
|
21
|
-
Wovnrb::CacheBase.set_single({})
|
22
|
-
cache = Wovnrb::CacheBase.get_single
|
23
|
-
assert_equal('Wovnrb::MemoryCache', cache.class.name)
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_get_single_without_set
|
27
|
-
assert_raises RuntimeError do
|
28
|
-
Wovnrb::CacheBase.get_single
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|