module Google
Provides common functionality for both GoogleTranslator
and GoogleDictionary
Constants
- SPEECH_API_URL
- TEXT_API_URL
Public Instance Methods
get_data(text, source, target, options = {})
click to toggle source
# File lib/google.rb, line 20 def get_data(text, source, target, options = {}) return get_translation(text, source, target) unless options[:cache_results] data = { text: text, source: source, target: target, service: 'google' } entry = Translation.find_by(data) if entry.nil? data[:response] = get_translation(text, source, target) entry = Translation.create(data) end entry.response end
get_pronunciation(text, source, audio_file)
click to toggle source
# File lib/google.rb, line 6 def get_pronunciation(text, source, audio_file) File.open(audio_file, 'w') do |f| f.write(RestClient.get(SPEECH_API_URL, params: { tl: source, q: text })) end end
get_translation(text, source, target)
click to toggle source
# File lib/google.rb, line 12 def get_translation(text, source, target) headers = { params: { client: 'p', text: text, sl: source, tl: target }, user_agent: 'Mozilla/5.0' } RestClient.get(TEXT_API_URL, headers).to_s.force_encoding(Encoding::UTF_8) end