wovnrb 1.1.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (120) hide show
  1. checksums.yaml +4 -4
  2. data/.circleci/config.yml +1 -0
  3. data/.gitignore +2 -0
  4. data/.rubocop.yml +1 -0
  5. data/.rubocop_todo.yml +308 -0
  6. data/Rakefile +13 -14
  7. data/lib/wovnrb.rb +43 -98
  8. data/lib/wovnrb/api_translator.rb +143 -0
  9. data/lib/wovnrb/headers.rb +78 -92
  10. data/lib/wovnrb/helpers/nokogumbo_helper.rb +1 -1
  11. data/lib/wovnrb/lang.rb +93 -125
  12. data/lib/wovnrb/railtie.rb +5 -7
  13. data/lib/wovnrb/services/glob.rb +3 -3
  14. data/lib/wovnrb/services/html_converter.rb +192 -0
  15. data/lib/wovnrb/services/html_replace_marker.rb +38 -0
  16. data/lib/wovnrb/services/wovn_logger.rb +8 -4
  17. data/lib/wovnrb/settings.rb +5 -3
  18. data/lib/wovnrb/store.rb +35 -26
  19. data/lib/wovnrb/text_caches/cache_base.rb +3 -2
  20. data/lib/wovnrb/text_caches/memory_cache.rb +2 -2
  21. data/lib/wovnrb/version.rb +1 -1
  22. data/test/fixtures/html/test.html +8 -0
  23. data/test/fixtures/html/test_translated.html +8 -0
  24. data/test/lib/api_translator_test.rb +109 -0
  25. data/test/lib/headers_test.rb +84 -55
  26. data/test/lib/lang_test.rb +157 -357
  27. data/test/lib/services/glob_test.rb +1 -1
  28. data/test/lib/services/html_converter_test.rb +166 -0
  29. data/test/lib/services/html_replace_marker_test.rb +75 -0
  30. data/test/lib/services/wovn_logger_test.rb +6 -6
  31. data/test/lib/store_test.rb +25 -69
  32. data/test/lib/text_caches/cache_base_test.rb +1 -1
  33. data/test/lib/text_caches/memory_cache_test.rb +10 -11
  34. data/test/lib/wovnrb_test.rb +77 -310
  35. data/test/test_helper.rb +22 -32
  36. data/wovnrb.gemspec +35 -44
  37. metadata +86 -205
  38. data/ext/dom/Makefile +0 -239
  39. data/lib/wovnrb/api_data.rb +0 -59
  40. data/lib/wovnrb/html_replacers/image_replacer.rb +0 -69
  41. data/lib/wovnrb/html_replacers/input_replacer.rb +0 -38
  42. data/lib/wovnrb/html_replacers/link_replacer.rb +0 -78
  43. data/lib/wovnrb/html_replacers/meta_replacer.rb +0 -28
  44. data/lib/wovnrb/html_replacers/replacer_base.rb +0 -49
  45. data/lib/wovnrb/html_replacers/script_replacer.rb +0 -39
  46. data/lib/wovnrb/html_replacers/text_replacer.rb +0 -21
  47. data/lib/wovnrb/html_replacers/unified_values/dst_swapping_targets_creator.rb +0 -76
  48. data/lib/wovnrb/html_replacers/unified_values/element_category.rb +0 -242
  49. data/lib/wovnrb/html_replacers/unified_values/node_swapping_targets_creator.rb +0 -134
  50. data/lib/wovnrb/html_replacers/unified_values/text_replacer.rb +0 -35
  51. data/lib/wovnrb/html_replacers/unified_values/text_scraper.rb +0 -152
  52. data/lib/wovnrb/html_replacers/unified_values/values_stack.rb +0 -65
  53. data/lib/wovnrb/services/url.rb +0 -12
  54. data/lib/wovnrb/services/value_agent.rb +0 -9
  55. data/test/fixtures/unified_values/site_html/simple_actual.html +0 -96
  56. data/test/fixtures/unified_values/site_html/simple_expected.json +0 -251
  57. data/test/fixtures/unified_values/site_html/wovn.io_actual.html +0 -686
  58. data/test/fixtures/unified_values/site_html/wovn.io_expected.json +0 -543
  59. data/test/fixtures/unified_values/site_html/www.yahoo.co.jp_actual.html +0 -1024
  60. data/test/fixtures/unified_values/site_html/www.yahoo.co.jp_expected.json +0 -3345
  61. data/test/fixtures/unified_values/small_html/block_inside_inline_actual.html +0 -12
  62. data/test/fixtures/unified_values/small_html/block_inside_inline_expected.json +0 -22
  63. data/test/fixtures/unified_values/small_html/br_tag_actual.html +0 -10
  64. data/test/fixtures/unified_values/small_html/br_tag_expected.json +0 -12
  65. data/test/fixtures/unified_values/small_html/comment_tag_actual.html +0 -12
  66. data/test/fixtures/unified_values/small_html/comment_tag_expected.json +0 -10
  67. data/test/fixtures/unified_values/small_html/complex_text_with_html_entity_actual.html +0 -7
  68. data/test/fixtures/unified_values/small_html/complex_text_with_html_entity_expected.json +0 -11
  69. data/test/fixtures/unified_values/small_html/deep_nested_block_actual.html +0 -14
  70. data/test/fixtures/unified_values/small_html/deep_nested_block_expected.json +0 -8
  71. data/test/fixtures/unified_values/small_html/deep_nested_inline_actual.html +0 -20
  72. data/test/fixtures/unified_values/small_html/deep_nested_inline_expected.json +0 -20
  73. data/test/fixtures/unified_values/small_html/empty_tag_actual.html +0 -10
  74. data/test/fixtures/unified_values/small_html/empty_tag_expected.json +0 -12
  75. data/test/fixtures/unified_values/small_html/empty_text_actual.html +0 -12
  76. data/test/fixtures/unified_values/small_html/empty_text_expected.json +0 -1
  77. data/test/fixtures/unified_values/small_html/ignore_tag_actual.html +0 -12
  78. data/test/fixtures/unified_values/small_html/ignore_tag_expected.json +0 -16
  79. data/test/fixtures/unified_values/small_html/ignored_class_actual.html +0 -10
  80. data/test/fixtures/unified_values/small_html/ignored_class_expected.json +0 -13
  81. data/test/fixtures/unified_values/small_html/img_actual.html +0 -12
  82. data/test/fixtures/unified_values/small_html/img_expected.json +0 -23
  83. data/test/fixtures/unified_values/small_html/nested_and_complex_wovn_ignore_actual.html +0 -10
  84. data/test/fixtures/unified_values/small_html/nested_and_complex_wovn_ignore_expected.json +0 -16
  85. data/test/fixtures/unified_values/small_html/nested_text_value_actual.html +0 -10
  86. data/test/fixtures/unified_values/small_html/nested_text_value_expected.json +0 -12
  87. data/test/fixtures/unified_values/small_html/nested_text_value_mixed_plan_text_actual.html +0 -10
  88. data/test/fixtures/unified_values/small_html/nested_text_value_mixed_plan_text_expected.json +0 -14
  89. data/test/fixtures/unified_values/small_html/option_tag_actual.html +0 -9
  90. data/test/fixtures/unified_values/small_html/option_tag_expected.json +0 -13
  91. data/test/fixtures/unified_values/small_html/text_different_inline_each_other_actual.html +0 -10
  92. data/test/fixtures/unified_values/small_html/text_different_inline_each_other_expected.json +0 -22
  93. data/test/fixtures/unified_values/small_html/text_in_svg_actual.html +0 -9
  94. data/test/fixtures/unified_values/small_html/text_in_svg_expected.json +0 -8
  95. data/test/fixtures/unified_values/small_html/text_with_html_entity_actual.html +0 -6
  96. data/test/fixtures/unified_values/small_html/text_with_html_entity_expected.json +0 -8
  97. data/test/fixtures/unified_values/small_html/unknown_or_custom_tag_actual.html +0 -12
  98. data/test/fixtures/unified_values/small_html/unknown_or_custom_tag_expected.json +0 -24
  99. data/test/fixtures/unified_values/small_html/unnecessay_top_end_tag_actual.html +0 -12
  100. data/test/fixtures/unified_values/small_html/unnecessay_top_end_tag_expected.json +0 -14
  101. data/test/fixtures/unified_values/small_html/wovn_ignore_actual.html +0 -10
  102. data/test/fixtures/unified_values/small_html/wovn_ignore_expected.json +0 -13
  103. data/test/lib/api_data_test.rb +0 -83
  104. data/test/lib/html_replacers/image_replacer_test.rb +0 -165
  105. data/test/lib/html_replacers/input_replacer_test.rb +0 -140
  106. data/test/lib/html_replacers/link_replacer_test.rb +0 -328
  107. data/test/lib/html_replacers/meta_replacer_test.rb +0 -157
  108. data/test/lib/html_replacers/replacer_base_test.rb +0 -128
  109. data/test/lib/html_replacers/script_replacer_test.rb +0 -139
  110. data/test/lib/html_replacers/text_replacer_test.rb +0 -99
  111. data/test/lib/html_replacers/unified_values/dst_swapping_targets_creator_test.rb +0 -137
  112. data/test/lib/html_replacers/unified_values/element_category_test.rb +0 -49
  113. data/test/lib/html_replacers/unified_values/node_swapping_targets_creator_test.rb +0 -137
  114. data/test/lib/html_replacers/unified_values/text_replacer_test.rb +0 -270
  115. data/test/lib/html_replacers/unified_values/text_scraper_test.rb +0 -121
  116. data/test/lib/html_replacers/unified_values/values_stack_test.rb +0 -122
  117. data/test/lib/services/url_test.rb +0 -9
  118. data/test/lib/services/value_agent_test.rb +0 -32
  119. data/test/services/url_test.rb +0 -163
  120. data/values/values +0 -1
