Class: Mymemory::Translation
- Inherits:
-
Object
- Object
- Mymemory::Translation
- Defined in:
- lib/mymemory/translation.rb
Constant Summary collapse
- BASE_URL =
'http://mymemory.translated.net/api/get'
Instance Attribute Summary collapse
-
#from ⇒ Object
Returns the value of attribute from.
-
#response_json ⇒ Object
Returns the value of attribute response_json.
-
#text ⇒ Object
Returns the value of attribute text.
-
#to ⇒ Object
Returns the value of attribute to.
Instance Method Summary collapse
- #email_key ⇒ Object
-
#initialize(text, opts) ⇒ Translation
constructor
A new instance of Translation.
- #lang_pair ⇒ Object
- #parsed_response ⇒ Object
- #translated_text ⇒ Object
- #url ⇒ Object
Constructor Details
#initialize(text, opts) ⇒ Translation
Returns a new instance of Translation.
10 11 12 13 14 |
# File 'lib/mymemory/translation.rb', line 10 def initialize(text, opts) @text = text @from = opts.fetch(:from, :en) @to = opts[:to] or raise LanguageMissingError.new end |
Instance Attribute Details
#from ⇒ Object
Returns the value of attribute from.
8 9 10 |
# File 'lib/mymemory/translation.rb', line 8 def from @from end |
#response_json ⇒ Object
Returns the value of attribute response_json.
8 9 10 |
# File 'lib/mymemory/translation.rb', line 8 def response_json @response_json end |
#text ⇒ Object
Returns the value of attribute text.
8 9 10 |
# File 'lib/mymemory/translation.rb', line 8 def text @text end |
#to ⇒ Object
Returns the value of attribute to.
8 9 10 |
# File 'lib/mymemory/translation.rb', line 8 def to @to end |
Instance Method Details
#email_key ⇒ Object
24 25 26 |
# File 'lib/mymemory/translation.rb', line 24 def email_key config_email && "&de=#{config_email}" end |
#lang_pair ⇒ Object
20 21 22 |
# File 'lib/mymemory/translation.rb', line 20 def lang_pair [from, to].join('|') end |
#parsed_response ⇒ Object
32 33 34 |
# File 'lib/mymemory/translation.rb', line 32 def parsed_response response.code == 200 ? JSON.parse(response.body) : {} end |
#translated_text ⇒ Object
28 29 30 |
# File 'lib/mymemory/translation.rb', line 28 def translated_text parsed_response['responseData']['translatedText'] end |
#url ⇒ Object
16 17 18 |
# File 'lib/mymemory/translation.rb', line 16 def url URI.escape("#{BASE_URL}?q=#{text}&langpair=#{lang_pair}#{email_key}") end |