diff --git a/lib/microsoft_translate_client.rb b/lib/remote_translations/microsoft/client.rb similarity index 97% rename from lib/microsoft_translate_client.rb rename to lib/remote_translations/microsoft/client.rb index 45c8845c8..c68447192 100644 --- a/lib/microsoft_translate_client.rb +++ b/lib/remote_translations/microsoft/client.rb @@ -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" diff --git a/lib/remote_translations_caller.rb b/lib/remote_translations_caller.rb index a73aafe4b..75d8a6e50 100644 --- a/lib/remote_translations_caller.rb +++ b/lib/remote_translations_caller.rb @@ -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 diff --git a/lib/sentences_parser.rb b/lib/sentences_parser.rb index 890a6eb57..1403b0698 100644 --- a/lib/sentences_parser.rb +++ b/lib/sentences_parser.rb @@ -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(" ") diff --git a/spec/lib/microsoft_translate_client_spec.rb b/spec/lib/remote_translations/microsoft/client_spec.rb similarity index 86% rename from spec/lib/microsoft_translate_client_spec.rb rename to spec/lib/remote_translations/microsoft/client_spec.rb index e93203502..fcabb5c1d 100644 --- a/spec/lib/microsoft_translate_client_spec.rb +++ b/spec/lib/remote_translations/microsoft/client_spec.rb @@ -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 diff --git a/spec/lib/remote_translations_caller_spec.rb b/spec/lib/remote_translations_caller_spec.rb index 3aef505c7..25b64005f 100644 --- a/spec/lib/remote_translations_caller_spec.rb +++ b/spec/lib/remote_translations_caller_spec.rb @@ -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