Class: Translated::Translator
- Inherits:
-
Object
- Object
- Translated::Translator
- Defined in:
- app/models/translated/translator.rb
Constant Summary collapse
- API_HOST =
ENV.fetch('TRANSLATED_API_HOST', 'https://translatedrb.com')
- MAX_RETRIES =
3- RETRY_DELAY =
seconds
1
Instance Method Summary collapse
Instance Method Details
#translate(text, from:, to:) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/models/translated/translator.rb', line 11 def translate(text, from:, to:) retries = 0 begin response = RestClient.post( "#{API_HOST}/translate", { text: text, from: from, to: to }.to_json, accept: :json, authorization: "Token token=\"#{api_key}\"", content_type: :json ) JSON.parse(response.body)['translated_text'] rescue RestClient::GatewayTimeout, RestClient::BadGateway => e retries += 1 if retries <= MAX_RETRIES sleep(RETRY_DELAY * retries) # exponential backoff retry else raise e end end end |