Robby Russell
Posts: 981
Nickname: matchboy
Registered: Apr, 2005
Robby Russell is the Founder & Executive Director PLANET ARGON, a Ruby on Rails development firm
HTTParty goes foreign
Posted: Mar 16, 2009 1:35 AM
This post originated from an RSS feed registered with Ruby Buzz
by Robby Russell.
Original Post: HTTParty goes foreign
Feed Title: Robby on Rails
Feed URL: http://feeds.feedburner.com/RobbyOnRails
Feed Description: Ruby on Rails development, consulting, and hosting from the trenches...
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Robby Russell
Latest Posts From Robby on Rails
Advertisement
Just a quick post to get share something I was tinkering with this evening.
I came across this post by Gerald Bauer , which shows you how to use the Google Translation API with Ruby via Net::HTTP. I thought I’d play with the service with HTTParty .
class GoogleApi
include HTTParty
base_uri ' ajax.googleapis.com '
def self.translate ( string =" ", to =" ", from =" en ")
get (" /ajax/services/language/translate ", :query => { :langpair => " #{from} |#{to} ", :q => string , :v => 1.0 })
end
end
A few examples from playing with it.
>> GoogleApi . translate (' bonjour ', ' en ', ' fr ')
=> " {\" responseData\" : {\" translatedText\" :\" hello\" }, \" responseDetails\" : null, \" responseStatus\" : 200} "
>> GoogleApi . translate (' Red wine ', ' fr ')
=> " {\" responseData\" : {\" translatedText\" :\" Vin rouge\" ,\" detectedSourceLanguage\" :\" en\" }, \" responseDetails\" : null, \" responseStatus\" : 200} "
>> GoogleApi . translate (' Where is the bathroom? ', ' es ')
=> " {\" responseData\" : {\" translatedText\" :\"\302\277 D\303\263 nde est\303\241 el ba\303\261 o?\" ,\" detectedSourceLanguage\" :\" en\" }, \" responseDetails\" : null, \" responseStatus\" : 200} "
>> GoogleApi . translate (' Good morning ', ' it ')
=> " {\" responseData\" : {\" translatedText\" :\" Buon giorno\" ,\" detectedSourceLanguage\" :\" en\" }, \" responseDetails\" : null, \" responseStatus\" : 200} "
What a party !
>> GoogleApi . translate (' party ', ' it ')
=> " {\" responseData\" : {\" translatedText\" :\" festa\" ,\" detectedSourceLanguage\" :\" en\" }, \" responseDetails\" : null, \" responseStatus\" : 200} "
>> GoogleApi . translate (' party ', ' es ')
=> " {\" responseData\" : {\" translatedText\" :\" fiesta\" ,\" detectedSourceLanguage\" :\" en\" }, \" responseDetails\" : null, \" responseStatus\" : 200} "
Look how easy that was. :-)
For a previous post on using this gem, read The HTTParty has just begun .
Read: HTTParty goes foreign