Sha256: f337fe5a928f41d5d9c14de8ffba343c1d1d7a86d0982075288da168b5bc18f8

Contents?: true

Size: 942 Bytes

Versions: 1

Compression:

Stored size: 942 Bytes

Contents

require 'open-uri'

module Tr4n5l4te
  class Translator
    START_PAGE = 'https://translate.google.com'.freeze

    attr_reader :sleep_time, :agent

    def initialize(args = {})
      @sleep_time = args.fetch(:sleep_time, 2)
      @agent = Agent.new
    end

    def translate(text, from_lang, to_lang)
      return '' if text.nil? || text == ''
      text.strip!
      return '' if text == ''
      encoded_text = URI.encode(text)
      url = "#{START_PAGE}/##{from_lang}/#{to_lang}/#{encoded_text}"
      load_cookies
      agent.visit(url)
      store_cookies
      sleep_default
      result_box = browser.find('#result_box')
      result_box.text
    end

    private

    def store_cookies
      agent.store_cookies(Tr4n5l4te.cookie_file)
    end

    def load_cookies
      agent.load_cookies(Tr4n5l4te.cookie_file)
    end

    def sleep_default
      sleep(sleep_time)
    end

    def browser
      agent.browser
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tr4n5l4te-0.1.2 lib/tr4n5l4te/translator.rb