Browse files

some refactoring

  • Loading branch information...
1 parent b12f874 commit 4b84461d6239f0d51ed7520b878e9961b3f7a3d8 Elise Huard committed Jan 22, 2009
View
52 README
@@ -1,27 +1,3 @@
-Copyright (c) 2009 Elise Huard
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*****************************************************************
-
google translate API
This is based on the documentation found here
http://code.google.com/apis/ajaxlanguage/documentation/#Versioning
@@ -35,8 +11,7 @@ translator = Translator.new("en","fr") # so several translations can be performe
result = translator.translate("nice day today") # returns "belle journée aujourd'hui"
language detection:
-detector = LanguageDetect.new
-language = detector.detect("il fait beau aujourd'hui") # returns "fr"
+language = LanguageDetect.detect("il fait beau aujourd'hui") # returns "fr"
TODO
@@ -48,3 +23,28 @@ TODO
Look at specs for behaviour.
Suggestions are welcome !
+
+License (MIT)
+==============
+Copyright (c) 2009 Elise Huard
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
View
4 VERSION.yml
@@ -1,4 +1,4 @@
---
-:minor: 1
-:patch: 0
+:minor: 0
+:patch: 2
:major: 0
View
6 google_translate.gemspec
@@ -2,14 +2,14 @@
Gem::Specification.new do |s|
s.name = %q{google_translate}
- s.version = "0.0.1"
+ s.version = "0.0.2"
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
s.authors = ["Elise Huard"]
- s.date = %q{2009-01-21}
+ s.date = %q{2009-01-22}
s.description = %q{Google Translate Ruby API - based on google documentation}
s.email = %q{googletranslate@elisehuard.be}
- s.files = ["lib/google_translate", "lib/google_translate/exceptions.rb", "lib/google_translate/language_detect.rb", "lib/google_translate/languages.rb", "lib/google_translate/parsed_response.rb", "lib/google_translate/translator.rb", "lib/google_translate.rb", "spec/spec_helper.rb", "spec/translate_spec.rb"]
+ s.files = ["VERSION.yml", "lib/google_translate", "lib/google_translate/exceptions.rb", "lib/google_translate/language_detect.rb", "lib/google_translate/languages.rb", "lib/google_translate/parsed_response.rb", "lib/google_translate/translator.rb", "lib/google_translate.rb", "spec/detect_spec.rb", "spec/spec_helper.rb", "spec/translate_spec.rb"]
s.has_rdoc = true
s.homepage = %q{http://github.com/elisehuard/google_translate}
s.rdoc_options = ["--inline-source", "--charset=UTF-8"]
View
11 lib/google_translate/language_detect.rb
@@ -1,13 +1,12 @@
module GoogleTranslate
# manage language detection based on a given string of text
# <b>how to use</b>:
- # detector = LanguageDetect.new<br/>
- # language = detector.detect("il fait beau aujourd'hui") # returns "fr"
+ # language = LanguageDetect.detect("il fait beau aujourd'hui") # returns "fr"
class LanguageDetect
SERVICE = "detect?v=#{VERSION}&q="
# detect the language of a given text.
- def detect(text)
+ def self.detect(text)
raise NoGivenString if text.nil?
request = URL_STRING + SERVICE + CGI.escape(text)
@@ -33,9 +32,9 @@ class DetectResponse < ParsedResponse
attr_reader :language, :is_reliable, :confidence
def initialize(string)
super(string)
- @language = @json['responseData']['language']
- @is_reliable = @json['responseData']['isReliable']
- @confidence = @json['confidence']
+ @language = @response_data['language']
+ @is_reliable = @response_data['isReliable']
+ @confidence = @response_data['confidence']
end
end
end
View
7 lib/google_translate/parsed_response.rb
@@ -3,9 +3,10 @@ module GoogleTranslate
class ParsedResponse
attr_reader :status, :details
def initialize(string)
- @json = JSON.parse(string)
- @status = @json['responseStatus']
- @details = @json['responseDetails']
+ json = JSON.parse(string)
+ @status = json['responseStatus']
+ @details = json['responseDetails']
+ @response_data = json['responseData']
end
end
end
View
2 lib/google_translate/translator.rb
@@ -56,7 +56,7 @@ def initialize(string)
super(string)
# here we unescape html escaped characters BUT if it's required to keep html format,
# an extra option should be added
- @translation = @json['responseData']['translatedText'] if @json['responseData']
+ @translation = @response_data['translatedText'] if @response_data
end
end
end
View
19 spec/detect_spec.rb
@@ -0,0 +1,19 @@
+require File.dirname(__FILE__) + '/spec_helper'
+
+describe GoogleTranslate, 'detect' do
+ it "should detect the language of a string" do
+ LanguageDetect.detect("il fait beau aujourd'hui").should be == "fr"
+ end
+
+ it "should raise an error if no string" do
+ lambda {
+ LanguageDetect.detect(nil)
+ }.should raise_error(NoGivenString)
+ end
+
+ it "should raise an error if language not recognized" do
+ lambda {
+ LanguageDetect.detect("bliiiioaarg oqsdkqsdf")
+ }.should raise_error(UnreliableDetection)
+ end
+end
View
5 spec/spec_helper.rb
@@ -8,3 +8,8 @@
Spec::Runner.configure do |config|
config.mock_with :mocha
end
+
+require 'google_translate'
+include GoogleTranslate
+
+
View
24 spec/translate_spec.rb
@@ -1,7 +1,4 @@
require File.dirname(__FILE__) + '/spec_helper'
-require 'google_translate'
-
-include GoogleTranslate
describe GoogleTranslate, 'translate' do
it "should raise an error if the from language is missing or doesn't exist" do
@@ -44,24 +41,3 @@
result.should be == "belle journée aujourd&#39;hui"
end
end
-
-describe GoogleTranslate, 'detect' do
- it "should detect the language of a string" do
- detect = LanguageDetect.new
- detect.detect("il fait beau aujourd'hui").should be == "fr"
- end
-
- it "should raise an error if no string" do
- detect = LanguageDetect.new
- lambda {
- detect.detect(nil)
- }.should raise_error(NoGivenString)
- end
-
- it "should raise an error if language not recognized" do
- detect = LanguageDetect.new
- lambda {
- detect.detect("bliiiioaarg oqsdkqsdf")
- }.should raise_error(UnreliableDetection)
- end
-end

0 comments on commit 4b84461

Please sign in to comment.