translation_engine 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fe8031fcf2dc7a9c9ccc1943a6a7eef302d15648
4
- data.tar.gz: 20d9f72bd23ce85b9231bc1ccddb4f10b4771a72
3
+ metadata.gz: d9403edc4263e3baab5d75038e0ce23c2d3e3b46
4
+ data.tar.gz: 63b5ce8b73ebe6a3866913167d347feae3048830
5
5
  SHA512:
6
- metadata.gz: 16a396afc09a799a4cf485d6cf53b972aa334220aae56bb9bd5d3a588b10edb0ed6bcd6b9df5bb98b85373dfca019b98858b41cbb4495596cc1f9819064ecae5
7
- data.tar.gz: 2637aefa8b0c515bfd28cc0e6d5d7474d0036f9c1298c69f0d2359c32734d7f89ed4a2006c58a4359c150b13cac9e102733c2e9d9fab15141c04b23b8c54f90b
6
+ metadata.gz: a527f2cc3b1387ce2e44d0fcfa04c169c87e1575acbe5d1e4319366ba39604ee37399d96959fc1e9e8cb6b525253690748314c41cc3bec96f61973faf9e62249
7
+ data.tar.gz: ac3c31d8ee464e4d11cd25be693122b0d9765c7e210b56c016c185559b6d2aea0fc877162a4709b50bb20bdc14409f97d3de5edd1e598e1fe522d8f54838ebd9
@@ -1,64 +1,68 @@
1
- class TranslationEngine::CatcherMiddleware
2
- REMOVE_QUERY = /\?.*/
3
- REPLACE_IDS = /\d+/
1
+ module TranslationEngine
2
+ class CatcherMiddleware
3
+ include Request
4
4
 
5
- def initialize(app)
6
- @app = app
7
- end
5
+ REMOVE_QUERY = /\?.*/
6
+ REPLACE_IDS = /\d+/
8
7
 
9
- def call(env)
10
- if TranslationEngine.use_catcher
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
- private
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
- def call_catcher(env)
20
- TranslationEngine::Translation.clear_catched
20
+ private
21
21
 
22
- if env['QUERY_STRING'].include?('translation_release')
23
- I18n.backend.release = params(env)['translation_release']
24
- end
22
+ def call_catcher(env)
23
+ TranslationEngine::Translation.clear_catched
25
24
 
26
- update_translations unless assets_request?(env)
25
+ if env['QUERY_STRING'].include?('translation_release')
26
+ I18n.backend.release = params(env)['translation_release']
27
+ end
27
28
 
28
- response = @app.call(env)
29
+ update_translations unless assets_request?(env)
29
30
 
30
- send_translations(env)
31
+ response = @app.call(env)
31
32
 
32
- response
33
- end
33
+ send_translations(env)
34
34
 
35
- def assets_request?(env)
36
- env['PATH_INFO'] =~ /\/assets/
37
- end
35
+ response
36
+ end
38
37
 
39
- def params(env)
40
- Rack::Utils.parse_query(env['QUERY_STRING'], '&')
41
- end
38
+ def assets_request?(env)
39
+ env['PATH_INFO'] =~ /\/assets/
40
+ end
42
41
 
43
- def update_translations
44
- translation_downloader.update
45
- end
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
- def send_translations(env)
48
- return if TranslationEngine::Translation.catched.empty?
50
+ def send_translations(env)
51
+ return if TranslationEngine::Translation.catched.empty?
49
52
 
50
- location = env['PATH_INFO'].gsub(REMOVE_QUERY, '').gsub(REPLACE_IDS, ':id')
53
+ location = env['PATH_INFO'].gsub(REMOVE_QUERY, '').gsub(REPLACE_IDS, ':id')
51
54
 
52
- data = {
53
- location: location,
54
- locale: I18n.locale,
55
- translations: TranslationEngine::Translation.catched.uniq
56
- }
55
+ data = {
56
+ location: location,
57
+ locale: I18n.locale,
58
+ translations: TranslationEngine::Translation.catched.uniq
59
+ }
57
60
 