@@ -13,7 +13,7 @@ class CacheBaseTest < Minitest::Test
13
13
 
14
14
  def test_build_with_invalid_strategy
15
15
  assert_raises RuntimeError do
16
- Wovnrb::CacheBase.build({strategy: :invalid})
16
+ Wovnrb::CacheBase.build(strategy: :invalid)
17
17
  end
18
18
  end
19
19
 
@@ -1,4 +1,3 @@
1
- # -*- encoding: UTF-8 -*-
2
1
  require 'wovnrb/text_caches/cache_base'
3
2
  require 'wovnrb/text_caches/memory_cache'
4
3
  require 'minitest/autorun'
@@ -6,10 +5,10 @@ require 'timecop'
6
5
 
7
6
  class MemoryCacheTest < Minitest::Test
8
7
  def test_initialize
9
- memory = Wovnrb::MemoryCache.new({
8
+ memory = Wovnrb::MemoryCache.new(
10
9
  'cache_megabytes' => 1,
11
10
  'ttl_seconds' => 1
12
- })
11
+ )
13
12
  option = memory.options
14
13
  assert_equal(1.megabytes, option[:size])
15
14
  assert_equal(1.megabytes, option[:size])
@@ -17,18 +16,18 @@ class MemoryCacheTest < Minitest::Test
17
16
  end
