Class: Glosbe::Translate
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(from, dest = 'eng') ⇒ Translate
constructor
A new instance of Translate.
- #translate(phrase) ⇒ Object
- #translate_and_definition(phrase) ⇒ Object
Constructor Details
#initialize(from, dest = 'eng') ⇒ Translate
Returns a new instance of Translate.
10 11 12 13 |
# File 'lib/glosbe.rb', line 10 def initialize(from, dest='eng') raise(MissingFromLanguage) if from.nil? = { query: {from: from, dest: dest }} end |
Class Method Details
.Exception(*names) ⇒ Object
15 16 17 18 |
# File 'lib/glosbe.rb', line 15 def self.Exception(*names) cl = Module === self ? self : Object names.each {|n| cl.const_set(n, Class.new(Exception))} end |
Instance Method Details
#translate(phrase) ⇒ Object
23 24 25 |
# File 'lib/glosbe.rb', line 23 def translate(phrase) translate_and_definition(phrase)[:translated] end |
#translate_and_definition(phrase) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/glosbe.rb', line 27 def translate_and_definition(phrase) raise(MissingPhraseLanguage) if phrase.nil? [:query][:phrase] = phrase response = self.class.get('/translate', ) response = (response && response.parsed_response) ? response.parsed_response : nil raise(TranslateServerIsDown) if (!response || response.empty?) target_definitions = [] source_definitions = [] translated = (response['tuc'].first && response['tuc'].first['phrase']) ? response['tuc'].first['phrase']['text'] : nil coder = HTMLEntities.new response['tuc'].each do |tranlation_block| if tranlation_block['meanings'] tranlation_block['meanings'].each do |meaning| if meaning['language'] == [:query][:dest] target_definitions << coder.decode(meaning['text']) else source_definitions << coder.decode(meaning['text']) end end end end {target_definitions: target_definitions, source_definitions: source_definitions, translated: translated} end |