Skip to content

Commit

Permalink
specs
Browse files Browse the repository at this point in the history
detect language method
translate with language detection
  • Loading branch information
ck3g committed Dec 8, 2011
1 parent 75c9b25 commit c6749e1
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@ test.rb
*.#*
*~
*.gem
*.swp
2 changes: 2 additions & 0 deletions .rspec
@@ -0,0 +1,2 @@
--format Fuubar
--color
1 change: 1 addition & 0 deletions .rvmrc
@@ -0,0 +1 @@
rvm --create use ree@bing_translator
5 changes: 5 additions & 0 deletions Gemfile
@@ -0,0 +1,5 @@
source 'http://rubygems.org'

gem "rspec", "~> 2.7.0"
gem "nokogiri", "~> 1.5.0"
gem "fuubar"
27 changes: 27 additions & 0 deletions Gemfile.lock
@@ -0,0 +1,27 @@
GEM
remote: http://rubygems.org/
specs:
diff-lcs (1.1.3)
fuubar (0.0.6)
rspec (~> 2.0)
rspec-instafail (~> 0.1.8)
ruby-progressbar (~> 0.0.10)
nokogiri (1.5.0)
rspec (2.7.0)
rspec-core (~> 2.7.0)
rspec-expectations (~> 2.7.0)
rspec-mocks (~> 2.7.0)
rspec-core (2.7.1)
rspec-expectations (2.7.0)
diff-lcs (~> 1.1.2)
rspec-instafail (0.1.9)
rspec-mocks (2.7.0)
ruby-progressbar (0.0.10)

PLATFORMS
ruby

DEPENDENCIES
fuubar
nokogiri (~> 1.5.0)
rspec (~> 2.7.0)
File renamed without changes.
1 change: 1 addition & 0 deletions bing_translator.gemspec
Expand Up @@ -7,4 +7,5 @@ Gem::Specification.new do |s|
s.authors = ["Ricky Elrod"]
s.email = 'ricky@elrod.me'
s.files = ["lib/bing_translator.rb"]
s.add_dependency "nokogiri", "~> 1.5.0"
end
59 changes: 38 additions & 21 deletions lib/bing_translator.rb
Expand Up @@ -9,32 +9,49 @@

class BingTranslator
TRANSLATE_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Translate'
DETECT_URI = 'http://api.microsofttranslator.com/V2/Http.svc/Detect'

def initialize(api_key)
@api_key = api_key
@translate_uri = URI.parse TRANSLATE_URI
@detect_uri = URI.parse DETECT_URI
end

def translate(text, params = {})
if params[:to].nil? or params[:from].nil?
raise "Must provide :to and :from."
else
to = CGI.escape params[:to].to_s
from = CGI.escape params[:from].to_s
text = CGI.escape text.to_s
uri = URI.parse(TRANSLATE_URI)
params = {
'to' => to,
'from' => from,
'text' => text,
'appId' => @api_key,
'category' => 'general',
'contentType' => 'text/plain'
}
params_s = params.map {|key, value| "#{key}=#{value}"}.join '&'
result = Net::HTTP.new(uri.host, uri.port)
result = result.get("#{uri.path}?#{params_s}")
noko = Nokogiri.parse(result.body)
noko.xpath("//xmlns:string")[0].content
end
raise "Must provide :to." if params[:to].nil?

from = CGI.escape params[:from].to_s
params = {
'to' => CGI.escape(params[:to].to_s),
'text' => CGI.escape(text.to_s),
'appId' => @api_key,
'category' => 'general',
'contentType' => 'text/plain'
}
params[:from] = from unless from.empty?
result = result @translate_uri, params

Nokogiri.parse(result.body).xpath("//xmlns:string")[0].content
end

def detect(text)
params = {
'text' => CGI.escape(text.to_s),
'appId' => @api_key,
'category' => 'general',
'contentType' => 'text/plain'
}
result = result @detect_uri, params

Nokogiri.parse(result.body).xpath("//xmlns:string")[0].content.to_sym
end

private
def prepare_param_string(params)
params.map {|key, value| "#{key}=#{value}"}.join '&'
end

def result(uri, params)
result = Net::HTTP.new(uri.host, uri.port).get("#{uri.path}?#{prepare_param_string(params)}")
end
end
42 changes: 42 additions & 0 deletions spec/bing_translator_spec.rb
@@ -0,0 +1,42 @@
require 'lib/bing_translator.rb'
$KCODE='u'

describe BingTranslator do
before(:each) do
@message_en = "This message should be translated"
@translator = BingTranslator.new "Your_API_Key"
end

it "should translate text" do
result = @translator.translate @message_en, :from => :en, :to => :ru
result.should == "Это сообщение должно быть переведено"

result = @translator.translate @message_en, :from => :en, :to => :fr
result.should == "Ce message devrait être traduit."

result = @translator.translate @message_en, :from => :en, :to => :de
result.should == "Diese Nachricht sollte übersetzt werden"
end

it "should translate text with language autodetection" do
result = @translator.translate @message_en, :to => :ru
result.should == "Это сообщение должно быть переведено"

result = @translator.translate "Ce message devrait être traduit", :to => :en
result.should == @message_en

result = @translator.translate "Diese Nachricht sollte übersetzt werden", :to => :en
result.should == @message_en
end

it "should detect language by passed text" do
result = @translator.detect @message_en
result.should == :en

result = @translator.detect "Это сообщение должно быть переведено"
result.should == :ru

result = @translator.detect "Diese Nachricht sollte übersetzt werden"
result.should == :de
end
end

0 comments on commit c6749e1

Please sign in to comment.