18
17
 
19
18
  def test_initialize_without_cache_megabytes
20
- memory = Wovnrb::MemoryCache.new({
19
+ memory = Wovnrb::MemoryCache.new(
21
20
  'ttl_seconds' => 1
22
- })
21
+ )
23
22
  option = memory.options
24
23
  assert_equal(200.megabytes, option[:size])
25
24
  assert_equal(1.seconds, option[:expires_in])
26
25
  end
27
26
 
28
27
  def test_initialize_without_ttl
29
- memory = Wovnrb::MemoryCache.new({
30
- 'cache_megabytes' => 1,
31
- })
28
+ memory = Wovnrb::MemoryCache.new(
29
+ 'cache_megabytes' => 1
30
+ )
32
31
  option = memory.options
33
32
  assert_equal(1.megabytes, option[:size])
34
33
  assert_equal(300.seconds, option[:expires_in])
@@ -75,9 +74,9 @@ class MemoryCacheTest < Minitest::Test
75
74
 
76
75
  def test_get_with_over_memory
77
76
  # ActiveSupport::Cache::MemoryStore has 240 bytes overhead per instance
78
- memory = Wovnrb::MemoryCache.new({
79
- 'cache_megabytes' => 400.0 / 1000 / 1000,
80
- })
77
+ memory = Wovnrb::MemoryCache.new(
78
+ 'cache_megabytes' => 400.0 / 1000 / 1000
79
+ )
81
80
  memory.put('a', 'c')
