Class: Translated::Translator

Inherits:
Object
  • Object
show all
Defined in:
app/models/translated/translator.rb

Constant Summary collapse

API_HOST =
ENV.fetch('TRANSLATED_API_HOST', 'https://translatedrb.com')

Instance Method Summary collapse

Instance Method Details

#translate(text, from:, to:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
# File 'app/models/translated/translator.rb', line 9

def translate(text, from:, to:)
  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']
end