Class: Telegem::Plugins::Translate

Inherits:
Object
  • Object
show all
Defined in:
lib/plugins/translate.rb

Instance Method Summary collapse

Constructor Details

#initialize(word, from, to) ⇒ Translate

Returns a new instance of Translate.



6
7
8
9
10
11
# File 'lib/plugins/translate.rb', line 6

def initialize(word, from, to)
  @word = word
  @from = from
  @to = to
  start_translating
end

Instance Method Details

#start_translatingObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/plugins/translate.rb', line 13

def start_translating
  url = "https://zen-drx-api.onrender.com/api/translate"
  options = {
    body: {
      word: @word,
      from: @from,
      to: @to
    }.to_json,
    headers: {
      'Content-Type' => 'application/json'
    }
  }
  response = HTTParty.post(url, options)
  if response.success?
    translation = response.parsed_response["translation"]
     {
       error: "false",
       translation: translation
     }
  else 
    {
      error: "an error occurred",
      code: "#{response.code}"
    }
  end 
end