82
81
  memory.put('b', 'd')
83
82
  assert_nil(memory.get('a'))
@@ -1,11 +1,5 @@
1
- # -*- coding: utf-8 -*-
2
1
  require 'test_helper'
3
2
  require 'wovnrb'
4
- require 'wovnrb/headers'
5
- require 'minitest/autorun'
6
- require 'webmock/minitest'
7
- require 'rack'
8
- require 'pry'
9
3
 
10
4
  class WovnrbTest < Minitest::Test
11
5
  def setup
@@ -17,225 +11,77 @@ class WovnrbTest < Minitest::Test
17
11
  refute_nil(i)
18
12
  end
19
13
 
20
-
21
- # def test_call(env)
22
- # end
23
-
24
-
25
- # def test_switch_lang(body, values, url, lang=STORE.settings['default_lang'], headers)
26
- # end
27
-
28
- def test_api_call
29
- settings = Wovnrb.get_settings
30
- token = settings['project_token']
31
- url = 'wovn.io/dashboard'
32
- stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
33
- to_return(:body => '{"test_body": "a"}')
34
-
35
- i = Wovnrb::Interceptor.new(get_app, settings)
36
-
37
- i.call(Wovnrb.get_env)
38
- assert_requested(stub, :times => 1)
39
- end
40
-
41
- def test_api_call_with_cache
42
- settings = Wovnrb.get_settings
43
- token = settings['project_token']
44
- url = 'wovn.io/dashboard'
45
- stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
46
- to_return(:body => '{"test_body": "a"}')
47
-
48
- i = Wovnrb::Interceptor.new(get_app, settings)
49
-
50
- i.call(Wovnrb.get_env)
51
- i.call(Wovnrb.get_env)
52
- assert_requested(stub, :times => 1)
53
- end
54
-
55
- def test_request_wovn_token
56
- settings = Wovnrb.get_settings
57
- default_token = 'token0'
58
- request_token = 'token1'
59
- settings['project_token'] = default_token
60
- url = 'wovn.io/dashboard'
61
- stub = stub_request(:get, "#{settings['api_url']}?token=#{request_token}&url=#{url}").
62
- to_return(:body => '{"test_body": "a"}')
63
-
64
- app = get_app(:params => {'wovn_token' => request_token})
65
- i = Wovnrb::Interceptor.new(app, settings)
66
-
67
- env = Wovnrb.get_env
68
- i.call(env)
69
- assert_requested(stub, :times => 1)
70
-
71
- # check use default a token after use dynamic token
72
- stub_default = stub_request(:get, "#{settings['api_url']}?token=#{default_token}&url=#{url}").
73
- to_return(:body => '{"test_body": "a"}')
74
- app.params.clear
75
- env['rack.request.query_hash'] = {}
76
- i.call(env)
77
- assert_requested(stub_default, :times => 1)
78
- end
79
-
80
- def test_request_invalid_wovn_token
81
- settings = Wovnrb.get_settings
82
- settings['project_token'] = 'token0'
83
- request_token = 'invalidtoken1'
84
- url = 'wovn.io/dashboard'
85
- stub = stub_request(:get, "#{settings['api_url']}?token=#{settings['project_token']}&url=#{url}").
86
- to_return(:body => '{"test_body": "a"}')
87
-
88
- i = Wovnrb::Interceptor.new(get_app(:params => {'wovn_token' => settings['project_token']}), settings)
89
-
90
- env = Wovnrb.get_env
91
- i.call(env)
92
- assert_requested(stub, :times => 1)
93
- end
94
-
95
- def test_request_wovn_ignore_paths
96
- settings = Wovnrb.get_settings
97
- url = 'wovn.io/dashboard'
98
- stub = stub_request(:get, "#{settings['api_url']}?token=#{settings['project_token']}&url=#{url}").
99
- to_return(:body => '{"test_body": "a"}')
100
-
101
- app = get_app(:params => {'wovn_ignore_paths' => ['/dashboard']})
102
- i = Wovnrb::Interceptor.new(app, settings)
103
-
104
- env = Wovnrb.get_env
105
- i.call(env)
106
- assert_requested(stub, :times => 0)
107
- end
108
-
109
- def test_request_wovn_disable
110
- settings = Wovnrb.get_settings
111
- token = settings['project_token']
112
- url = 'wovn.io/dashboard'
113
- stub = stub_request(:get, "#{settings['api_url']}?token=#{token}&url=#{url}").
114
- to_return(:body => '{"test_body": "a"}')
115
-
116
- i = Wovnrb::Interceptor.new(get_app(:params => {'wovn_disable' => true}), settings)
117
-
118
- env = Wovnrb.get_env
119
- i.call(env)
120
- assert_requested(stub, :times => 0)
121
- end
122
-
123
14
  def test_switch_lang
