Sha256: f7a6719c0c9006ceab804a6db0c97ba73102153c232429aa7a0a0b8441095060

Contents?: true

Size: 921 Bytes

Versions: 1

Compression:

Stored size: 921 Bytes

Contents

module Nele
  class BabelfishTranslator < Nele::GenericTranslator
    attr_reader :post_form

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

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

    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

    private
    def create_post_form(str)
      {
        'ei' => 'UTF-8',
        'doit' => 'done',
        'fr' => 'bf-res',
        'intl' => 1,
        'tt' => 'urltext',
        'trtext' => str,
        'lp' => config[:lp],
        'btnTrTxt' => 'Translate'
      }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nele-0.2.2 lib/nele/babelfish_translator.rb