Convert AvailableLocales from Module to Class
This commit is contained in:
@@ -3,15 +3,15 @@ require "uri"
|
||||
require "cgi"
|
||||
require "json"
|
||||
|
||||
module RemoteTranslations::Microsoft::AvailableLocales
|
||||
class RemoteTranslations::Microsoft::AvailableLocales
|
||||
|
||||
def available_locales
|
||||
def self.available_locales
|
||||
daily_cache("locales") do
|
||||
remote_available_locales.map { |locale| locale.first }
|
||||
end
|
||||
end
|
||||
|
||||
def parse_locale(locale)
|
||||
def self.parse_locale(locale)
|
||||
case locale
|
||||
when :"pt-BR"
|
||||
:pt
|
||||
@@ -24,13 +24,13 @@ module RemoteTranslations::Microsoft::AvailableLocales
|
||||
end
|
||||
end
|
||||
|
||||
def include_locale?(locale)
|
||||
def self.include_locale?(locale)
|
||||
available_locales.include?(parse_locale(locale).to_s)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def remote_available_locales
|
||||
def self.remote_available_locales
|
||||
host = "https://api.cognitive.microsofttranslator.com"
|
||||
path = "/languages?api-version=3.0"
|
||||
|
||||
@@ -48,7 +48,7 @@ module RemoteTranslations::Microsoft::AvailableLocales
|
||||
JSON.parse(result)["translation"]
|
||||
end
|
||||
|
||||
def daily_cache(key, &block)
|
||||
def self.daily_cache(key, &block)
|
||||
Rails.cache.fetch("remote_available_locales/#{Time.current.strftime("%Y-%m-%d")}/#{key}", &block)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
require "translator-text"
|
||||
include RemoteTranslations::Microsoft::SentencesParser
|
||||
include RemoteTranslations::Microsoft::AvailableLocales
|
||||
|
||||
class RemoteTranslations::Microsoft::Client
|
||||
CHARACTERS_LIMIT_PER_REQUEST = 5000
|
||||
@@ -13,7 +12,7 @@ class RemoteTranslations::Microsoft::Client
|
||||
|
||||
def call(fields_values, locale)
|
||||
texts = prepare_texts(fields_values)
|
||||
valid_locale = parse_locale(locale)
|
||||
valid_locale = RemoteTranslations::Microsoft::AvailableLocales.parse_locale(locale)
|
||||
request_translation(texts, valid_locale)
|
||||
end
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
require "rails_helper"
|
||||
include RemoteTranslations::Microsoft::AvailableLocales
|
||||
|
||||
describe "Remote Translations" do
|
||||
|
||||
@@ -8,8 +7,8 @@ describe "Remote Translations" do
|
||||
create(:proposal)
|
||||
available_locales_response = ["ar", "de", "en", "es", "fa", "fr", "he", "it", "nl", "pl",
|
||||
"pt", "sv", "zh-Hans", "zh-Hant"]
|
||||
expect_any_instance_of(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).
|
||||
and_return(available_locales_response)
|
||||
expect(RemoteTranslations::Microsoft::AvailableLocales).to receive(:available_locales).
|
||||
and_return(available_locales_response)
|
||||
end
|
||||
|
||||
after do
|
||||
|
||||
Reference in New Issue
Block a user