124
- i = Wovnrb::Interceptor.new(get_app)
125
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
126
- body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
127
- <div><p>Hello</p></div>
128
- </body></html>"
129
- values = generate_values
130
- url = h.url
131
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
132
-
133
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
134
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>
135
- </body></html>
136
- "
137
- assert_equal([expected_body], swapped_body)
138
- end
15
+ body = '<html lang="ja"><body><h1>Mr. Belvedere Fan Club</h1><div><p>Hello</p></div></body></html>'
139
16
 
140
- def test_switch_lang_of_html_fragment_with_no_translation_values
141
- i = Wovnrb::Interceptor.new(get_app)
142
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
143
- body = '<select name="test"><option value="1">1</option><option value="2">2</option></select>'
144
- values = {}
145
- url = h.url
146
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
17
+ expected_body = [
18
+ '<html lang="ja"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">',
19
+ "<script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script>",
20
+ '<link rel="alternate" hreflang="ja" href="http://ja.page.com/">',
21
+ '<link rel="alternate" hreflang="en" href="http://page.com/"></head>',
22
+ '<body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>',
23
+ '<div><p><!--wovn-src:Hello-->こんにちは</p></div>',
24
+ '</body></html>'
25
+ ].join
147
26
 
148
- assert_equal([body], swapped_body)
27
+ assert_switch_lang('en', 'ja', body, expected_body, true)
149
28
  end
150
29
 
151
30
  def test_switch_lang_of_html_fragment_with_japanese_translations
152
- i = Wovnrb::Interceptor.new(get_app)
153
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
154
- body = '<span>Hello</span>'
155
- expected_body = '<span><!--wovn-src:Hello-->こんにちは</span>'
156
- values = generate_values
157
- url = h.url
158
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
159
-
160
- assert_equal([expected_body], swapped_body)
31
+ bodies = ['<span>Hello</span>'].join
32
+ expected_bodies = ['<span><!--wovn-src:Hello-->こんにちは</span>'].join
33
+
34
+ assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
161
35
  end
162
36
 
163
37
  def test_switch_lang_splitted_body
