wovnrb 3.1.0 → 3.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4d23f8ac05cb8fbefe04b2c8ae96f9932b280762e2491c80552bd20314d17e6d
4
- data.tar.gz: 44118f0db0276085f492b631eeba2af8fe349f6b9d7ef96627590dda63793d80
3
+ metadata.gz: 964d0d3b7325badb2202f50fe61b2a82554138346c183c0784b313c683aa26c8
4
+ data.tar.gz: b622aaf71523728ae235d8ba1d6de060c906970dbc471a0408de8e274a545482
5
5
  SHA512:
6
- metadata.gz: a71cbbd14782608c0c445d146bc262d5dfcf6bd7fca9cbfb467aad6f62caf9f979a98ae29a3e0fee38d77202217960b39e5ac9755975ba1fbd5e3899d5d7e69b
7
- data.tar.gz: 9576ec7bd9d7053be56a116286ee9fe45df7a1f0c9e826dc114b0c77963d5e7c4ae53245655269fa8287cb45aa2ea0e56fdeecdf4cb81e4ce397d7f5595c31ef
6
+ metadata.gz: 3ba07bb7792de8c7e2f8e53128cc69b5e5a22b7cdbeb87ec7d87f7d026fac2a3907f838e9f88234c237b119aed8ee66aa7980d79f86db3391619422ce7254a17
7
+ data.tar.gz: c285d5d65556fa7953345f38c295250546dc041bea372b32ec559d7a44ef9783b8b9c36ced77b24b464dbcccf783ffd2994680e720bb147fe162080c258b20bd
data/.rubocop.yml CHANGED
@@ -1 +1,6 @@
1
+ require: rubocop-performance
2
+
1
3
  inherit_from: .rubocop_todo.yml
4
+
5
+ AllCops:
6
+ SuggestExtensions: false
data/README.en.md CHANGED
@@ -39,8 +39,8 @@ Insert the following into either config/application.rb or config/environments/.
39
39
 
40
40
  config.wovnrb = {
41
41
  :project_token => 'EnS!t3',
42
- :default_lang => 'en',
43
- :supported_langs => ['en'],
42
+ :default_lang => 'ja',
43
+ :supported_langs => ['ja', en'],
44
44
  :url_pattern => 'path'
45
45
  }
46
46
 
@@ -60,8 +60,8 @@ require 'wovnrb'
60
60
 
61
61
  use Wovnrb::Interceptor, {
62
62
  :project_token => 'EnS!t3',
63
- :default_lang => 'en',
64
- :supported_langs => ['en'],
63
+ :default_lang => 'ja',
64
+ :supported_langs => ['ja', 'en'],
65
65
  :url_pattern => 'path'
66
66
  }
67
67
 
@@ -74,18 +74,19 @@ After completing setup, start the Ruby Application, and make sure the WOVN.io li
74
74
 
75
75
  The following is a list of the WOVN.io Ruby Library's valid parameters.
76
76
 
77
- Parameter Name | Required | Default Setting
78
- ------------------ | -------- | ----------------
79
- project_token | yes | ''
80
- default_lang | yes | 'en'
81
- supported_langs | yes | ['en']
82
- url_pattern | yes | 'path'
83
- lang_param_name | | 'wovn'
84
- query | | []
85
- ignore_class | | []
86
- translate_fragment | | true
87
- ignore_paths | | []
88
- install_middleware | | true
77
+ Parameter Name | Required | Default Setting
78
+ ----------------------| -------- | ----------------
79
+ project_token | yes | ''
80
+ default_lang | yes | 'ja'
81
+ supported_langs | yes | ['ja', 'en']
82
+ url_pattern | yes | 'path'
83
+ lang_param_name | | 'wovn'
84
+ query | | []
85
+ ignore_class | | []
86
+ translate_fragment | | true
87
+ ignore_paths | | []
88
+ install_middleware | | true
89
+ compress_api_requests | | true
89
90
 
90
91
  ### 2.1. project_token
91
92
 
@@ -196,3 +197,7 @@ WOVN.rb needs to be added after any compression middleware.
196
197
  :install_middleware => false
197
198
  }
