wovnrb 3.2.0 → 3.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.en.md +21 -16
- data/README.ja.md +6 -6
- data/lib/wovnrb/api_translator.rb +37 -17
- data/lib/wovnrb/store.rb +8 -3
- data/lib/wovnrb/version.rb +1 -1
- data/test/lib/api_translator_test.rb +45 -5
- data/test/lib/headers_test.rb +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e5c458ea4be1b5bc1325a0c54a9672f7fd744b864975699ae48e70acde6d5265
|
4
|
+
data.tar.gz: 318102aa29ae9bbb03d5cf0b1ccffa3f39ada7a8fc963daad856b058f46e46ec
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fd81bc766461ce8212a32afafc0a673790985213304c55765322e42591e34107a649d70e1133a597b88c6cda3ee0813cd1b4efbac1ff45d2d794f52d78c80814
|
7
|
+
data.tar.gz: fd4304143e2226ab1b9a887d45e2326da8b8b452e0a60bcbb94f3b680fc4280459e47fb3b025d6660c2d338a1d3150617fc279b279e68693b0566a41050bd3de
|
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 => '
|
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 => '
|
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
|
78
|
-
|
79
|
-
project_token
|
80
|
-
default_lang
|
81
|
-
supported_langs
|
82
|
-
url_pattern
|
83
|
-
lang_param_name
|
84
|
-
query
|
85
|
-
ignore_class
|
86
|
-
translate_fragment
|
87
|
-
ignore_paths
|
88
|
-
install_middleware
|
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 => '
|
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 => '
|
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 | '
|
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 | | []
|
@@ -52,25 +52,45 @@ module Wovnrb
|
|
52
52
|
end
|
53
53
|
|
54
54
|
def prepare_request(body)
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
55
|
+
if @store.compress_api_requests?
|
56
|
+
gzip_request(body)
|
57
|
+
else
|
58
|
+
json_request(body)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def gzip_request(html_body)
|
63
|
+
api_params = build_api_params(html_body)
|
64
|
+
compressed_body = compress_request_data(api_params)
|
65
|
+
request = Net::HTTP::Post.new(request_path(html_body), {
|
66
|
+
'Accept-Encoding' => 'gzip',
|
67
|
+
'Content-Type' => 'application/octet-stream',
|
68
|
+
'Content-Encoding' => 'gzip',
|
69
|
+
'Content-Length' => compressed_body.bytesize.to_s,
|
70
|
+
'X-Request-Id' => @uuid
|
71
|
+
})
|
72
|
+
request.body = compressed_body
|
73
|
+
|
74
|
+
request
|
75
|
+
end
|
63
76
|
|
64
|
-
|
77
|
+
def json_request(html_body)
|
78
|
+
api_params = build_api_params(html_body)
|
79
|
+
request = Net::HTTP::Post.new(request_path(html_body), {
|
80
|
+
'Accept-Encoding' => 'gzip',
|
81
|
+
'Content-Type' => 'application/json',
|
82
|
+
'X-Request-Id' => @uuid
|
83
|
+
})
|
84
|
+
request.body = api_params.to_json
|
65
85
|
|
66
86
|
request
|
67
87
|
end
|
68
88
|
|
69
|
-
def
|
70
|
-
"#{api_uri.path
|
89
|
+
def request_path(body)
|
90
|
+
"#{api_uri.path}/translation?cache_key=#{cache_key(body)}"
|
71
91
|
end
|
72
92
|
|
73
|
-
def
|
93
|
+
def cache_key(body)
|
74
94
|
cache_key_components = {
|
75
95
|
'token' => token,
|
76
96
|
'settings_hash' => settings_hash,
|
@@ -83,8 +103,8 @@ module Wovnrb
|
|
83
103
|
CGI.escape("(#{cache_key_components})")
|
84
104
|
end
|
85
105
|
|
86
|
-
def
|
87
|
-
|
106
|
+
def build_api_params(body)
|
107
|
+
result = {
|
88
108
|
'url' => page_url,
|
89
109
|
'token' => token,
|
90
110
|
'lang_code' => lang_code,
|
@@ -95,9 +115,9 @@ module Wovnrb
|
|
95
115
|
'body' => body
|
96
116
|
}
|
97
117
|
|
98
|
-
|
118
|
+
result['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
|
99
119
|
|
100
|
-
|
120
|
+
result
|
101
121
|
end
|
102
122
|
|
103
123
|
def compress_request_data(data_hash)
|
@@ -111,7 +131,7 @@ module Wovnrb
|
|
111
131
|
end
|
112
132
|
|
113
133
|
def api_uri
|
114
|
-
Addressable::URI.parse("#{@store.settings['api_url']}/v0
|
134
|
+
Addressable::URI.parse("#{@store.settings['api_url']}/v0")
|
115
135
|
end
|
116
136
|
|
117
137
|
def api_timeout
|
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' => '
|
29
|
-
'supported_langs' => [
|
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
|
data/lib/wovnrb/version.rb
CHANGED
@@ -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)
|
@@ -24,6 +26,38 @@ module Wovnrb
|
|
24
26
|
assert_translation('test.html', 'test_translated.html', true, encoding: 'text/json')
|
25
27
|
end
|
26
28
|
|
29
|
+
def test_translate_without_api_compression_sends_json
|
30
|
+
Wovnrb::Store.instance.update_settings('compress_api_requests' => false)
|
31
|
+
sut, _store, _headers = create_sut
|
32
|
+
html_body = 'foo'
|
33
|
+
|
34
|
+
stub_request(:post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*})
|
35
|
+
.to_return(status: 200, body: { 'body' => 'translated_body' }.to_json)
|
36
|
+
|
37
|
+
sut.translate(html_body)
|
38
|
+
|
39
|
+
assert_requested :post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*},
|
40
|
+
headers: {
|
41
|
+
'Accept' => '*/*',
|
42
|
+
'Accept-Encoding' => 'gzip',
|
43
|
+
'Content-Type' => 'application/json',
|
44
|
+
'User-Agent' => 'Ruby',
|
45
|
+
'X-Request-Id' => REQUEST_UUID
|
46
|
+
},
|
47
|
+
body: {
|
48
|
+
'url' => 'http://wovn.io/test',
|
49
|
+
'token' => '123456',
|
50
|
+
'lang_code' => 'fr',
|
51
|
+
'url_pattern' => 'subdomain',
|
52
|
+
'lang_param_name' => 'lang',
|
53
|
+
'product' => 'WOVN.rb',
|
54
|
+
'version' => VERSION,
|
55
|
+
'body' => 'foo',
|
56
|
+
'custom_lang_aliases' => { 'ja' => 'Japanese' }.to_json
|
57
|
+
}.to_json,
|
58
|
+
times: 1
|
59
|
+
end
|
60
|
+
|
27
61
|
private
|
28
62
|
|
29
63
|
def assert_translation(original_html_fixture, translated_html_fixture, success_expected, response = { encoding: 'gzip', status_code: 200 })
|
@@ -39,6 +73,15 @@ module Wovnrb
|
|
39
73
|
end
|
40
74
|
|
41
75
|
def translate(original_html, translated_html, response)
|
76
|
+
api_translator, store, headers = create_sut
|
77
|
+
translation_request_stub = stub_translation_api_request(store, headers, original_html, translated_html, response)
|
78
|
+
|
79
|
+
actual_translated_html = api_translator.translate(original_html)
|
80
|
+
assert_requested(translation_request_stub, times: 1) if translation_request_stub
|
81
|
+
actual_translated_html
|
82
|
+
end
|
83
|
+
|
84
|
+
def create_sut
|
42
85
|
settings = {
|
43
86
|
'project_token' => '123456',
|
44
87
|
'custom_lang_aliases' => { 'ja' => 'Japanese' },
|
@@ -53,12 +96,9 @@ module Wovnrb
|
|
53
96
|
Wovnrb.get_env('url' => 'http://fr.wovn.io/test'),
|
54
97
|
Wovnrb.get_settings(settings)
|
55
98
|
)
|
56
|
-
api_translator = ApiTranslator.new(store, headers,
|
57
|
-
translation_request_stub = stub_translation_api_request(store, headers, original_html, translated_html, response)
|
99
|
+
api_translator = ApiTranslator.new(store, headers, REQUEST_UUID)
|
58
100
|
|
59
|
-
|
60
|
-
assert_requested(translation_request_stub, times: 1) if translation_request_stub
|
61
|
-
actual_translated_html
|
101
|
+
[api_translator, store, headers]
|
62
102
|
end
|
63
103
|
|
64
104
|
def stub_translation_api_request(store, headers, original_html, translated_html, response)
|
data/test/lib/headers_test.rb
CHANGED
@@ -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=
|
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=
|
182
|
+
assert_equal('http://wovn.io/contact?lang=en', sut.redirect_location('en'))
|
183
183
|
end
|
184
184
|
|
185
185
|
#########################
|
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-10-
|
11
|
+
date: 2021-10-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|