164
- i = Wovnrb::Interceptor.new(get_app)
165
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
166
- bodies = ["<html><body><h1>Mr. Belvedere Fan Club</h1>",
167
- "<div><p>Hello</p></div>",
168
- "</body></html>"]
169
- values = generate_values
170
- url = h.url
171
- swapped_body = i.switch_lang(bodies, values, url, 'ja', h)
172
-
173
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1><div><p><!--wovn-src:Hello-->こんにちは</p></div></body></html>
174
- "
175
- assert_equal([expected_body], swapped_body)
38
+ bodies = ['<html><body><h1>Mr. Belvedere Fan Club</h1>',
39
+ '<div><p>Hello</p></div>',
40
+ '</body></html>'].join
41
+ expected_bodies = ["<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;amp;backend=true&amp;amp;currentLang=ja&amp;amp;defaultLang=en&amp;amp;urlPattern=subdomain&amp;amp;langCodeAliases={}&amp;amp;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
42
+
43
+ assert_switch_lang('en', 'ja', bodies, expected_bodies, true)
176
44
  end
177
45
 
178
46
  def test_switch_lang_of_html_fragment_in_splitted_body
179
- i = Wovnrb::Interceptor.new(get_app)
180
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
181
- bodies = ['<select name="test"><option value="1">1</option>',
182
- '<option value="2">2</option></select>']
183
- expected_body = '<select name="test"><option value="1">1</option><option value="2">2</option></select>'
184
- values = generate_values
185
- url = h.url
186
- swapped_body = i.switch_lang(bodies, values, url, 'ja', h)
187
-
188
- assert_equal([expected_body], swapped_body)
47
+ body = ['<select name="test"><option value="1">1</option>',
48
+ '<option value="2">2</option></select>'].join
49
+ expected_body = ['<select name="test"><option value="1">1</option><option value="2">2</option></select>'].join
50
+
51
+ assert_switch_lang('en', 'ja', body, expected_body, true)
189
52
  end
190
53
 
191
54
  def test_switch_lang_missing_values
192
- i = Wovnrb::Interceptor.new(get_app)
193
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
194
- body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
55
+ body = "<html><body><h1>Mr. Belvedere Fan Club</h1>
195
56
  <div><p>Hello</p></div>
196
57
  </body></html>"
197
- values = generate_missing_values
198
- url = h.url
199
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
200
-
201
58
  expected_body = "<html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script></head><body><h1>Mr. Belvedere Fan Club</h1>
202
59
  <div><p>Hello</p></div>
203
60
  </body></html>
204
61
  "
205
- assert_equal([expected_body], swapped_body)
62
+
63
+ assert_switch_lang('en', 'ja', body, expected_body, true)
206
64
  end
207
65
 
208
66
  def test_switch_lang_on_fragment_with_translate_fragment_false
209
- Wovnrb::Store.instance.settings['translate_fragment'] = false
210
- i = Wovnrb::Interceptor.new(get_app)
211
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
212
- body = "<h1>Mr. Belvedere Fan Club</h1>
67
+ body = "<h1>Mr. Belvedere Fan Club</h1>
213
68
  <div><p>Hello</p></div>"
214
- values = generate_values
215
- url = h.url
216
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
217
69
 
218
- assert_equal([body], swapped_body)
70
+ Wovnrb::Store.instance.settings['translate_fragment'] = false
71
+ assert_switch_lang('en', 'ja', body, body, false)
219
72
  end
220
73
 
221
74
  def test_switch_lang_on_fragment_with_translate_fragment_true
222
- Wovnrb::Store.instance.settings['translate_fragment'] = true
223
- i = Wovnrb::Interceptor.new(get_app)
224
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
225
- body = "<h1>Mr. Belvedere Fan Club</h1>
75
+ body = "<h1>Mr. Belvedere Fan Club</h1>
226
76
  <div><p>Hello</p></div>"
227
- values = generate_values
228
- url = h.url
229
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
230
77
  expected_body = "<h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
231
78
  <div><p><!--wovn-src:Hello-->こんにちは</p></div>"
232
79
 
233
- assert_equal([expected_body], swapped_body)
80
+ Wovnrb::Store.instance.settings['translate_fragment'] = true
81
+ assert_switch_lang('en', 'ja', body, expected_body, true)
234
82
  end
235
83
 
236
84
  def test_switch_lang_ignores_amp
237
- interceptor = Wovnrb::Interceptor.new(get_app)
238
- headers = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
239
85
  body = <<HTML
240
86
  <html amp>
241
87
  <head><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
@@ -245,25 +91,11 @@ class WovnrbTest < Minitest::Test
245
91
  </body>
246
92
  </html>
247
93
  HTML
248
- expected_body = <<HTML
249
- <html amp=""><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><noscript><style amp-boilerplate>body{-webkit-animation:none;-moz-animation:none;-ms-animation:none;animation:none}</style></noscript></head>
250
- <body>
251
- <h1>Mr. Belvedere Fan Club</h1>
252
- <div><p>Hello</p></div>
253
-
254
-
255
- </body></html>
256
- HTML
257
- values = generate_values
258
- url = headers.url
259
- swapped_bodies = interceptor.switch_lang([body], values, url, 'ja', headers)
260
94
 
261
- assert_equal([expected_body], swapped_bodies)
95
+ assert_switch_lang('en', 'ja', body, body, false)
262
96
  end
263
97
 
264
98
  def test_switch_lang_ignores_amp_defined_with_symbol_attribute
265
- interceptor = Wovnrb::Interceptor.new(get_app)
266
- headers = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
267
99
  body = <<HTML
268
100
  <html ⚡>
269
101
  <body>
@@ -272,132 +104,67 @@ HTML
272
104
  </body>
273
105
  </html>
274
106
  HTML
275
- expected_body = <<HTML
276
- <html ⚡=""><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"></head><body>
277
- <h1>Mr. Belvedere Fan Club</h1>
278
- <div><p>Hello</p></div>
279
-
280
-
281
- </body></html>
282
- HTML
283
- values = generate_values
284
- url = headers.url
285
- swapped_bodies = interceptor.switch_lang([body], values, url, 'ja', headers)
286
107
 
287
- assert_equal([expected_body], swapped_bodies)
108
+ assert_switch_lang('en', 'ja', body, body, false)
288
109
  end
289
110
 
290
- def test_switch_lang_with_noscript_in_head
291
- i = Wovnrb::Interceptor.new(get_app)
292
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
293
- body = "<html><head><noscript><div>test</div></noscript></head><body><h1>Mr. Belvedere Fan Club</h1>
294
- <div><p>Hello</p></div>
295
- </body></html>"
296
- values = generate_values
297
- url = h.url
298
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
111
+ private
299
112
 
300
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><noscript><div>test</div></noscript><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
301
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>
302
- </body></html>
303
- "
304
- assert_equal([expected_body], swapped_body)
305
- end
113
+ def assert_switch_lang(original_lang, target_lang, body, expected_body, api_expected = true)
114
+ subdomain = target_lang == original_lang ? '' : "#{target_lang}."
115
+ interceptor = Wovnrb::Interceptor.new(get_app)
306
116
 
307
- def test_switch_lang_with_multiline_noscript_in_head
308
- i = Wovnrb::Interceptor.new(get_app)
309
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
310
- body = "<html><head><noscript>
311
- <div>test</div>
312
- </noscript></head><body><h1>Mr. Belvedere Fan Club</h1>
313
- <div><p>Hello</p></div>
314
- </body></html>"
315
- values = generate_values
316
- url = h.url
317
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
318
-
319
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><noscript>
320
- <div>test</div>
321
- </noscript><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
322
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>
323
- </body></html>
324
- "
325
- assert_equal([expected_body], swapped_body)
326
- end
117
+ store, headers = store_headers_factory(subdomain, original_lang)
118
+ if api_expected
119
+ dom = Wovnrb::Helpers::NokogumboHelper.parse_html(body)
120
+ converter = Wovnrb::HtmlConverter.new(dom, store, headers)
121
+ apified_body, = converter.build_api_compatible_html
122
+ mock_translation_api_response(apified_body, expected_body)
123
+ end
327
124
 
328
- def test_switch_lang_with_multiple_noscript_in_head
329
- i = Wovnrb::Interceptor.new(get_app)
330
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
331
- body = "<html><head><noscript><div>test</div></noscript><title>plop</title><noscript><div>test2</div></noscript></head><body><h1>Mr. Belvedere Fan Club</h1>
332
- <div><p>Hello</p></div>
333
- </body></html>"
334
- values = generate_values
335
- url = h.url
336
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
125
+ actual_bodies = interceptor.switch_lang(headers, [body])
337
126
 
338
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><noscript><div>test</div></noscript><title>plop</title><noscript><div>test2</div></noscript><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
339
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>
340
- </body></html>
341
- "
342
- assert_equal([expected_body], swapped_body)
127
+ assert_same_elements([expected_body], actual_bodies)
343
128
  end
344
129
 
345
- def test_switch_lang_with_noscript_in_head_and_comment_inside
346
- i = Wovnrb::Interceptor.new(get_app)
347
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
348
- body = "<html><head><noscript><!-- --><div>test</div></noscript></head><body><h1>Mr. Belvedere Fan Club</h1>
349
- <div><p>Hello</p></div>
350
- </body></html>"
351
- values = generate_values
352
- url = h.url
353
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
354
-
355
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><noscript><!-- --><div>test</div></noscript><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
356
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>
357
- </body></html>
358
- "
359
- assert_equal([expected_body], swapped_body)
360
- end
130
+ def store_headers_factory(subdomain, original_lang)
131
+ settings = {
132
+ 'project_token' => '123456',
133
+ 'custom_lang_aliases' => {},
134
+ 'default_lang' => original_lang,
135
+ 'url_pattern' => 'subdomain',
136
+ 'url_pattern_reg' => '^(?<lang>[^.]+).'
137
+ }
361
138
 
362
- def test_switch_lang_with_commented_noscript_in_head
363
- i = Wovnrb::Interceptor.new(get_app)
364
- h = Wovnrb::Headers.new(Wovnrb.get_env('url' => 'http://page.com'), Wovnrb.get_settings('url_pattern' => 'subdomain', 'url_pattern_reg' => '^(?<lang>[^.]+).'))
365
- body = "<html><head><!--<noscript><div>test</div></noscript>--></head><body><h1>Mr. Belvedere Fan Club</h1>
366
- <div><p>Hello</p></div>
367
- </body></html>"
368
- values = generate_values
369
- url = h.url
370
- swapped_body = i.switch_lang([body], values, url, 'ja', h)
139
+ store = Wovnrb::Store.instance
140
+ store.update_settings(settings)
371
141
 
372
- expected_body = "<html lang=\"ja\"><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"><script src=\"//j.wovn.io/1\" async=\"true\" data-wovnio=\"key=&amp;backend=true&amp;currentLang=ja&amp;defaultLang=en&amp;urlPattern=path&amp;langCodeAliases={}&amp;version=#{Wovnrb::VERSION}\"> </script><!--<noscript><div>test</div></noscript>--><link rel=\"alternate\" hreflang=\"ja\" href=\"http://ja.page.com/\"><link rel=\"alternate\" hreflang=\"en\" href=\"http://page.com/\"></head><body><h1><!--wovn-src:Mr. Belvedere Fan Club-->ベルベデアさんファンクラブ</h1>
373
- <div><p><!--wovn-src:Hello-->こんにちは</p></div>
374
- </body></html>
375
- "
376
- assert_equal([expected_body], swapped_body)
377
- end
142
+ headers = Wovnrb::Headers.new(
143
+ Wovnrb.get_env('url' => "http://#{subdomain}page.com"),
144
+ Wovnrb.get_settings(settings)
145
+ )
378
146
 
379
- def get_app(opts={})
380
- RackMock.new(opts)
147
+ [store, headers]
381
148
  end
382
149
 
383
- def generate_values
384
- values = {}
385
- values['text_vals'] = {'Hello' => {'ja' => [{'data' => 'こんにちは'}]},
386
- 'Mr. Belvedere Fan Club' => {'ja' => [{'data' => 'ベルベデアさんファンクラブ'}]}}
387
- return values
150
+ def mock_translation_api_response(body, expected_body)
151
+ Wovnrb::ApiTranslator.any_instance
152
+ .expects(:translate)
153
+ .once
154
+ .with(body)
155
+ .returns(expected_body)
388
156
  end
389
157
 
390
- def generate_missing_values
391
- # 'values' will be an empty object whenever API request has status != 200
392
- return {}
158
+ def get_app(opts = {})
159
+ RackMock.new(opts)
393
160
  end
394
161
 
395
162
  class RackMock
396
163
  attr_accessor :params
397
164
 
398
- def initialize(opts={})
165
+ def initialize(opts = {})
399
166
  @params = {}
400
- if opts.has_key?(:params) && opts[:params].class == Hash
167
+ if opts.key?(:params) && opts[:params].class == Hash
401
168
  opts[:params].each do |key, val|
402
169
  @params[key] = val
403
170
  end
@@ -407,13 +174,13 @@ HTML
407
174
 
408
175
  def call(env)
409
176
  @env = env
410
- if @params.length > 0
177
+ unless @params.empty?
411
178
  req = Rack::Request.new(@env)
412
179
  @params.each do |key, val|
413
180
  req.update_param(key, val)
414
181
  end
415
182
  end
416
- [200, {'Content-Type' => 'text/html'}, ['']]
183
+ [200, { 'Content-Type' => 'text/html' }, ['']]
417
184
  end
418
185
 
419
186
  def [](key)