Sha256: f78b4a274b687efc0ad3e2b0f99468334e3319d13400b199599ab750fcf809a3
Contents?: true
Size: 1.2 KB
Versions: 4
Compression:
Stored size: 1.2 KB
Contents
require 'net/https' require 'uri' require 'oj' module Google module API URL = 'https://www.googleapis.com/language/translate/v2' module ClassMethods end module InstanceMethods 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 def translated_date_text(body) body['data']['translations'].first['translatedText'] rescue '' end def get_uri url = "#{URL}?q=#{text}" url += "&target=#{target}" if target url += "&source=#{source}" if source url += "&key=#{key}" normalize_uri(url) end 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 end def self.included(receiver) receiver.extend ClassMethods receiver.send :include, InstanceMethods end end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
babel_i18n-0.0.6 | lib/google/api.rb |
babel_i18n-0.0.5 | lib/google/api.rb |
babel_i18n-0.0.4 | lib/google/api.rb |
babel_i18n-0.0.3 | lib/google/api.rb |