Skip to content

Commit

Permalink
refactoring of the google api call into ApiCall module
Browse files Browse the repository at this point in the history
  • Loading branch information
Elise Huard committed Jan 23, 2009
1 parent 1feb4f5 commit 0d238cf
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 41 deletions.
2 changes: 1 addition & 1 deletion VERSION.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
:minor: 0
:patch: 2
:patch: 3
:major: 0
3 changes: 3 additions & 0 deletions lib/google_translate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
#
# look at the Translator and LanguageDetect class (or README file) for instructions
module GoogleTranslate
URL_STRING = "http://ajax.googleapis.com/ajax/services/language/"
VERSION = "1.0"
end

require 'google_translate/exceptions'
require 'google_translate/languages'
require 'google_translate/parsed_response'
require 'google_translate/api_call'
require 'google_translate/translator'
require 'google_translate/language_detect'
22 changes: 22 additions & 0 deletions lib/google_translate/api_call.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module GoogleTranslate
module ApiCall
def google_api_call(text,params,klas)
raise NoGivenString if text.nil?

request = URL_STRING + params + CGI.escape(text)
response = ''
open(request) { |f|
response = f.read
}
raise GoogleUnavailable if response.nil? || response == ""

parsed_response = klas.new(response)
raise GoogleException, parsed_response.details if parsed_response.status != 200 # success

parsed_response # return response class

rescue OpenURI::HTTPError
raise GoogleUnavailable
end
end
end
3 changes: 0 additions & 3 deletions lib/google_translate/exceptions.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
module GoogleTranslate

URL_STRING = "http://ajax.googleapis.com/ajax/services/language/"
VERSION = "1.0"

# Invalid language (or more specifically not one in existing list)
class InvalidLanguage < Exception
end
Expand Down
21 changes: 4 additions & 17 deletions lib/google_translate/language_detect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,14 @@ module GoogleTranslate
# <b>how to use</b>:
# language = LanguageDetect.detect("il fait beau aujourd'hui") # returns "fr"
class LanguageDetect
extend ApiCall
SERVICE = "detect?v=#{VERSION}&q="

# detect the language of a given text.
def self.detect(text)
raise NoGivenString if text.nil?

request = URL_STRING + SERVICE + CGI.escape(text)
response = ''
open(request) { |f|
response = f.read
}
raise GoogleUnavailable if response.nil? || response == ""

parsed = DetectResponse.new(response)
raise GoogleException, parsed.details if parsed.status != 200
raise UnreliableDetection if !parsed.is_reliable

parsed.language # return value

rescue OpenURI::HTTPError
raise GoogleUnavailable
response = google_api_call(text,"#{SERVICE}",DetectResponse)
raise UnreliableDetection if !response.is_reliable
response.language # return value
end
end

Expand Down
24 changes: 4 additions & 20 deletions lib/google_translate/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ module GoogleTranslate
# translator = Translator.new("en","fr") # so several translations can be performed with this new object
# result = translator.translate("nice day today") # returns "belle journée aujourd'hui"
class Translator
include ApiCall

SERVICE = "translate?v=#{VERSION}&langpair="
TEXT_PAR = "&q="

Expand All @@ -20,27 +22,9 @@ def initialize(from,to)
# - html: if html encoding is desirable (for immediate display on a web page for instance)
# then this option needs to have a true value (:html => true)
def translate(text, options = {})
raise NoGivenString if text.nil?

request = URL_STRING + SERVICE + @from + "%7C" + @to + TEXT_PAR + CGI.escape(text)
response = ''
open(request) { |f|
response = f.read
}
raise GoogleUnavailable if response.nil? || response == ""

parsed = TranslationResponse.new(response)
raise GoogleException, parsed.details if parsed.status != 200 # success

if options[:html]
translation = parsed.translation
else
translation = CGI.unescapeHTML(parsed.translation)
end
response = google_api_call(text,"#{SERVICE}#{@from}%7C#{@to}#{TEXT_PAR}",TranslationResponse)
translation = options[:html] ? response.translation : CGI.unescapeHTML(response.translation)
translation # return value

rescue OpenURI::HTTPError
raise GoogleUnavailable
end

private
Expand Down

0 comments on commit 0d238cf

Please sign in to comment.