Add MicrososftTranslateClient to new namespace
- Rename to Client - Add to new namespace RemoteTranslations::Microsoft - Update references to new namespace
This commit is contained in:
@@ -2,7 +2,7 @@ require "translator-text"
|
||||
include SentencesParser
|
||||
include RemoteAvailableLocales
|
||||
|
||||
class MicrosoftTranslateClient
|
||||
class RemoteTranslations::Microsoft::Client
|
||||
CHARACTERS_LIMIT_PER_REQUEST = 5000
|
||||
PREVENTING_TRANSLATION_KEY = "notranslate"
|
||||
|
||||
@@ -35,7 +35,7 @@ class RemoteTranslationsCaller
|
||||
end
|
||||
|
||||
def translations
|
||||
@translations ||= MicrosoftTranslateClient.new.call(fields_values, locale)
|
||||
@translations ||= RemoteTranslations::Microsoft::Client.new.call(fields_values, locale)
|
||||
end
|
||||
|
||||
def fields_values
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module SentencesParser
|
||||
|
||||
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_whitespace = text[minimum_valid_index..text.size].index(" ")
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
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
|
||||
|
||||
@@ -12,7 +12,7 @@ describe MicrosoftTranslateClient do
|
||||
|
||||
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"])
|
||||
end
|
||||
@@ -22,7 +22,7 @@ describe MicrosoftTranslateClient do
|
||||
|
||||
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"])
|
||||
end
|
||||
@@ -31,7 +31,7 @@ describe MicrosoftTranslateClient 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
|
||||
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20)
|
||||
stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
|
||||
text_en = Faker::Lorem.characters(11)
|
||||
another_text_en = Faker::Lorem.characters(11)
|
||||
|
||||
@@ -47,13 +47,13 @@ describe MicrosoftTranslateClient do
|
||||
.times
|
||||
.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])
|
||||
end
|
||||
|
||||
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) + " "
|
||||
end_text_en = Faker::Lorem.characters(10)
|
||||
text_en = start_text_en + end_text_en
|
||||
@@ -92,7 +92,7 @@ describe MicrosoftTranslateClient do
|
||||
.times
|
||||
.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])
|
||||
end
|
||||
@@ -105,10 +105,10 @@ describe MicrosoftTranslateClient do
|
||||
|
||||
context "text has less characters than characters limit" 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)
|
||||
|
||||
result = microsoft_client.fragments_for(text_to_translate)
|
||||
result = client.fragments_for(text_to_translate)
|
||||
|
||||
expect(result).to eq [text_to_translate]
|
||||
end
|
||||
@@ -116,36 +116,36 @@ describe MicrosoftTranslateClient 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
|
||||
stub_const("MicrosoftTranslateClient::CHARACTERS_LIMIT_PER_REQUEST", 20)
|
||||
stub_const("RemoteTranslations::Microsoft::Client::CHARACTERS_LIMIT_PER_REQUEST", 20)
|
||||
start_text = Faker::Lorem.characters(10) + "."
|
||||
end_text = Faker::Lorem.characters(10)
|
||||
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])
|
||||
end
|
||||
|
||||
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) + " "
|
||||
end_text = Faker::Lorem.characters(10)
|
||||
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])
|
||||
end
|
||||
|
||||
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_2 = Faker::Lorem.characters(5)
|
||||
sub_part_text_3 = Faker::Lorem.characters(9)
|
||||
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
|
||||
|
||||
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])
|
||||
end
|
||||
@@ -21,7 +21,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "returns the resource with new translation persisted" do
|
||||
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
|
||||
|
||||
@@ -30,7 +30,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "when new translation locale is distinct to default_locale skip length validations" do
|
||||
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
|
||||
|
||||
@@ -41,7 +41,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "when new translation locale is distinct to default_locale not skip presence validations" do
|
||||
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
|
||||
|
||||
@@ -52,7 +52,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "destroy remote translation instance" do
|
||||
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
|
||||
|
||||
@@ -70,7 +70,7 @@ describe RemoteTranslationsCaller do
|
||||
it "returns the resource with new translation persisted" do
|
||||
response = ["Título traducido", "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
|
||||
|
||||
@@ -79,7 +79,7 @@ describe RemoteTranslationsCaller 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]
|
||||
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
|
||||
|
||||
@@ -90,7 +90,7 @@ describe RemoteTranslationsCaller 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]
|
||||
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
|
||||
|
||||
@@ -102,7 +102,7 @@ describe RemoteTranslationsCaller do
|
||||
it "destroy remote translation instance" do
|
||||
response = ["Título traducido", "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
|
||||
|
||||
@@ -120,7 +120,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "returns the resource with new translation persisted" do
|
||||
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
|
||||
|
||||
@@ -129,7 +129,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "when new translation locale is distinct to default_locale skip lenght validations" do
|
||||
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
|
||||
|
||||
@@ -140,7 +140,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "when new translation locale is distinct to default_locale not skip presence validations" do
|
||||
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
|
||||
|
||||
@@ -151,7 +151,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "destroy remote translation instance" do
|
||||
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
|
||||
|
||||
@@ -168,7 +168,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "returns the resource with new translation persisted" do
|
||||
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
|
||||
|
||||
@@ -177,7 +177,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "when new translation locale is distinct to default_locale skip lenght validations" do
|
||||
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
|
||||
|
||||
@@ -188,7 +188,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "when new translation locale is distinct to default_locale not skip presence validations" do
|
||||
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
|
||||
|
||||
@@ -199,7 +199,7 @@ describe RemoteTranslationsCaller do
|
||||
|
||||
it "destroy remote translation instance" do
|
||||
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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user