Class: Translation
- Inherits:
-
Object
- Object
- Translation
- Defined in:
- lib/command_translate/translation.rb
Instance Attribute Summary collapse
-
#from ⇒ Object
Returns the value of attribute from.
-
#text ⇒ Object
Returns the value of attribute text.
-
#to ⇒ Object
Returns the value of attribute to.
-
#translation ⇒ Object
Returns the value of attribute translation.
Instance Method Summary collapse
-
#initialize(text, from, to) ⇒ Translation
constructor
A new instance of Translation.
- #translate ⇒ Object
- #url ⇒ Object
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
#from ⇒ Object
Returns the value of attribute from.
4 5 6 |
# File 'lib/command_translate/translation.rb', line 4 def from @from end |
#text ⇒ Object
Returns the value of attribute text.
4 5 6 |
# File 'lib/command_translate/translation.rb', line 4 def text @text end |
#to ⇒ Object
Returns the value of attribute to.
4 5 6 |
# File 'lib/command_translate/translation.rb', line 4 def to @to end |
#translation ⇒ Object
Returns the value of attribute translation.
4 5 6 |
# File 'lib/command_translate/translation.rb', line 4 def translation @translation end |
Instance Method Details
#translate ⇒ Object
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 |
#url ⇒ Object
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 |