Add MicrososftTranslateClient to new namespace

- Rename to Client
- Add to new namespace RemoteTranslations::Microsoft
- Update references to new namespace
This commit is contained in:
taitus
2019-05-24 15:13:15 +02:00
committed by voodoorai2000
parent 91ba5ff879
commit d29656d78f
5 changed files with 35 additions and 35 deletions

View File

@@ -2,7 +2,7 @@ require "translator-text"
include SentencesParser include SentencesParser
include RemoteAvailableLocales include RemoteAvailableLocales
class MicrosoftTranslateClient class RemoteTranslations::Microsoft::Client
CHARACTERS_LIMIT_PER_REQUEST = 5000 CHARACTERS_LIMIT_PER_REQUEST = 5000
PREVENTING_TRANSLATION_KEY = "notranslate" PREVENTING_TRANSLATION_KEY = "notranslate"

View File

@@ -35,7 +35,7 @@ class RemoteTranslationsCaller
end end
def translations def translations
@translations ||= MicrosoftTranslateClient.new.call(fields_values, locale) @translations ||= RemoteTranslations::Microsoft::Client.new.call(fields_values, locale)
end end
def fields_values def fields_values

View File

@@ -1,7 +1,7 @@
module SentencesParser module SentencesParser
def detect_split_position(text) def detect_split_position(text)
minimum_valid_index = text.size - MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST minimum_valid_index = text.size - RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST
valid_point = text[minimum_valid_index..text.size].index(".") valid_point = text[minimum_valid_index..text.size].index(".")
valid_whitespace = text[minimum_valid_index..text.size].index(" ") valid_whitespace = text[minimum_valid_index..text.size].index(" ")

View File

