wovnrb 3.10.2 → 3.10.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,183 +1,183 @@
1
- require 'addressable'
2
- require 'digest'
3
- require 'json'
4
- require 'zlib'
5
-
6
- module Wovnrb
7
- class ApiTranslator
8
- def initialize(store, headers, uuid)
9
- @store = store
10
- @headers = headers
11
- @uuid = uuid
12
- end
13
-
14
- def translate(body)
15
- connection = prepare_connection
16
- request = prepare_request(body)
17
-
18
- begin
19
- response = connection.request(request)
20
- rescue => e
21
- WovnLogger.error("\"#{e.message}\" error occurred when contacting WOVNio translation API")
22
- return body
23
- end
24
-
25
- case response
26
- when Net::HTTPSuccess
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
36
- body
37
- end
38
- else
39
- WovnLogger.error("HTML-swapper call failed. Received \"#{response.message}\" from WOVNio translation API.")
40
- body
41
- end
42
- end
43
-
44
- private
45
-
46
- def prepare_connection
47
- connection = Net::HTTP.new(api_uri.host, api_uri.port)
48
-
49
- connection.open_timeout = api_timeout
50
- connection.read_timeout = api_timeout
51
-
52
- connection
53
- end
54
-
55
- def prepare_request(body)
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.to_json)
66
- request = Net::HTTP::Post.new(request_path(html_body), {
67
- 'Accept-Encoding' => 'gzip',
68
- 'Content-Type' => 'application/json',
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
77
-
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
86
-
87
- request
88
- end
89
-
90
- def request_path(body)
91
- "#{api_uri.path}/translation?cache_key=#{cache_key(body)}"
92
- end
93
-
94
- def cache_key(body)
95
- cache_key_components = {
96
- 'token' => token,
97
- 'settings_hash' => settings_hash,
98
- 'body_hash' => Digest::MD5.hexdigest(body),
99
- 'path' => page_pathname,
100
- 'lang' => lang_code,
101
- 'version' => "wovnrb_#{VERSION}"
102
- }.map { |k, v| "#{k}=#{v}" }.join('&')
103
-
104
- CGI.escape("(#{cache_key_components})")
105
- end
106
-
107
- def build_api_params(body)
108
- result = {
109
- 'url' => page_url,
110
- 'token' => token,
111
- 'lang_code' => lang_code,
112
- 'url_pattern' => url_pattern,
113
- 'lang_param_name' => lang_param_name,
114
- 'translate_canonical_tag' => translate_canonical_tag,
115
- 'insert_hreflangs' => insert_hreflangs,
116
- 'product' => 'WOVN.rb',
117
- 'version' => VERSION,
118
- 'body' => body
119
- }
120
-
121
- result['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
122
- result['custom_domain_langs'] = JSON.dump(custom_domain_langs) unless custom_domain_langs.empty?
123
-
124
- result
125
- end
126
-
127
- def compress_request_data(data_hash)
128
- ActiveSupport::Gzip.compress(data_hash)
129
- end
130
-
131
- def api_uri
132
- Addressable::URI.parse("#{@store.settings['api_url']}/v0")
133
- end
134
-
135
- def api_timeout
136
- @headers.search_engine_bot? ? @store.settings['api_timeout_search_engine_bots'] : @store.settings['api_timeout_seconds']
137
- end
138
-
139
- def settings_hash
140
- Digest::MD5.hexdigest(JSON.dump(@store.settings))
141
- end
142
-
143
- def token
144
- @store.settings['project_token']
145
- end
146
-
147
- def lang_code
148
- @headers.lang_code
149
- end
150
-
151
- def url_pattern
152
- @store.settings['url_pattern']
153
- end
154
-
155
- def lang_param_name
156
- @store.settings['lang_param_name']
157
- end
158
-
159
- def custom_lang_aliases
160
- @store.settings['custom_lang_aliases']
161
- end
162
-
163
- def translate_canonical_tag
164
- @store.settings['translate_canonical_tag']
165
- end
166
-
167
- def insert_hreflangs
168
- @store.settings['insert_hreflangs']
169
- end
170
-
171
- def custom_domain_langs
172
- @store.custom_domain_langs.to_html_swapper_hash
173
- end
174
-
175
- def page_url
176
- "#{@headers.protocol}://#{@headers.url}"
177
- end
178
-
179
- def page_pathname
180
- @headers.pathname_with_trailing_slash_if_present
181
- end
182
- end
183
- end
1
+ require 'addressable'
2
+ require 'digest'
3
+ require 'json'
4
+ require 'zlib'
5
+
6
+ module Wovnrb
7
+ class ApiTranslator
8
+ def initialize(store, headers, uuid)
9
+ @store = store
10
+ @headers = headers
11
+ @uuid = uuid
12
+ end
13
+
14
+ def translate(body)
15
+ connection = prepare_connection
16
+ request = prepare_request(body)
17
+
18
+ begin
19
+ response = connection.request(request)
20
+ rescue => e
21
+ WovnLogger.error("\"#{e.message}\" error occurred when contacting WOVNio translation API")
22
+ return body
23
+ end
24
+
25
+ case response
26
+ when Net::HTTPSuccess
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
36
+ body
37
+ end
38
+ else
39
+ WovnLogger.error("HTML-swapper call failed. Received \"#{response.message}\" from WOVNio translation API.")
40
+ body
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def prepare_connection
47
+ connection = Net::HTTP.new(api_uri.host, api_uri.port)
48
+
49
+ connection.open_timeout = api_timeout
50
+ connection.read_timeout = api_timeout
51
+
52
+ connection
53
+ end
54
+
55
+ def prepare_request(body)
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.to_json)
66
+ request = Net::HTTP::Post.new(request_path(html_body), {
67
+ 'Accept-Encoding' => 'gzip',
68
+ 'Content-Type' => 'application/json',
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
77
+
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
86
+
87
+ request
88
+ end
89
+
90
+ def request_path(body)
91
+ "#{api_uri.path}/translation?cache_key=#{cache_key(body)}"
92
+ end
93
+
94
+ def cache_key(body)
95
+ cache_key_components = {
96
+ 'token' => token,
97
+ 'settings_hash' => settings_hash,
98
+ 'body_hash' => Digest::MD5.hexdigest(body),
99
+ 'path' => page_pathname,
100
+ 'lang' => lang_code,
101
+ 'version' => "wovnrb_#{VERSION}"
102
+ }.map { |k, v| "#{k}=#{v}" }.join('&')
103
+
104
+ CGI.escape("(#{cache_key_components})")
105
+ end
106
+
107
+ def build_api_params(body)
108
+ result = {
109
+ 'url' => page_url,
110
+ 'token' => token,
111
+ 'lang_code' => lang_code,
112
+ 'url_pattern' => url_pattern,
113
+ 'lang_param_name' => lang_param_name,
114
+ 'translate_canonical_tag' => translate_canonical_tag,
115
+ 'insert_hreflangs' => insert_hreflangs,
116
+ 'product' => 'WOVN.rb',
117
+ 'version' => VERSION,
118
+ 'body' => body
119
+ }
120
+
121
+ result['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
122
+ result['custom_domain_langs'] = JSON.dump(custom_domain_langs) unless custom_domain_langs.empty?
123
+
124
+ result
125
+ end
126
+
127
+ def compress_request_data(data_hash)
128
+ ActiveSupport::Gzip.compress(data_hash)
129
+ end
130
+
131
+ def api_uri
132
+ Addressable::URI.parse("#{@store.settings['api_url']}/v0")
133
+ end
134
+
135
+ def api_timeout
136
+ @headers.search_engine_bot? ? @store.settings['api_timeout_search_engine_bots'] : @store.settings['api_timeout_seconds']
137
+ end
138
+
139
+ def settings_hash
140
+ Digest::MD5.hexdigest(JSON.dump(@store.settings))
141
+ end
142
+
143
+ def token
144
+ @store.settings['project_token']
145
+ end
146
+
147
+ def lang_code
148
+ @headers.lang_code
149
+ end
150
+
151
+ def url_pattern
152
+ @store.settings['url_pattern']
153
+ end
154
+
155
+ def lang_param_name
156
+ @store.settings['lang_param_name']
157
+ end
158
+
159
+ def custom_lang_aliases
160
+ @store.settings['custom_lang_aliases']
161
+ end
162
+
163
+ def translate_canonical_tag
164
+ @store.settings['translate_canonical_tag']
165
+ end
166
+
167
+ def insert_hreflangs
168
+ @store.settings['insert_hreflangs']
169
+ end
170
+
171
+ def custom_domain_langs
172
+ @store.custom_domain_langs.to_html_swapper_hash
173
+ end
174
+
175
+ def page_url
176
+ "#{@headers.protocol}://#{@headers.url}"
177
+ end
178
+
179
+ def page_pathname
180
+ @headers.pathname_with_trailing_slash_if_present
181
+ end
182
+ end
183
+ end