lib/tr4n5l4te/translator.rb in tr4n5l4te-0.1.5 vs lib/tr4n5l4te/translator.rb in tr4n5l4te-0.1.6
- old
+ new
@@ -1,10 +1,12 @@
+# frozen_string_literal: true
+
require 'open-uri'
module Tr4n5l4te
class Translator
- START_PAGE = 'https://translate.google.com'.freeze
+ START_PAGE = 'https://translate.google.com'
attr_reader :sleep_time, :agent
def initialize(args = {})
@sleep_time = args.fetch(:sleep_time, 2)
@@ -13,21 +15,22 @@
def translate(text, from_lang, to_lang)
puts "Translating: #{text}"
encoded_text = validate_and_encode(text)
return '' if encoded_text == ''
+
smart_visit(translator_url(encoded_text, from_lang, to_lang))
result_box = browser.find('.tlid-translation')
result_box.text
end
private
def validate_and_encode(text)
return '' if text.nil?
fail "Cannot translate a [#{text.class}]: '#{text}'" unless text.respond_to?(:gsub)
- text.strip!
- URI.encode(text)
+
+ CGI.escape(text.strip)
end
def smart_visit(url)
load_cookies
agent.visit(url)