@@ -1,8 +1,8 @@
require "rails_helper" require "rails_helper"
describe MicrosoftTranslateClient do describe RemoteTranslations::Microsoft::Client do
let(:microsoft_client) { described_class.new } let(:client) { described_class.new }
describe "#call" do describe "#call" do
@@ -12,7 +12,7 @@ describe MicrosoftTranslateClient do
expect_any_instance_of(TranslatorText::Client).to receive(:translate).and_return(response) expect_any_instance_of(TranslatorText::Client).to receive(:translate).and_return(response)
result = microsoft_client.call([ "New title", "New description"], :es) result = client.call([ "New title", "New description"], :es)
expect(result).to eq(["Nuevo título", "Nueva descripción"]) expect(result).to eq(["Nuevo título", "Nueva descripción"])
end end
@@ -22,7 +22,7 @@ describe MicrosoftTranslateClient do
expect_any_instance_of(TranslatorText::Client).to receive(:translate).and_return(response) expect_any_instance_of(TranslatorText::Client).to receive(:translate).and_return(response)
result = microsoft_client.call([nil, "New description"], :es) result = client.call([nil, "New description"], :es)
expect(result).to eq([nil, "Nueva descripción"]) expect(result).to eq([nil, "Nueva descripción"])
end end
@@ -31,7 +31,7 @@ describe MicrosoftTranslateClient do
context "when characters from request are greater than characters limit" do context "when characters from request are greater than characters limit" do
it "response has the expected result when the request has 2 texts, where both less than CHARACTERS_LIMIT_PER_REQUEST" do it "response has the expected result when the request has 2 texts, where both less than CHARACTERS_LIMIT_PER_REQUEST" do
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20) stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
text_en = Faker::Lorem.characters(11) text_en = Faker::Lorem.characters(11)
another_text_en = Faker::Lorem.characters(11) another_text_en = Faker::Lorem.characters(11)
@@ -47,13 +47,13 @@ describe MicrosoftTranslateClient do
.times .times
.and_return(response_another_text) .and_return(response_another_text)
result = microsoft_client.call([text_en, another_text_en], :es) result = client.call([text_en, another_text_en], :es)
expect(result).to eq([translated_text_es, another_translated_text_es]) expect(result).to eq([translated_text_es, another_translated_text_es])
end end
it "response has the expected result when the request has 2 texts and both are greater than CHARACTERS_LIMIT_PER_REQUEST" do it "response has the expected result when the request has 2 texts and both are greater than CHARACTERS_LIMIT_PER_REQUEST" do
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20) stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
start_text_en = Faker::Lorem.characters(10) + " " start_text_en = Faker::Lorem.characters(10) + " "
end_text_en = Faker::Lorem.characters(10) end_text_en = Faker::Lorem.characters(10)
text_en = start_text_en + end_text_en text_en = start_text_en + end_text_en
@@ -92,7 +92,7 @@ describe MicrosoftTranslateClient do
.times .times
.and_return(response_another_end_text) .and_return(response_another_end_text)
result = microsoft_client.call([text_en, another_text_en], :es) result = client.call([text_en, another_text_en], :es)
expect(result).to eq([translated_text_es, another_translated_text_es]) expect(result).to eq([translated_text_es, another_translated_text_es])
end end
@@ -105,10 +105,10 @@ describe MicrosoftTranslateClient do
context "text has less characters than characters limit" do context "text has less characters than characters limit" do
it "does not split the text" do it "does not split the text" do
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20) stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
text_to_translate = Faker::Lorem.characters(10) text_to_translate = Faker::Lorem.characters(10)
result = microsoft_client.fragments_for(text_to_translate) result = client.fragments_for(text_to_translate)
expect(result).to eq [text_to_translate] expect(result).to eq [text_to_translate]
end end
@@ -116,36 +116,36 @@ describe MicrosoftTranslateClient do
context "text has more characters than characters limit" do context "text has more characters than characters limit" do
it "to split text by first valid dot when there is a dot for split" do it "to split text by first valid dot when there is a dot for split" do
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20) stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
start_text = Faker::Lorem.characters(10) + "." start_text = Faker::Lorem.characters(10) + "."
end_text = Faker::Lorem.characters(10) end_text = Faker::Lorem.characters(10)
text_to_translate = start_text + end_text text_to_translate = start_text + end_text
result = microsoft_client.fragments_for(text_to_translate) result = client.fragments_for(text_to_translate)
expect(result).to eq([start_text, end_text]) expect(result).to eq([start_text, end_text])
end end
it "to split text by first valid space when there is not a dot for split but there is a space" do it "to split text by first valid space when there is not a dot for split but there is a space" do
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20) stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
start_text = Faker::Lorem.characters(10) + " " start_text = Faker::Lorem.characters(10) + " "
end_text = Faker::Lorem.characters(10) end_text = Faker::Lorem.characters(10)
text_to_translate = start_text + end_text text_to_translate = start_text + end_text
result = microsoft_client.fragments_for(text_to_translate) result = client.fragments_for(text_to_translate)
expect(result).to eq([start_text, end_text]) expect(result).to eq([start_text, end_text])
end end
it "to split text in the middle of a word when there are not valid dots and spaces" do it "to split text in the middle of a word when there are not valid dots and spaces" do
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 40) stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 40)
sub_part_text_1 = Faker::Lorem.characters(5) + " ." sub_part_text_1 = Faker::Lorem.characters(5) + " ."
sub_part_text_2 = Faker::Lorem.characters(5) sub_part_text_2 = Faker::Lorem.characters(5)
sub_part_text_3 = Faker::Lorem.characters(9) sub_part_text_3 = Faker::Lorem.characters(9)
sub_part_text_4 = Faker::Lorem.characters(30) sub_part_text_4 = Faker::Lorem.characters(30)
text_to_translate = sub_part_text_1 + sub_part_text_2 + sub_part_text_3 + sub_part_text_4 text_to_translate = sub_part_text_1 + sub_part_text_2 + sub_part_text_3 + sub_part_text_4
result = microsoft_client.fragments_for(text_to_translate) result = client.fragments_for(text_to_translate)
expect(result).to eq([sub_part_text_1 + sub_part_text_2, sub_part_text_3 + sub_part_text_4]) expect(result).to eq([sub_part_text_1 + sub_part_text_2, sub_part_text_3 + sub_part_text_4])
end end

