translation_engine 0.0.2 → 0.0.3
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/app/middlewares/translation_engine/catcher_middleware.rb +48 -44
- data/app/middlewares/translation_engine/connection_exception_middleware.rb +13 -11
- data/app/middlewares/translation_engine/keys_middleware.rb +24 -22
- data/app/middlewares/translation_engine/request.rb +13 -0
- data/app/middlewares/translation_engine/screenshots_middleware.rb +24 -21
- data/app/models/translation_engine/connection.rb +5 -3
- data/lib/translation_engine/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d9403edc4263e3baab5d75038e0ce23c2d3e3b46
|
4
|
+
data.tar.gz: 63b5ce8b73ebe6a3866913167d347feae3048830
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a527f2cc3b1387ce2e44d0fcfa04c169c87e1575acbe5d1e4319366ba39604ee37399d96959fc1e9e8cb6b525253690748314c41cc3bec96f61973faf9e62249
|
7
|
+
data.tar.gz: ac3c31d8ee464e4d11cd25be693122b0d9765c7e210b56c016c185559b6d2aea0fc877162a4709b50bb20bdc14409f97d3de5edd1e598e1fe522d8f54838ebd9
|
@@ -1,64 +1,68 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module TranslationEngine
|
2
|
+
class CatcherMiddleware
|
3
|
+
include Request
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
end
|
5
|
+
REMOVE_QUERY = /\?.*/
|
6
|
+
REPLACE_IDS = /\d+/
|
8
7
|
|
9
|
-
|
10
|
-
|
11
|
-
call_catcher(env)
|
12
|
-
else
|
13
|
-
@app.call(env)
|
8
|
+
def initialize(app)
|
9
|
+
@app = app
|
14
10
|
end
|
15
|
-
end
|
16
11
|
|
17
|
-
|
12
|
+
def call(env)
|
13
|
+
if TranslationEngine.use_catcher
|
14
|
+
call_catcher(env)
|
15
|
+
else
|
16
|
+
@app.call(env)
|
17
|
+
end
|
18
|
+
end
|
18
19
|
|
19
|
-
|
20
|
-
TranslationEngine::Translation.clear_catched
|
20
|
+
private
|
21
21
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
22
|
+
def call_catcher(env)
|
23
|
+
TranslationEngine::Translation.clear_catched
|
25
24
|
|
26
|
-
|
25
|
+
if env['QUERY_STRING'].include?('translation_release')
|
26
|
+
I18n.backend.release = params(env)['translation_release']
|
27
|
+
end
|
27
28
|
|
28
|
-
|
29
|
+
update_translations unless assets_request?(env)
|
29
30
|
|
30
|
-
|
31
|
+
response = @app.call(env)
|
31
32
|
|
32
|
-
|
33
|
-
end
|
33
|
+
send_translations(env)
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
end
|
35
|
+
response
|
36
|
+
end
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
def assets_request?(env)
|
39
|
+
env['PATH_INFO'] =~ /\/assets/
|
40
|
+
end
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
def params(env)
|
43
|
+
Rack::Utils.parse_query(env['QUERY_STRING'], '&')
|
44
|
+
end
|
45
|
+
|
46
|
+
def update_translations
|
47
|
+
translation_downloader.update
|
48
|
+
end
|
46
49
|
|
47
|
-
|
48
|
-
|
50
|
+
def send_translations(env)
|
51
|
+
return if TranslationEngine::Translation.catched.empty?
|
49
52
|
|
50
|
-
|
53
|
+
location = env['PATH_INFO'].gsub(REMOVE_QUERY, '').gsub(REPLACE_IDS, ':id')
|
51
54
|
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
55
|
+
data = {
|
56
|
+
location: location,
|
57
|
+
locale: I18n.locale,
|
58
|
+
translations: TranslationEngine::Translation.catched.uniq
|
59
|
+
}
|
57
60
|
|
58
|
-
|
59
|
-
|
61
|
+
Thread.new { TranslationEngine::Connection.new.send_translations(data, remote_ip(env)) }
|
62
|
+
end
|
60
63
|
|
61
|
-
|
62
|
-
|
64
|
+
def translation_downloader
|
65
|
+
@translation_downloader ||= TranslationEngine::Downloader.new
|
66
|
+
end
|
63
67
|
end
|
64
68
|
end
|
@@ -1,17 +1,19 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
module TranslationEngine
|
4
|
+
class ConnectionExceptionMiddleware < Faraday::Middleware
|
5
|
+
def call(env)
|
6
|
+
begin
|
7
|
+
@app.call(env)
|
8
|
+
rescue Faraday::Error => e
|
9
|
+
message = "Connecting to TranslationServer got #{e.class}: #{e.message}"
|
9
10
|
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
11
|
+
if TranslationEngine.raise_exceptions
|
12
|
+
raise TranslationEngine::ConnectionError, message
|
13
|
+
else
|
14
|
+
Rails.logger.error { message }
|
15
|
+
{}
|
16
|
+
end
|
15
17
|
end
|
16
18
|
end
|
17
19
|
end
|
@@ -1,31 +1,33 @@
|
|
1
|
-
|
1
|
+
module TranslationEngine
|
2
|
+
class KeysMiddleware
|
2
3
|
|
3
|
-
|
4
|
+
URL_PATH = /\A\/translation_engine\/keys/
|
4
5
|
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
def initialize(app)
|
7
|
+
@app = app
|
8
|
+
end
|
8
9
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def call(env)
|
11
|
+
if env['PATH_INFO'] =~ URL_PATH
|
12
|
+
handle_redirect_request(env)
|
13
|
+
else
|
14
|
+
@app.call(env)
|
15
|
+
end
|
14
16
|
end
|
15
|
-
end
|
16
17
|
|
17
|
-
|
18
|
+
private
|
18
19
|
|
19
|
-
|
20
|
-
|
20
|
+
def handle_redirect_request(env)
|
21
|
+
key_path = env['PATH_INFO'].gsub(URL_PATH, '')
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
[
|
24
|
+
302,
|
25
|
+
{
|
26
|
+
'Location' => "#{TranslationEngine.api_host}/#{key_path}",
|
27
|
+
'Content-Type' => 'text/html'
|
28
|
+
},
|
29
|
+
['Moved Temporarily']
|
30
|
+
]
|
31
|
+
end
|
30
32
|
end
|
31
33
|
end
|
@@ -1,31 +1,34 @@
|
|
1
|
-
|
1
|
+
module TranslationEngine
|
2
|
+
class ScreenshotsMiddleware
|
3
|
+
include Request
|
2
4
|
|
3
|
-
|
5
|
+
URL_PATH = /\A\/translation_engine\/screenshots/
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
7
|
+
def initialize(app)
|
8
|
+
@app = app
|
9
|
+
end
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
11
|
+
def call(env)
|
12
|
+
if TranslationEngine.use_screenshots && env['PATH_INFO'] =~ URL_PATH
|
13
|
+
handle_translation_request(env)
|
14
|
+
else
|
15
|
+
@app.call(env)
|
16
|
+
end
|
14
17
|
end
|
15
|
-
end
|
16
18
|
|
17
|
-
|
19
|
+
private
|
18
20
|
|
19
|
-
|
20
|
-
|
21
|
+
def handle_translation_request(env)
|
22
|
+
data = JSON.parse(env["rack.input"].read)
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
+
TranslationEngine::Connection.new
|
25
|
+
.send_images(data.merge(locale: I18n.locale), remote_ip(env))
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
27
|
+
[
|
28
|
+
200,
|
29
|
+
{ 'Content-Type' => 'application/json' },
|
30
|
+
[{ message: 'translations saved' }.to_json]
|
31
|
+
]
|
32
|
+
end
|
30
33
|
end
|
31
34
|
end
|
@@ -4,22 +4,24 @@ class TranslationEngine::Connection
|
|
4
4
|
|
5
5
|
NotFound = Class.new(Exception)
|
6
6
|
|
7
|
-
def send_images(data)
|
7
|
+
def send_images(data, ip_address = nil)
|
8
8
|
connection.post do |req|
|
9
9
|
req.url '/api/v1/images'
|
10
10
|
req.headers['Content-Type'] = 'application/json'
|
11
11
|
req.headers['Authorization'] = api_token
|
12
|
+
req.headers['Original-IP-Address'] = ip_address if ip_address.present?
|
12
13
|
req.body = data.to_json
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
16
|
-
def send_translations(data)
|
17
|
+
def send_translations(data, ip_address = nil)
|
17
18
|
Thread.new do
|
18
19
|
begin
|
19
20
|
puts "Sending translations in separate thread"
|
20
21
|
connection(60).post do |req|
|
21
22
|
req.url '/api/v1/translations'
|
22
|
-
req.headers['Content-Type']
|
23
|
+
req.headers['Content-Type'] = 'application/json'
|
24
|
+
req.headers['Original-IP-Address'] = ip_address if ip_address.present?
|
23
25
|
req.headers['Authorization'] = api_token
|
24
26
|
req.body = data.to_json
|
25
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: translation_engine
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ondrej Bartas
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-11-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -108,6 +108,7 @@ files:
|
|
108
108
|
- app/middlewares/translation_engine/catcher_middleware.rb
|
109
109
|
- app/middlewares/translation_engine/connection_exception_middleware.rb
|
110
110
|
- app/middlewares/translation_engine/keys_middleware.rb
|
111
|
+
- app/middlewares/translation_engine/request.rb
|
111
112
|
- app/middlewares/translation_engine/screenshots_middleware.rb
|
112
113
|
- app/models/translation_engine/backend.rb
|
113
114
|
- app/models/translation_engine/connection.rb
|