Class: Nele::BabelfishTranslator

Inherits:
GenericTranslator show all
Defined in:
lib/nele/babelfish_translator.rb

Instance Attribute Summary collapse

Attributes inherited from GenericTranslator

#config, #uri

Instance Method Summary collapse

Methods inherited from GenericTranslator

build_translator_class_name, check_inheritance, #initialize, #name, #parse_uri, #translate

Constructor Details

This class inherits a constructor from Nele::GenericTranslator

Instance Attribute Details

#post_form ⇒ Object (readonly)

Returns the value of attribute post_form.



3
4
5
# File 'lib/nele/babelfish_translator.rb', line 3

def post_form
  @post_form
end

Instance Method Details

#build_request(str) ⇒ Object



5
6
7
8
# File 'lib/nele/babelfish_translator.rb', line 5

def build_request(str)
  @uri = parse_uri(config[:url])
  @post_form = create_post_form(str)
end

#connect ⇒ Object



10
11
12
13
14
15
16
# File 'lib/nele/babelfish_translator.rb', line 10

def connect
  begin
    Net::HTTP.post_form(uri, post_form)
  rescue => err
    raise Nele::ConnectionError, err
  end
end

#parse_response(res) ⇒ Object



18
19
20
21
22
23
24
25
26
# File 'lib/nele/babelfish_translator.rb', line 18

def parse_response(res)
  body = res.body
  tag = body.match(/<div\sid=\"result\"[^>]*>(.*?)<\/div><\/div>/)
  if tag && tag.length > 0
    tag[1].match(/>(.*)$/)[0].gsub(">", "")
  else
    raise TranslatorAPIError, "error"
  end
end