View File

@@ -21,7 +21,7 @@ describe RemoteTranslationsCaller do
it "returns the resource with new translation persisted" do it "returns the resource with new translation persisted" do
response = ["Título traducido", "Descripción traducida"] response = ["Título traducido", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -30,7 +30,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale skip length validations" do it "when new translation locale is distinct to default_locale skip length validations" do
response = ["TT", "Descripción traducida"] response = ["TT", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -41,7 +41,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale not skip presence validations" do it "when new translation locale is distinct to default_locale not skip presence validations" do
response = ["", "Descripción traducida"] response = ["", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -52,7 +52,7 @@ describe RemoteTranslationsCaller do
it "destroy remote translation instance" do it "destroy remote translation instance" do
response = ["Título traducido", "Descripción traducida"] response = ["Título traducido", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -70,7 +70,7 @@ describe RemoteTranslationsCaller do
it "returns the resource with new translation persisted" do it "returns the resource with new translation persisted" do
response = ["Título traducido", "Descripción traducida", "Pregunta traducida", response = ["Título traducido", "Descripción traducida", "Pregunta traducida",
"Resumen traducido", nil] "Resumen traducido", nil]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -79,7 +79,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale skip lenght validations" do it "when new translation locale is distinct to default_locale skip lenght validations" do
response = ["TT", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil] response = ["TT", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -90,7 +90,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale do not skip presence validations" do it "when new translation locale is distinct to default_locale do not skip presence validations" do
response = ["", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil] response = ["", "Descripción traducida", "Pregunta traducida", "Resumen traducido", nil]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -102,7 +102,7 @@ describe RemoteTranslationsCaller do
it "destroy remote translation instance" do it "destroy remote translation instance" do
response = ["Título traducido", "Descripción traducida", "Pregunta traducida", response = ["Título traducido", "Descripción traducida", "Pregunta traducida",
"Resumen traducido", nil] "Resumen traducido", nil]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -120,7 +120,7 @@ describe RemoteTranslationsCaller do
it "returns the resource with new translation persisted" do it "returns the resource with new translation persisted" do
response = ["Título traducido", "Descripción traducida"] response = ["Título traducido", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -129,7 +129,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale skip lenght validations" do it "when new translation locale is distinct to default_locale skip lenght validations" do
response = ["TT", "Descripción traducida"] response = ["TT", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -140,7 +140,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale not skip presence validations" do it "when new translation locale is distinct to default_locale not skip presence validations" do
response = ["", "Descripción traducida"] response = ["", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -151,7 +151,7 @@ describe RemoteTranslationsCaller do
it "destroy remote translation instance" do it "destroy remote translation instance" do
response = ["Título traducido", "Descripción traducida"] response = ["Título traducido", "Descripción traducida"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -168,7 +168,7 @@ describe RemoteTranslationsCaller do
it "returns the resource with new translation persisted" do it "returns the resource with new translation persisted" do
response = ["Body traducido"] response = ["Body traducido"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -177,7 +177,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale skip lenght validations" do it "when new translation locale is distinct to default_locale skip lenght validations" do
response = ["BT"] response = ["BT"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -188,7 +188,7 @@ describe RemoteTranslationsCaller do
it "when new translation locale is distinct to default_locale not skip presence validations" do it "when new translation locale is distinct to default_locale not skip presence validations" do
response = [""] response = [""]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call
@@ -199,7 +199,7 @@ describe RemoteTranslationsCaller do
it "destroy remote translation instance" do it "destroy remote translation instance" do
response = ["Body traducido"] response = ["Body traducido"]
expect_any_instance_of(MicrosoftTranslateClient).to receive(:call).and_return(response) expect_any_instance_of(RemoteTranslations::Microsoft::Client).to receive(:call).and_return(response)
remote_translation_caller.call remote_translation_caller.call