Permalink
Browse files

Translate works

still needs better test coverage
  • Loading branch information...
1 parent 5d4d120 commit 1499a6c1b73f9a3f999be91229b25f3036ccdd48 LupineDev committed Jun 11, 2012
@@ -1,6 +1,8 @@
require 'microsoft_translator/version'
require 'uri'
+require 'json'
require 'rest-client'
+require 'nokogiri'
module MicrosoftTranslator
autoload :Client, 'microsoft_translator/client'
@@ -1,4 +1,3 @@
-require 'json'
module MicrosoftTranslator
class AzureAuthentication
AUTH_URL = 'https://datamarket.accesscontrol.windows.net/v2/OAuth2-13'
@@ -6,8 +6,38 @@ def initialize(client_id, client_secret)
raise "client_id must be a string" unless @client_id.is_a?(String)
@client_secret = client_secret
raise "client_secret must be a string" unless @client_secret.is_a?(String)
-
@auth = MicrosoftTranslator::AzureAuthentication.new(client_id, client_secret)
end
+
+ def translate(text, from_lang, to_lang, content_type)
+ response = RestClient.get(
+ "http://api.microsofttranslator.com/V2/Http.svc/Translate",
+ translate_params(text, from_lang, to_lang, content_type)
+ )
+ parse_xml(response.body)
+ end
+
+ private
+
+ def base_params
+ {:Authorization => "Bearer #{@auth.current_token}"}
+ end
+
+ def translate_params(text, from_lang, to_lang, content_type)
+ hash = base_params
+ hash.store(:params,{
+ "text" => text,
+ "from" => from_lang,
+ "to" => to_lang,
+ "contentType" => content_type
+ })
+ puts hash
+ hash
+ end
+
+ def parse_xml(xml)
+ xml_doc = Nokogiri::XML(xml)
+ xml_doc.xpath("/").text
+ end
end
end
@@ -15,6 +15,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = MicrosoftTranslator::VERSION
gem.add_runtime_dependency "rest-client", [">= 1.6.0"]
+ gem.add_runtime_dependency "nokogiri", [">= 1.4.0"]
gem.add_development_dependency "rspec", [">= 2.0.0"]
gem.add_development_dependency "timecop", [">= 0.3.5"]
end
@@ -3,9 +3,20 @@
describe MicrosoftTranslator::Client do
describe "#initialize" do
it "should build a new AzureAuthentication with the id and secret" do
+ stub_auth_request
MicrosoftTranslator::AzureAuthentication.should_receive(:new).with('id','secret')
translator = MicrosoftTranslator::Client.new('id', 'secret')
end
end
+ describe "#translate" do
+ it "should send a request with the correct params" do
+ pending "better way to test this"
+ stub_auth_request
+ translator = MicrosoftTranslator::Client.new('id', 'secret')
+ spanish = "no corras muchacho"
+ stub_translate_request("don't run boy")
+ translator.translate(spanish,"es","en","text/html").should eq("don't run boy")
+ end
+ end
end
@@ -11,3 +11,9 @@ def stub_auth_request(returned_token=nil)
RestClient.stub(:post).and_return(response)
end
+
+def stub_translate_request(translated_text=nil)
+ response = stub("response", :body => "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">#{translated_text || "hello world"}</string>")
+
+ RestClient.stub(:get).and_return(response)
+end

0 comments on commit 1499a6c

Please sign in to comment.