58
- Thread.new { TranslationEngine::Connection.new.send_translations(data) }
59
- end
61
+ Thread.new { TranslationEngine::Connection.new.send_translations(data, remote_ip(env)) }
62
+ end
60
63
 
61
- def translation_downloader
62
- @translation_downloader ||= TranslationEngine::Downloader.new
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
- class TranslationEngine::ConnectionExceptionMiddleware < Faraday::Middleware
4
- def call(env)
5
- begin
6
- @app.call(env)
7
- rescue Faraday::Error => e
8
- message = "Connecting to TranslationServer got #{e.class}: #{e.message}"
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
- if TranslationEngine.raise_exceptions
11
- raise TranslationEngine::ConnectionError, message
12
- else
13
- Rails.logger.error { message }
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
- class TranslationEngine::KeysMiddleware
1
+ module TranslationEngine
2
+ class KeysMiddleware
2
3
 
3
- URL_PATH = /\A\/translation_engine\/keys/
4
+ URL_PATH = /\A\/translation_engine\/keys/
4
5
 
5
- def initialize(app)
6
- @app = app
7
- end
6
+ def initialize(app)
7
+ @app = app
8
+ end
8
9
 
9
- def call(env)
10
- if env['PATH_INFO'] =~ URL_PATH
11
- handle_redirect_request(env)
12
- else
13
- @app.call(env)
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
- private
18
+ private
18
19
 
19
- def handle_redirect_request(env)
20
- key_path = env['PATH_INFO'].gsub(URL_PATH, '')
20
+ def handle_redirect_request(env)
21
+ key_path = env['PATH_INFO'].gsub(URL_PATH, '')
21
22
 
22
- [
23
- 302,
24
- {
25
- 'Location' => "#{TranslationEngine.api_host}/#{key_path}",
26
- 'Content-Type' => 'text/html'
27
- },
28
- ['Moved Temporarily']
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
@@ -0,0 +1,13 @@
1
+ module TranslationEngine
2
+ module Request
3
+ def remote_ip(env)
4
+ request(env).ip
5
+ end
6
+
7
+ private
8
+
9
+ def request(env)
10
+ Rack::Request.new(env)
11
+ end
12
+ end
13
+ end
@@ -1,31 +1,34 @@
1
- class TranslationEngine::ScreenshotsMiddleware
1
+ module TranslationEngine
2
+ class ScreenshotsMiddleware
3
+ include Request
2
4
 
3
- URL_PATH = /\A\/translation_engine\/screenshots/
5
+ URL_PATH = /\A\/translation_engine\/screenshots/
4
6
 
5
- def initialize(app)
6
- @app = app
7
- end
7
+ def initialize(app)
8
+ @app = app
9
+ end
8
10
 
9
- def call(env)
10
- if TranslationEngine.use_screenshots && env['PATH_INFO'] =~ URL_PATH
11
- handle_translation_request(env)
12
- else
13
- @app.call(env)
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
- private
19
+ private
18
20
 
19
- def handle_translation_request(env)
20
- data = JSON.parse(env["rack.input"].read)
21
+ def handle_translation_request(env)
22
+ data = JSON.parse(env["rack.input"].read)
21
23
 
22
- TranslationEngine::Connection.new
23
- .send_images(data.merge(locale: I18n.locale))
24
+ TranslationEngine::Connection.new
25
+ .send_images(data.merge(locale: I18n.locale), remote_ip(env))
24
26
 
25
- [
26
- 200,
27
- { 'Content-Type' => 'application/json' },
28
- [{ message: 'translations saved' }.to_json]
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'] = 'application/json'
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
@@ -1,3 +1,3 @@
1
1
  module TranslationEngine
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  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.2
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-10-06 00:00:00.000000000 Z
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