198
199
  ```
200
+
201
+ ### 2.11 compress_api_requests
202
+
203
+ By default, requests to the translation API will be sent with gzip compression. Set to false to disable compression.
data/README.ja.md CHANGED
@@ -44,8 +44,8 @@ bundle install
44
44
 
45
45
  config.wovnrb = {
46
46
  :project_token => 'EnS!t3',
47
- :default_lang => 'en',
48
- :supported_langs => ['en'],
47
+ :default_lang => 'ja',
48
+ :supported_langs => ['ja', en'],
49
49
  :url_pattern => 'path'
50
50
  }
51
51
 
@@ -63,8 +63,8 @@ require 'wovnrb'
63
63
 
64
64
  use Wovnrb::Interceptor, {
65
65
  :project_token => 'EnS!t3',
66
- :default_lang => 'en',
67
- :supported_langs => ['en'],
66
+ :default_lang => 'ja',
67
+ :supported_langs => ['ja', 'en'],
68
68
  :url_pattern => 'path'
69
69
  }
70
70
 
@@ -80,8 +80,8 @@ use Wovnrb::Interceptor, {
80
80
  パラメータ名 | 必須 | デフォルト設定
81
81
  ------------------ | -------- | ----------------
82
82
  project_token | yes | ''
83
- default_lang | yes | 'en'
84
- supported_langs | yes | ['en']
83
+ default_lang | yes | 'ja'
84
+ supported_langs | yes | ['ja', 'en']
85
85
  url_pattern | yes | 'path'
86
86
  lang_param_name | | 'wovn'
87
87
  query | | []
@@ -5129,9 +5129,9 @@ path-key@^2.0.0, path-key@^2.0.1:
5129
5129
  integrity sha1-QRyttXTFoUDTpLGRDUDYDMn0C0A=
5130
5130
 
5131
5131
  path-parse@^1.0.6:
5132
- version "1.0.6"
5133
- resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.6.tgz#d62dbb5679405d72c4737ec58600e9ddcf06d24c"
5134
- integrity sha512-GSmOT2EbHrINBf9SR7CDELwlJ8AENk3Qn7OikK4nFYAu3Ote2+JYNVvkpAEQm3/TLNEJFD/xZJjzyxg3KBWOzw==
5132
+ version "1.0.7"
5133
+ resolved "https://registry.yarnpkg.com/path-parse/-/path-parse-1.0.7.tgz#fbc114b60ca42b30d9daf5858e4bd68bbedb6735"
5134
+ integrity sha512-LDJzPVEEEPR+y48z93A0Ed0yXb8pAByGWo/k5YYdYgpY2/2EsOsksJrq7lOHxryrVOn1ejG6oAp8ahvOIQD8sw==
5135
5135
 
5136
5136
  path-to-regexp@0.1.7:
5137
5137
  version "0.1.7"
@@ -5,9 +5,10 @@ require 'zlib'
5
5
 
6
6
  module Wovnrb
7
7
  class ApiTranslator
8
- def initialize(store, headers)
8
+ def initialize(store, headers, uuid)
9
9
  @store = store
10
10
  @headers = headers
11
+ @uuid = uuid
11
12
  end
12
13
 
13
14
  def translate(body)
@@ -23,18 +24,19 @@ module Wovnrb
23
24
 
24
25
  case response
25
26
  when Net::HTTPSuccess
26
- if response.header['Content-Encoding'] == 'gzip'
27
- response_body = Zlib::GzipReader.new(StringIO.new(response.body)).read
28
-
29
- JSON.parse(response_body)['body'] || body
30
- elsif @store.dev_mode?
31
- JSON.parse(response.body)['body'] || body
32
- else
33
- WovnLogger.error("Received invalid content (\"#{response.header['Content-Encoding']}\") from WOVNio translation API.")
27
+ begin
28
+ raw_response_body = @store.dev_mode? ? response.body : Zlib::GzipReader.new(StringIO.new(response.body)).read
29
+ rescue Zlib::GzipFile::Error
30
+ raw_response_body = response.body
31
+ end
32
+
33
+ begin
34
+ JSON.parse(raw_response_body)['body'] || body
35
+ rescue JSON::JSONError
34
36
  body
35
37
  end
36
38
  else
37
- WovnLogger.error("Received \"#{response.message}\" from WOVNio translation API.")
39
+ WovnLogger.error("HTML-swapper call failed. Received \"#{response.message}\" from WOVNio translation API.")
38
40
  body
39
41
  end
40
42
  end
@@ -51,24 +53,45 @@ module Wovnrb
51
53
  end
52
54
 
53
55
  def prepare_request(body)
54
- data = compress_request_data(generate_request_data(body))
55
- headers = {
56
- 'Accept-Encoding' => 'gzip',
57
- 'Content-Type' => 'application/octet-stream',
58
- 'Content-Length' => data.bytesize.to_s
59
- }
60
- request = Net::HTTP::Post.new(generate_request_path(body), headers)
56
+ if @store.compress_api_requests?
57
+ gzip_request(body)
58
+ else
59
+ json_request(body)
60
+ end
61
+ end
62
+
63
+ def gzip_request(html_body)
64
+ api_params = build_api_params(html_body)
65
+ compressed_body = compress_request_data(api_params)
66
+ request = Net::HTTP::Post.new(request_path(html_body), {
67
+ 'Accept-Encoding' => 'gzip',
68
+ 'Content-Type' => 'application/octet-stream',
69
+ 'Content-Encoding' => 'gzip',
70
+ 'Content-Length' => compressed_body.bytesize.to_s,
71
+ 'X-Request-Id' => @uuid
72
+ })
73
+ request.body = compressed_body
74
+
75
+ request
76
+ end
61
77
 
62
- request.body = data
78
+ def json_request(html_body)
79
+ api_params = build_api_params(html_body)
80
+ request = Net::HTTP::Post.new(request_path(html_body), {
81
+ 'Accept-Encoding' => 'gzip',
82
+ 'Content-Type' => 'application/json',
83
+ 'X-Request-Id' => @uuid
84
+ })
85
+ request.body = api_params.to_json
63
86
 
64
87
  request
65
88
  end
66
89
 
67
- def generate_request_path(body)
68
- "#{api_uri.path.sub(/\/$/, '')}/translation?cache_key=#{generate_cache_key(body)}"
90
+ def request_path(body)
91
+ "#{api_uri.path}/translation?cache_key=#{cache_key(body)}"
69
92
  end
70
93
 
71
- def generate_cache_key(body)
94
+ def cache_key(body)
72
95
  cache_key_components = {
73
96
  'token' => token,
74
97
  'settings_hash' => settings_hash,
@@ -81,8 +104,8 @@ module Wovnrb
81
104
  CGI.escape("(#{cache_key_components})")
82
105
  end
83
106
 
84
- def generate_request_data(body)
85
- data = {
107
+ def build_api_params(body)
108
+ result = {
86
109
  'url' => page_url,
87
110
  'token' => token,
88
111
  'lang_code' => lang_code,
@@ -93,9 +116,9 @@ module Wovnrb
93
116
  'body' => body
94
117
  }
95
118
 
96
- data['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
119
+ result['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
97
120
 
98
- data
121
+ result
99
122
  end
100
123
 
101
124
  def compress_request_data(data_hash)
@@ -109,7 +132,7 @@ module Wovnrb
109
132
  end
110
133
 
111
134
  def api_uri
112
- Addressable::URI.parse("#{@store.settings['api_url']}/v0/")
135
+ Addressable::URI.parse("#{@store.settings['api_url']}/v0")
113
136
  end
114
137
 
115
138
  def api_timeout
@@ -18,11 +18,11 @@ module Wovnrb
18
18
  end
19
19
  unless @env.key?('REQUEST_URI')
20
20
  # Add '/' to PATH_INFO as a possible fix for pow server
21
- @env['REQUEST_URI'] = (@env['PATH_INFO'] =~ /^[^\/]/ ? '/' : '') + @env['PATH_INFO'] + (@env['QUERY_STRING'].empty? ? '' : "?#{@env['QUERY_STRING']}")
21
+ @env['REQUEST_URI'] = (/^[^\/]/.match?(@env['PATH_INFO']) ? '/' : '') + @env['PATH_INFO'] + (@env['QUERY_STRING'].empty? ? '' : "?#{@env['QUERY_STRING']}")
22
22
  end
23
23
  # REQUEST_URI is expected to not contain the server name
24
24
  # heroku contains http://...
25
- @env['REQUEST_URI'] = @env['REQUEST_URI'].sub(/^https?:\/\/[^\/]+/, '') if @env['REQUEST_URI'] =~ /^https?:\/\//
25
+ @env['REQUEST_URI'] = @env['REQUEST_URI'].sub(/^https?:\/\/[^\/]+/, '') if /^https?:\/\//.match?(@env['REQUEST_URI'])
26
26
  @unmasked_pathname = @env['REQUEST_URI'].split('?')[0]
27
27
  @unmasked_pathname += '/' unless @unmasked_pathname =~ /\/$/ || @unmasked_pathname =~ /\/[^\/.]+\.[^\/.]+$/
28
28
  @unmasked_url = "#{@protocol}://#{@unmasked_host}#{@unmasked_pathname}"
@@ -108,7 +108,7 @@ module Wovnrb
108
108
  case @settings['url_pattern']
109
109
  when 'query'
110
110
  lang_param_name = @settings['lang_param_name']
111
- location = if location =~ /\?/
111
+ location = if /\?/.match?(location)
112
112
  "#{location}&#{lang_param_name}=#{lang_code}"
113
113
  else
114
114
  "#{location}?#{lang_param_name}=#{lang_code}"
@@ -180,7 +180,7 @@ module Wovnrb
180
180
  if lang_code != @settings['default_lang'] && headers.key?('Location') && headers['Location'] =~ r && !@settings['ignore_globs'].ignore?(headers['Location'])
181
181
  case @settings['url_pattern']
182
182
  when 'query'
183
- headers['Location'] += if headers['Location'] =~ /\?/
183
+ headers['Location'] += if /\?/.match?(headers['Location'])
184
184
  '&'
185
185
  else
186
186
  '?'
@@ -2,7 +2,7 @@ module Wovnrb
2
2
  module Helpers
3
3
  module NokogumboHelper
4
4
  def parse_html(html_string, encoding = 'UTF-8')
5
- if html_string.strip[0..999] =~ /<html/i
5
+ if /<html/i.match?(html_string.strip[0..999])
6
6
  d = Nokogiri::HTML5(html_string)
7
7
  d.encoding = encoding
8
8
  d
data/lib/wovnrb/lang.rb CHANGED
@@ -126,7 +126,7 @@ module Wovnrb
126
126
  # @param [Wovnrb::Header] headers instance of Wovn::Header. It generates new env variable for original request.
127
127
  # @return [String] URL added langauge code.
128
128
  def add_lang_code(href, pattern, headers)
129
- return href if href =~ /^(#.*)?$/
129
+ return href if /^(#.*)?$/.match?(href)
130
130
 
131
131
  settings = Store.instance.settings
132
132
  code_to_add = settings['custom_lang_aliases'][@lang_code] || @lang_code
@@ -181,7 +181,7 @@ module Wovnrb
181
181
  when 'query'
182
182
  new_href = add_query_lang_code(href, code_to_add, lang_param_name)
183
183
  else # path
184
- if href =~ /^\//
184
+ if /^\//.match?(href)
185
185
  new_href = "/#{code_to_add}#{href}"
186
186
  else
187
187
  current_dir = headers.pathname.sub(/[^\/]*\.[^.]{2,6}$/, '')
@@ -250,7 +250,7 @@ module Wovnrb
250
250
  end
251
251
 
252
252
  def add_query_lang_code(href, lang_code, lang_param_name)
253
- query_separator = href =~ /\?/ ? '&' : '?'
253
+ query_separator = /\?/.match?(href) ? '&' : '?'
254
254
 
255
255
  href.sub(/(#|$)/, "#{query_separator}#{lang_param_name}=#{lang_code}\\1")
256
256
  end
@@ -21,6 +21,10 @@ module Wovnrb
21
21
  private
22
22
 
23
23
  def html
24
+ # Ensure a Content-Type declaration in the header. This mimics Nokogumbo
25
+ # 1.5.0 default serialization behavior.
26
+ @dom.meta_encoding = 'UTF-8' if @dom.respond_to?(:meta_encoding=)
27
+
24
28
  @dom.to_html(save_with: 0).strip
25
29
  end
26
30
 
@@ -3,15 +3,22 @@ require 'logger' unless defined?(Logger)
3
3
 
4
4
  module Wovnrb
5
5
  class WovnLogger
6
+ attr_reader :uuid
7
+
6
8
  include Singleton
7
9
 
8
10
  class << self
9
11
  def error(message)
10
12
  instance.error(message)
11
13
  end
14
+
15
+ def uuid
16
+ instance.uuid
17
+ end
12
18
  end
13
19
 
14
20
  def initialize
21
+ @uuid = SecureRandom.uuid
15
22
  path = Store.instance.settings['log_path']
16
23
  if path
17
24
  begin
@@ -40,9 +47,9 @@ module Wovnrb
40
47
 
41
48
  def error(message)
42
49
  if @logger == $stderr
43
- @logger.puts "Wovnrb Error: #{message}"
50
+ @logger.puts "[#{@uuid}] Wovnrb Error: #{message}"
44
51
  else
45
- @logger.error message
52
+ @logger.error "[#{@uuid}] #{message}"
46
53
  end
47
54
  end
48
55
  end
data/lib/wovnrb/store.rb CHANGED
@@ -25,8 +25,8 @@ module Wovnrb
25
25
  'ignore_class' => [],
26
26
  'api_url' => 'https://wovn.global.ssl.fastly.net',
27
27
  'api_timeout_seconds' => 1.0,
28
- 'default_lang' => 'en',
29
- 'supported_langs' => ['en'],
28
+ 'default_lang' => 'ja',
29
+ 'supported_langs' => %w[en ja],
30
30
  'test_mode' => false,
31
31
  'test_url' => '',
32
32
  'cache_megabytes' => nil,
@@ -35,7 +35,8 @@ module Wovnrb
35
35
  'custom_lang_aliases' => {},
36
36
  'translate_fragment' => true,
37
37
  'widget_url' => 'https://j.wovn.io/1',
38
- 'wovn_dev_mode' => false
38
+ 'wovn_dev_mode' => false,
39
+ 'compress_api_requests' => true
39
40
  )
40
41
  end
41
42
 
@@ -181,6 +182,10 @@ module Wovnrb
181
182
  @settings['supported_langs'] || []
182
183
  end
183
184
 
185
+ def compress_api_requests?
186
+ @settings['compress_api_requests']
187
+ end
188
+
184
189
  def widget_url
185
190
  @settings['widget_url'] || 'https://j.wovn.io/1'
186
191
  end
@@ -1,3 +1,3 @@
1
1
  module Wovnrb
2
- VERSION = '3.1.0'.freeze
2
+ VERSION = '3.4.1'.freeze
3
3
  end
data/lib/wovnrb.rb CHANGED
@@ -5,7 +5,7 @@ require 'wovnrb/store'
5
5
  require 'wovnrb/lang'
6
6
  require 'wovnrb/services/html_converter'
7
7
  require 'wovnrb/services/html_replace_marker'
8
- require 'nokogumbo'
8
+ require 'nokogiri'
9
9
  require 'active_support'
10
10
  require 'json'
11
11
  require 'wovnrb/helpers/nokogumbo_helper'
@@ -49,7 +49,7 @@ module Wovnrb
49
49
  status, res_headers, body = @app.call(headers.request_out)
50
50
 
51
51
  # disabled by next Rack middleware
52
- return output(headers, status, res_headers, body) unless res_headers['Content-Type'] =~ /html/
52
+ return output(headers, status, res_headers, body) unless /html/.match?(res_headers['Content-Type'])
53
53
 
54
54
  request = Rack::Request.new(env)
55
55
 
@@ -58,7 +58,7 @@ module Wovnrb
58
58
  @store.settings.update_dynamic_settings!(request.params)
59
59
  return output(headers, status, res_headers, body) if ignore_path?(headers.pathname)
60
60
 
61
- body = switch_lang(headers, body) unless status.to_s =~ /^1|302/
61
+ body = switch_lang(headers, body) unless /^1|302/.match?(status.to_s)
62
62
 
63
63
  content_length = 0
64
64
  body.each { |b| content_length += b.respond_to?(:bytesize) ? b.bytesize : 0 }
@@ -80,7 +80,7 @@ module Wovnrb
80
80
 
81
81
  if needs_api?(html_body, headers)
82
82
  converted_html, marker = html_converter.build_api_compatible_html
83
- translated_content = ApiTranslator.new(@store, headers).translate(converted_html)
83
+ translated_content = ApiTranslator.new(@store, headers, WovnLogger.uuid).translate(converted_html)
84
84
  translated_body.push(marker.revert(translated_content))
85
85
  else
86
86
  string_body = html_converter.build if html_body.html?
@@ -1,6 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
3
  module Wovnrb
4
+ REQUEST_UUID = 'ABCD'.freeze
5
+
4
6
  class ApiTranslatorTest < WovnMiniTest
5
7
  def test_translate
6
8
  assert_translation('test.html', 'test_translated.html', true)
@@ -8,28 +10,64 @@ module Wovnrb
8
10
 
9
11
  def test_translate_falls_back_to_original_body_if_exception
10
12
  Net::HTTP.any_instance.expects(:request).raises
11
- assert_translation('test.html', 'test_translated.html', false, nil)
13
+ assert_translation('test.html', 'test_translated.html', false, response: nil)
12
14
  end
13
15
 
14
16
  def test_translate_falls_back_to_original_body_if_api_error
15
- assert_translation('test.html', 'test_translated.html', false, status_code: 500)
17
+ assert_translation('test.html', 'test_translated.html', false, response: { encoding: 'text/json', status_code: 500 })
18
+ end
19
+
20
+ def test_translate_continues_if_api_response_is_not_compressed
21
+ assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'unknown', status: 200 }, compress_data: false)
16
22
  end
17
23
 
18
- def test_translate_falls_back_to_original_body_if_api_response_is_not_compressed
19
- assert_translation('test.html', 'test_translated.html', false, encoding: 'unknown')
24
+ def test_translate_continues_if_api_response_is_compressed
25
+ assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'unknown', status: 200 })
20
26
  end
21
27
 
22
28
  def test_translate_accepts_uncompressed_response_from_api_in_dev_mode
23
29
  Wovnrb::Store.instance.update_settings('wovn_dev_mode' => true)
24
- assert_translation('test.html', 'test_translated.html', true, encoding: 'text/json')
30
+ assert_translation('test.html', 'test_translated.html', true, response: { encoding: 'text/json', status: 200 }, compress_data: false)
31
+ end
32
+
33
+ def test_translate_without_api_compression_sends_json
34
+ Wovnrb::Store.instance.update_settings('compress_api_requests' => false)
35
+ sut, _store, _headers = create_sut
36
+ html_body = 'foo'
37
+
38
+ stub_request(:post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*})
39
+ .to_return(status: 200, body: { 'body' => 'translated_body' }.to_json)
40
+
41
+ sut.translate(html_body)
42
+
43
+ assert_requested :post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*},
44
+ headers: {
45
+ 'Accept' => '*/*',
46
+ 'Accept-Encoding' => 'gzip',
47
+ 'Content-Type' => 'application/json',
48
+ 'User-Agent' => 'Ruby',
49
+ 'X-Request-Id' => REQUEST_UUID
50
+ },
51
+ body: {
52
+ 'url' => 'http://wovn.io/test',
53
+ 'token' => '123456',
54
+ 'lang_code' => 'fr',
55
+ 'url_pattern' => 'subdomain',
56
+ 'lang_param_name' => 'lang',
57
+ 'product' => 'WOVN.rb',
58
+ 'version' => VERSION,
59
+ 'body' => 'foo',
60
+ 'custom_lang_aliases' => { 'ja' => 'Japanese' }.to_json
61
+ }.to_json,
62
+ times: 1
25
63
  end
26
64
 
27
65
  private
28
66
 
29
- def assert_translation(original_html_fixture, translated_html_fixture, success_expected, response = { encoding: 'gzip', status_code: 200 })
67
+ def assert_translation(original_html_fixture, translated_html_fixture, success_expected, response: { encoding: 'gzip', status_code: 200 }, compress_data: true)
30
68
  original_html = File.read("test/fixtures/html/#{original_html_fixture}")
31
69
  translated_html = File.read("test/fixtures/html/#{translated_html_fixture}")
32
- actual_translated_html = translate(original_html, translated_html, response)
70
+ actual_translated_html = translate(original_html, translated_html, response, compress_data: compress_data)
33
71
 
34
72
  if success_expected
35
73
  assert_equal(actual_translated_html, translated_html)
@@ -38,7 +76,16 @@ module Wovnrb
38
76
  end
39
77
  end
40
78
 
41
- def translate(original_html, translated_html, response)
79
+ def translate(original_html, translated_html, response, compress_data: true)
80
+ api_translator, store, _headers = create_sut
81
+ translation_request_stub = stub_translation_api_request(store, original_html, translated_html, response, compress_data: compress_data)
82
+
83
+ actual_translated_html = api_translator.translate(original_html)
84
+ assert_requested(translation_request_stub, times: 1) if translation_request_stub
85
+ actual_translated_html
86
+ end
87
+
88
+ def create_sut
42
89
  settings = {
43
90
  'project_token' => '123456',
44
91
  'custom_lang_aliases' => { 'ja' => 'Japanese' },
@@ -53,15 +100,12 @@ module Wovnrb
53
100
  Wovnrb.get_env('url' => 'http://fr.wovn.io/test'),
54
101
  Wovnrb.get_settings(settings)
55
102
  )
56
- api_translator = ApiTranslator.new(store, headers)
57
- translation_request_stub = stub_translation_api_request(store, headers, original_html, translated_html, response)
103
+ api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
58
104
 
59
- actual_translated_html = api_translator.translate(original_html)
60
- assert_requested(translation_request_stub, times: 1) if translation_request_stub
61
- actual_translated_html
105
+ [api_translator, store, headers]
62
106
  end
63
107
 
64
- def stub_translation_api_request(store, headers, original_html, translated_html, response)
108
+ def stub_translation_api_request(store, original_html, translated_html, response, compress_data: true)
65
109
  if response
66
110
  cache_key = generate_cache_key(store, original_html)
67
111
  api_host = if store.dev_mode?
@@ -79,10 +123,10 @@ module Wovnrb
79
123
  'User-Agent' => 'Ruby'
80
124
  }
81
125
  stub_response_json = "{\"body\":\"#{translated_html.gsub("\n", '\n')}\"}"
82
- stub_response = if store.dev_mode?
83
- stub_response_json
84
- else
126
+ stub_response = if compress_data
85
127
  compress(stub_response_json)
128
+ else
129
+ stub_response_json
86
130
  end
87
131
  response_headers = { 'Content-Encoding' => response[:encoding] || 'gzip' }
88
132
  stub_request(:post, api_url)
@@ -170,7 +170,7 @@ module Wovnrb
170
170
  Wovnrb.get_env('url' => 'http://wovn.io/contact', 'HTTP_X_FORWARDED_HOST' => 'wovn.io'),
171
171
  Store.instance.settings
172
172
  )
173
- assert_equal('http://wovn.io/contact?wovn=ja', sut.redirect_location('ja'))
173
+ assert_equal('http://wovn.io/contact?wovn=en', sut.redirect_location('en'))
174
174
  end
175
175
 
176
176
  def test_redirect_location_with_lang_param_name
@@ -179,7 +179,7 @@ module Wovnrb
179
179
  Wovnrb.get_env('url' => 'http://wovn.io/contact', 'HTTP_X_FORWARDED_HOST' => 'wovn.io'),
180
180
  Store.instance.settings
181
181
  )
182
- assert_equal('http://wovn.io/contact?lang=ja', sut.redirect_location('ja'))
182
+ assert_equal('http://wovn.io/contact?lang=en', sut.redirect_location('en'))
183
183
  end
184
184
 
185
185
  #########################
@@ -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=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;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>"
48
+ expected_convert_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&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;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><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=\"https://j.wovn.io/1\" async=\"true\" data-wovnio=\"key=123456&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;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>"
65
+ expected_convert_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&amp;backend=true&amp;currentLang=en&amp;defaultLang=en&amp;urlPattern=query&amp;langCodeAliases={}&amp;langParamName=wovn&amp;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><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
 
@@ -31,9 +31,11 @@ module Wovnrb
31
31
  end
32
32
 
33
33
  def test_error
34
- mock = LogMock.mock_log
35
- WovnLogger.instance.error('aaa')
36
- assert_equal(['aaa'], mock.errors)
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
@@ -78,12 +78,14 @@ module Wovnrb
78
78
  end
79
79
 
80
80
  def test_invalid_settings
81
- mock = LogMock.mock_log
82
- store = Wovnrb::Store.instance
83
- valid = store.valid_settings?
81
+ SecureRandom.stub(:uuid, 'a') do
82
+ mock = LogMock.mock_log
83
+ store = Wovnrb::Store.instance
84
+ valid = store.valid_settings?
84
85
 
85
- assert_equal(false, valid)
86
- assert_equal(['Project token is not valid.'], mock.errors)
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
- mock = LogMock.mock_log
114
- store = Wovnrb::Store.instance
115
- store.update_settings('ignore_paths' => 'aaaa')
116
-
117
- assert_equal(false, store.valid_settings?)
118
- assert_equal(['Project token is not valid.', 'Ignore Paths aaaa should be Array.'], mock.errors)
115
+ SecureRandom.stub(:uuid, 'a') do
116
+ mock = LogMock.mock_log
117
+ store = Wovnrb::Store.instance
118
+ store.update_settings('ignore_paths' => 'aaaa')
119
+
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
data/test/test_helper.rb CHANGED
@@ -14,7 +14,7 @@ SimpleCov.start do
14
14
  track_files 'lib/**/*.rb'
15
15
  end
16
16
 
17
- require 'nokogumbo'
17
+ require 'nokogiri'
18
18
  require 'rack'
19
19
  require 'minitest/autorun'
20
20
  require 'mocha/minitest'
data/wovnrb.gemspec CHANGED
@@ -12,6 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.homepage = 'https://wovn.io'
13
13
  spec.license = 'MIT'
14
14
  spec.required_ruby_version = '>= 2.5'
15
+ spec.metadata['rubygems_mfa_required'] = 'true'
15
16
 
16
17
  files = `git ls-files -z`.split("\x0")
17
18
  files.delete('BEFORE_PUSHING')
@@ -23,8 +24,7 @@ Gem::Specification.new do |spec|
23
24
  spec.add_dependency 'activesupport'
24
25
  spec.add_dependency 'addressable'
25
26
  spec.add_dependency 'lz4-ruby'
26
- spec.add_dependency 'nokogiri', '>= 1.8.1', '<= 1.11'
27
- spec.add_dependency 'nokogumbo', '>= 1.4.0', '< 2.0.0'
27
+ spec.add_dependency 'nokogiri', '>= 1.12', '<2'
28
28
  spec.add_dependency 'rack'
29
29
 
30
30
  spec.add_development_dependency 'bundler', '>= 1.7'
@@ -45,6 +45,7 @@ Gem::Specification.new do |spec|
45
45
 
46
46
  spec.add_development_dependency 'rake-compiler'
47
47
  spec.add_development_dependency 'rubocop'
48
+ spec.add_development_dependency 'rubocop-performance'
48
49
  spec.add_development_dependency 'timecop'
49
50
 
50
51
  # NOTE; it need to use webmock 2.3.2 for avoiding error when we use ruby 2.4.x.
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.1.0
4
+ version: 3.4.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wovn Technologies, Inc.
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-28 00:00:00.000000000 Z
11
+ date: 2021-11-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -58,40 +58,20 @@ dependencies:
58
58
  requirements:
59
59
  - - ">="
60
60
  - !ruby/object:Gem::Version
61
- version: 1.8.1
62
- - - "<="
63
- - !ruby/object:Gem::Version
64
- version: '1.11'
65
- type: :runtime
66
- prerelease: false
67
- version_requirements: !ruby/object:Gem::Requirement
68
- requirements:
69
- - - ">="
70
- - !ruby/object:Gem::Version
71
- version: 1.8.1
72
- - - "<="
73
- - !ruby/object:Gem::Version
74
- version: '1.11'
75
- - !ruby/object:Gem::Dependency
76
- name: nokogumbo
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - ">="
80
- - !ruby/object:Gem::Version
81
- version: 1.4.0
61
+ version: '1.12'
82
62
  - - "<"
83
63
  - !ruby/object:Gem::Version
84
- version: 2.0.0
64
+ version: '2'
85
65
  type: :runtime
86
66
  prerelease: false
87
67
  version_requirements: !ruby/object:Gem::Requirement
88
68
  requirements:
89
69
  - - ">="
90
70
  - !ruby/object:Gem::Version
91
- version: 1.4.0
71
+ version: '1.12'
92
72
  - - "<"
93
73
  - !ruby/object:Gem::Version
94
- version: 2.0.0
74
+ version: '2'
95
75
  - !ruby/object:Gem::Dependency
96
76
  name: rack
97
77
  requirement: !ruby/object:Gem::Requirement
@@ -344,6 +324,20 @@ dependencies:
344
324
  - - ">="
345
325
  - !ruby/object:Gem::Version
346
326
  version: '0'
327
+ - !ruby/object:Gem::Dependency
328
+ name: rubocop-performance
329
+ requirement: !ruby/object:Gem::Requirement
330
+ requirements:
331
+ - - ">="
332
+ - !ruby/object:Gem::Version
333
+ version: '0'
334
+ type: :development
335
+ prerelease: false
336
+ version_requirements: !ruby/object:Gem::Requirement
337
+ requirements:
338
+ - - ">="
339
+ - !ruby/object:Gem::Version
340
+ version: '0'
347
341
  - !ruby/object:Gem::Dependency
348
342
  name: timecop
349
343
  requirement: !ruby/object:Gem::Requirement
@@ -551,8 +545,9 @@ files:
551
545
  homepage: https://wovn.io
552
546
  licenses:
553
547
  - MIT
554
- metadata: {}
555
- post_install_message:
548
+ metadata:
549
+ rubygems_mfa_required: 'true'
550
+ post_install_message:
556
551
  rdoc_options: []
557
552
  require_paths:
558
553
  - lib
@@ -568,7 +563,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
568
563
  version: '0'
569
564
  requirements: []
570
565
  rubygems_version: 3.1.4
571
- signing_key:
566
+ signing_key:
572
567
  specification_version: 4
573
568
  summary: Gem for WOVN.io
574
569
  test_files: