Class: Translation

Inherits:
Object
  • Object
show all
Defined in:
lib/command_translate/translation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text, from, to) ⇒ Translation

Returns a new instance of Translation.



6
7
8
9
10
11
# File 'lib/command_translate/translation.rb', line 6

def initialize(text, from, to)
  @text = text
  @from = from
  @to = to
  @translation = translate.squeeze(" ")
end

Instance Attribute Details

#fromObject

Returns the value of attribute from.



4
5
6
# File 'lib/command_translate/translation.rb', line 4

def from
  @from
end

#textObject

Returns the value of attribute text.



4
5
6
# File 'lib/command_translate/translation.rb', line 4

def text
  @text
end

#toObject

Returns the value of attribute to.



4
5
6
# File 'lib/command_translate/translation.rb', line 4

def to
  @to
end

#translationObject

Returns the value of attribute translation.



4
5
6
# File 'lib/command_translate/translation.rb', line 4

def translation
  @translation
end

Instance Method Details

#translateObject



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/command_translate/translation.rb', line 13

def translate
  encoded_url = URI.encode(url)
  response = Net::HTTP.get(URI(encoded_url))

  # use JSON parser to avoid to use eval
  translations = JSON.parse(response.squeeze(","))

  if translations[0].size > 1
    translations[0].collect {|x| x[0] }.join(" ")
  else
    translations[1][0][1].nil? ? translations[0][0][0] : translations[1][0][1].join(", ")
  end
end

#urlObject



27
28
29
# File 'lib/command_translate/translation.rb', line 27

def url
  "http://translate.google.com.br/translate_a/t?client=t&text=#{text}&hl=#{to}&sl=#{from}&ie=UTF-8&oe=UTF-8"
end