Created
September 2, 2011 08:48
-
-
Save jzajpt/1188209 to your computer and use it in GitHub Desktop.
Google Translate Ruby API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
class GoogleTranslate | |
BASE_URL = 'https://www.googleapis.com/language/translate/v2' | |
URL_OPTIONS = [ :key, :target, :source, :q, :format ] | |
include HTTParty | |
format :json | |
attr_accessor :key | |
attr_accessor :source | |
attr_accessor :target | |
def initialize(key, options = {}) | |
@key = key | |
@source = options[:source] | |
@target = options[:target] | |
end | |
def translate(string, options = {}) | |
Rails.logger.info "GoogleTranslate.translate: #{string}" | |
response = self.class.post(BASE_URL, self.options_for_request(string, options)) | |
translation_from_response(response) | |
end | |
protected | |
def options_for_request(string, options = {}) | |
target = options[:target] || self.target | |
source = options[:source] || self.source | |
params = options.merge(key: key, q: string, target: target) | |
params[:source] = source if source | |
{ body: params, headers: { 'X-HTTP-Method-Override' => 'GET' } } | |
end | |
def translation_from_response(response) | |
raise "Invalid response" unless response.kind_of?(Hash) | |
raise "API returned error!" if response["error"] | |
raise "Translation data not present!" unless response["data"] | |
translations = response["data"]["translations"].map { |tr| tr["translatedText"] } | |
translations.length == 1 ? translations.first : translations | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# encoding: utf-8 | |
require 'spec_helper' | |
describe GoogleTranslate do | |
describe "#translate" do | |
let(:key) { 'MyKey1234' } | |
let(:translate) { GoogleTranslate.new(key, source: 'cs', target: 'en') } | |
let(:headers) { { headers: {"X-HTTP-Method-Override"=>"GET" } } } | |
context "with single query given" do | |
let(:response_data) { { | |
"data" => { | |
"translations" => [ | |
{ "translatedText" => "Hello" } | |
] | |
} | |
} } | |
before do | |
GoogleTranslate.stub(:post).and_return(response_data) | |
end | |
it "uses source language from instance variable" do | |
GoogleTranslate.should_receive(:post).with(anything, hash_including(body: hash_including(source: 'cs'))) | |
translate.translate('ahoj') | |
end | |
it "uses source language from option when given" do | |
GoogleTranslate.should_receive(:post).with(anything, hash_including(body: hash_including(source: 'da'))) | |
translate.translate('ahoj', source: 'da') | |
end | |
it "uses target language from instance variable" do | |
GoogleTranslate.should_receive(:post).with(anything, hash_including(body: hash_including(target: 'en'))) | |
translate.translate('ahoj') | |
end | |
it "uses target language from option when given" do | |
GoogleTranslate.should_receive(:post).with(anything, hash_including(body: hash_including(target: 'nl'))) | |
translate.translate('ahoj', target: 'nl') | |
end | |
it "uses X-HTTP-Method-Override header" do | |
GoogleTranslate.should_receive(:post).with(anything, hash_including(headers)) | |
translate.translate('ahoj') | |
end | |
it "uses BASE_URL" do | |
GoogleTranslate.should_receive(:post).with(GoogleTranslate::BASE_URL, anything) | |
translate.translate('ahoj') | |
end | |
it "adds options to body" do | |
api_options = { key: key, q: 'ahoj', target: 'en', source: 'cs' } | |
GoogleTranslate.should_receive(:post).with(anything, hash_including(body: api_options)).and_return(response_data) | |
translate.translate('ahoj') | |
end | |
it "returns translation from Google Translate API" do | |
translation = translate.translate('ahoj') | |
translation.should eq("Hello") | |
end | |
end | |
context "with multiple query given" do | |
let(:response_data) { { | |
"data" => { | |
"translations" => [ | |
{ "translatedText" => "Hello" }, | |
{ "translatedText" => "Goodbye" } | |
] | |
} | |
} } | |
before do | |
GoogleTranslate.stub(:post).and_return(response_data) | |
end | |
it "returns translation from Google Translate API in an array" do | |
translation = translate.translate(%w(ahoj nashledanou)) | |
translation.should eq(["Hello", "Goodbye"]) | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment