Browse files

refactoring of the google api call into ApiCall module

  • Loading branch information...
1 parent 1feb4f5 commit 0d238cf4f1523cca96d852c2f028bb20ecd354b9 Elise Huard committed Jan 23, 2009
View
2 VERSION.yml
@@ -1,4 +1,4 @@
---
:minor: 0
-:patch: 2
+:patch: 3
:major: 0
View
3 lib/google_translate.rb
@@ -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'
View
22 lib/google_translate/api_call.rb
@@ -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
View
3 lib/google_translate/exceptions.rb
@@ -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
View
21 lib/google_translate/language_detect.rb
@@ -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
View
24 lib/google_translate/translator.rb
@@ -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="
@@ -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

0 comments on commit 0d238cf

Please sign in to comment.