module Google::API::InstanceMethods

Public Instance Methods

get_uri() click to toggle source
# File lib/google/api.rb, line 32
def get_uri
  url = "#{URL}?q=#{text}"
  url += "&target=#{target}" if target
  url += "&source=#{source}" if source
  url += "&key=#{key}"
  normalize_uri(url)
end
google_translate() click to toggle source
# File lib/google/api.rb, line 14
def google_translate
  uri = get_uri
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = Net::HTTP::Get.new(uri.request_uri)
  response = http.request(request)

  translated_date_text(Oj.load(response.body))
end
normalize_uri(uri) click to toggle source
# File lib/google/api.rb, line 40
def normalize_uri(uri)
  return uri if uri.is_a? URI

  uri = uri.to_s
  uri, *tail = uri.rpartition "#" if uri["#"]

  URI(URI.encode(uri) << Array(tail).join)
end
translated_date_text(body) click to toggle source
# File lib/google/api.rb, line 26
def translated_date_text(body)
  body['data']['translations'].first['translatedText']
rescue
  ''
end