Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ST-1312] Added dynamic loading support #205

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added dynamic loading support.
  • Loading branch information
zeyuwu committed Mar 14, 2022
commit cc837d7d100c30b1444ed0ec311a06ab0b0977fe
1 change: 1 addition & 0 deletions lib/wovnrb.rb
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@
require 'wovnrb/lang'
require 'wovnrb/services/html_converter'
require 'wovnrb/services/html_replace_marker'
require 'wovnrb/services/time'
require 'wovnrb/url_language_switcher'
require 'nokogiri'
require 'active_support'
17 changes: 14 additions & 3 deletions lib/wovnrb/api_translator.rb
Original file line number Diff line number Diff line change
@@ -5,10 +5,11 @@

module Wovnrb
class ApiTranslator
def initialize(store, headers, uuid)
def initialize(store, headers, uuid, time_proc = nil)
@store = store
@headers = headers
@uuid = uuid
@time_proc = time_proc.nil? ? TimeUtil.time_proc : time_proc
end

def translate(body)
@@ -99,7 +100,9 @@ def cache_key(body)
'path' => page_pathname,
'lang' => lang_code,
'version' => "wovnrb_#{VERSION}"
}.map { |k, v| "#{k}=#{v}" }.join('&')
}
cache_key_components['timestamp'] = search_engine_bot_timestamp(@time_proc) if @headers.search_engine_bot?
cache_key_components = cache_key_components.map { |k, v| "#{k}=#{v}" }.join('&')

CGI.escape("(#{cache_key_components})")
end
@@ -114,7 +117,8 @@ def build_api_params(body)
'translate_canonical_tag' => translate_canonical_tag,
'product' => 'WOVN.rb',
'version' => VERSION,
'body' => body
'body' => body,
'user_agent' => @headers.user_agent
}

result['custom_lang_aliases'] = JSON.dump(custom_lang_aliases) unless custom_lang_aliases.empty?
@@ -169,5 +173,12 @@ def page_url
def page_pathname
@headers.pathname_with_trailing_slash_if_present
end

def search_engine_bot_timestamp(time_proc)
twenty_minutes = 20 * 60
cache_time = TimeUtil.round_down_time(time_proc.call, twenty_minutes)
datetime = Time.at(cache_time, in: 'UTC').to_datetime
datetime.iso8601
end
end
end
4 changes: 4 additions & 0 deletions lib/wovnrb/headers.rb
Original file line number Diff line number Diff line change
@@ -172,6 +172,10 @@ def search_engine_bot?
bots.any? { |bot| @env['HTTP_USER_AGENT'].include?(bot) }
end

def user_agent
@env.key?('HTTP_USER_AGENT') ? @env['HTTP_USER_AGENT'] : ''
end

def to_absolute_path(path)
absolute_path = path.blank? ? '/' : path
absolute_path = absolute_path.starts_with?('/') ? absolute_path : URL.join_paths(dirname, absolute_path)
14 changes: 14 additions & 0 deletions lib/wovnrb/services/time.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Wovnrb
# Provides utilities related to Time
class TimeUtil
class << self
def round_down_time(time, unit)
time - (time % unit)
end

def time_proc
-> { return Time.new.sec }
end
end
end
end
2 changes: 1 addition & 1 deletion lib/wovnrb/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Wovnrb
VERSION = '3.7.0'.freeze
VERSION = '3.8.0'.freeze
end
48 changes: 48 additions & 0 deletions test/lib/api_translator_test.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
require 'test_helper'
require 'wovnrb/services/time'

module Wovnrb
REQUEST_UUID = 'ABCD'.freeze

class ApiTranslatorTest < WovnMiniTest
def teardown
WebMock.reset!
end

def test_translate
assert_translation('test.html', 'test_translated.html', true)
end
@@ -58,11 +63,53 @@ def test_translate_without_api_compression_sends_json
'product' => 'WOVN.rb',
'version' => VERSION,
'body' => 'foo',
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
'custom_lang_aliases' => { 'ja' => 'Japanese' }.to_json
}.to_json,
times: 1
end

def test_translate_is_search_engine_bot
settings = {
'project_token' => '123456',
'custom_lang_aliases' => { 'ja' => 'Japanese' },
'default_lang' => 'en',
'url_pattern' => 'subdomain',
'url_pattern_reg' => '^(?<lang>[^.]+).',
'lang_param_name' => 'lang',
'compress_api_requests' => false
}
store = Wovnrb::Store.instance
store.update_settings(settings)
env = Wovnrb.get_env('url' => 'http://fr.wovn.io/test')
env['HTTP_USER_AGENT'] = 'Googlebot/'
headers = Wovnrb::Headers.new(
env,
Wovnrb.get_settings(settings),
UrlLanguageSwitcher.new(store)
)
html_body = 'foo'

time_proc = -> { return 25_000_000 }
api_translator = ApiTranslator.new(store, headers, REQUEST_UUID, time_proc)
stub_request(:post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*timestamp=1970-10-17T08:20:00%2B00:00.*})
.to_return(status: 200, body: { 'body' => 'translated_body' }.to_json)

api_translator.translate(html_body)

time_proc = -> { return 25_000_300 }
api_translator = ApiTranslator.new(store, headers, REQUEST_UUID, time_proc)
zeyuwu marked this conversation as resolved.
Show resolved Hide resolved
api_translator.translate(html_body)

WebMock.reset!

time_proc = -> { return 25_001_200 }
api_translator = ApiTranslator.new(store, headers, REQUEST_UUID, time_proc)
stub_request(:post, %r{http://wovn\.global\.ssl\.fastly\.net/v0/translation\?cache_key=.*timestamp=1970-10-17T08:40:00%2B00:00.*})
.to_return(status: 200, body: { 'body' => 'translated_body' }.to_json)
api_translator.translate(html_body)
end

def test_api_timeout_is_search_engine_user_higher_default
settings = {
'project_token' => '123456',
@@ -202,6 +249,7 @@ def generate_data(original_html)
'product' => 'WOVN.rb',
'version' => VERSION,
'body' => original_html,
'user_agent' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36',
'custom_lang_aliases' => '{"ja":"Japanese"}'
}

19 changes: 19 additions & 0 deletions test/lib/services/time_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'test_helper'
require 'wovnrb/services/time'

module Wovnrb
class TimeTest < WovnMiniTest
def test_round_down_time
assert_equal(0, TimeUtil.round_down_time(0, 10))
assert_equal(10, TimeUtil.round_down_time(10, 10))
assert_equal(10, TimeUtil.round_down_time(16, 10))
assert_equal(30, TimeUtil.round_down_time(30, 15))
assert_equal(100, TimeUtil.round_down_time(100, 10))
end

def test_time_proc
current_time = Time.new.sec
assert((TimeUtil.time_proc.call - current_time) < 2.0)
end
end
end