google_language 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Christian Hellsten
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.textile ADDED
@@ -0,0 +1,26 @@
1
+ h1. GoogleLanguage - A Ruby client for the Google AJAX Language API
2
+
3
+ h2. Installation
4
+
5
+ <pre>
6
+ <code>
7
+ $ gem sources -a http://gems.github.com (you only have to do this once)
8
+ $ sudo gem install christianhellsten-ruby-google-ajax-language
9
+ </code>
10
+ </pre>
11
+
12
+ h3. Usage
13
+
14
+ <pre>
15
+ <code>
16
+ require 'rubygems'
17
+ require 'google_language'
18
+
19
+ GoogleLanguage.translate('you', :from => 'en', :to => 'ja')
20
+ </code>
21
+ </pre>
22
+
23
+
24
+ h2. Author
25
+
26
+ "Christian Hellsten":http://christianhellsten.com ("Aktagon Ltd.":http://aktagon.com)
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "google_language"
8
+ gem.summary = %Q{A Ruby client for the Google AJAX Language API}
9
+ gem.description = %Q{http://code.google.com/intl/sv-SE/apis/ajaxlanguage/}
10
+ gem.email = "christian.hellsten@gmail.com"
11
+ gem.homepage = "http://github.com/christian.hellsten/ruby-google-ajax-language"
12
+ gem.authors = ["Christian Hellsten"]
13
+ gem.add_dependency "pauldix-typhoeus"
14
+ gem.add_dependency "json"
15
+ gem.add_development_dependency "thoughtbot-shoulda"
16
+ gem.add_development_dependency "jeremymcanally-matchy"
17
+ end
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ if File.exist?('VERSION')
49
+ version = File.read('VERSION')
50
+ else
51
+ version = ""
52
+ end
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "google_language #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
data/VERSION.yml ADDED
@@ -0,0 +1,5 @@
1
+ ---
2
+ :minor: 0
3
+ :build:
4
+ :patch: 0
5
+ :major: 1
@@ -0,0 +1,58 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{google_language}
8
+ s.version = "0.0.0"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Christian Hellsten"]
12
+ s.date = %q{2009-10-13}
13
+ s.description = %q{http://code.google.com/intl/sv-SE/apis/ajaxlanguage/}
14
+ s.email = %q{christian.hellsten@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.textile"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "Rakefile",
24
+ "lib/google_language.rb",
25
+ "test/test_helper.rb"
26
+ ]
27
+ s.homepage = %q{http://github.com/christian.hellsten/ruby-google-ajax-language}
28
+ s.rdoc_options = ["--charset=UTF-8"]
29
+ s.require_paths = ["lib"]
30
+ s.rubygems_version = %q{1.3.5}
31
+ s.summary = %q{A Ruby client for the Google AJAX Language API}
32
+ s.test_files = [
33
+ "test/test_google_language.rb",
34
+ "test/test_helper.rb"
35
+ ]
36
+
37
+ if s.respond_to? :specification_version then
38
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
39
+ s.specification_version = 3
40
+
41
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
42
+ s.add_runtime_dependency(%q<pauldix-typhoeus>, [">= 0"])
43
+ s.add_runtime_dependency(%q<json>, [">= 0"])
44
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
45
+ s.add_development_dependency(%q<jeremymcanally-matchy>, [">= 0"])
46
+ else
47
+ s.add_dependency(%q<pauldix-typhoeus>, [">= 0"])
48
+ s.add_dependency(%q<json>, [">= 0"])
49
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
50
+ s.add_dependency(%q<jeremymcanally-matchy>, [">= 0"])
51
+ end
52
+ else
53
+ s.add_dependency(%q<pauldix-typhoeus>, [">= 0"])
54
+ s.add_dependency(%q<json>, [">= 0"])
55
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
56
+ s.add_dependency(%q<jeremymcanally-matchy>, [">= 0"])
57
+ end
58
+ end
@@ -0,0 +1,62 @@
1
+ require 'typhoeus'
2
+ require 'json'
3
+ require 'uri'
4
+
5
+ #
6
+ # http://code.google.com/apis/ajaxlanguage/documentation
7
+ #
8
+ class GoogleLanguageError < RuntimeError; end
9
+
10
+ class GoogleLanguage
11
+ include Typhoeus
12
+
13
+ URL = 'http://ajax.googleapis.com/ajax/services/language/translate'
14
+ API_VERSION = '1.0'
15
+ DEBUG = false
16
+ SUCCESS_HANDLER = lambda do |response|
17
+ puts response.inspect if DEBUG
18
+
19
+ r = JSON.parse(response.body)
20
+ d = r['responseData']
21
+ s = r['responseStatus']
22
+ raise GoogleLanguageError, r['responseDetails'] if d.nil?
23
+
24
+ d['translatedText']
25
+ end
26
+ ERROR_HANDLER = lambda do |response|
27
+ raise "Error: #{response.code}. Body #{response.body}"
28
+ end
29
+
30
+ remote_defaults :on_success => SUCCESS_HANDLER,
31
+ :on_failure => ERROR_HANDLER,
32
+ :cache_responses => 180
33
+ define_remote_method :get_translate,
34
+ :base_uri => URL,
35
+ :headers => { "User-Agent" => "http://aktagon.com/projects/ruby/google-language" }
36
+
37
+ class << self
38
+ def url(text, options = {})
39
+ "#{URL}?#{to_uri(options)}"
40
+ end
41
+
42
+ def translate(text, options = {})
43
+ get_translate(:params => to_params(text, options))
44
+ end
45
+
46
+ protected
47
+ def to_uri(text, options)
48
+ to_params(text, options).map {|key, val| "#{key}=#{val}" }.join("&")
49
+ end
50
+
51
+ def to_params(text, options)
52
+ from = options.delete(:from)
53
+ to = options.delete(:to)
54
+ options.merge!(
55
+ :q => text,
56
+ :langpair => "#{from}|#{to}",
57
+ :v => API_VERSION
58
+ )
59
+ end
60
+ end
61
+
62
+ end
@@ -0,0 +1,22 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestGoogleLanguage < Test::Unit::TestCase
4
+
5
+ should "support the Google AJAX Language API" do
6
+ GoogleLanguage.translate('you', :from => 'en', :to => 'it').should == 'tu'
7
+ GoogleLanguage.translate('sinä', :from => 'fi', :to => 'en').should == 'you'
8
+ GoogleLanguage.translate('du', :from => 'sv', :to => 'fi').should == 'sinä'
9
+ GoogleLanguage.translate('you', :from => 'en', :to => 'zh-CN').should == '您'
10
+ GoogleLanguage.translate('you', :from => 'en', :to => 'ja').should == 'あなた'
11
+ end
12
+
13
+ should "raise an error when given an invalid language" do
14
+ # lambda { GoogleLanguage.translate('you', :from => 'xx', :to => '88') }.should raise_error(GoogleLanguageError)
15
+ GoogleLanguage.translate('you', :from => 'xx', :to => '88')
16
+ begin
17
+ flunk "WTF"
18
+ rescue
19
+ end
20
+ end
21
+
22
+ end
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'matchy'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+
9
+ require 'google_language'
10
+
11
+ class Test::Unit::TestCase
12
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: google_language
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Christian Hellsten
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-22 00:00:00 +03:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: pauldix-typhoeus
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: json
27
+ type: :runtime
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: thoughtbot-shoulda
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ - !ruby/object:Gem::Dependency
46
+ name: jeremymcanally-matchy
47
+ type: :development
48
+ version_requirement:
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ description: http://code.google.com/intl/sv-SE/apis/ajaxlanguage/
56
+ email: christian.hellsten@gmail.com
57
+ executables: []
58
+
59
+ extensions: []
60
+
61
+ extra_rdoc_files:
62
+ - LICENSE
63
+ - README.textile
64
+ files:
65
+ - .document
66
+ - .gitignore
67
+ - LICENSE
68
+ - README.textile
69
+ - Rakefile
70
+ - VERSION.yml
71
+ - google_language.gemspec
72
+ - lib/google_language.rb
73
+ - test/test_google_language.rb
74
+ - test/test_helper.rb
75
+ has_rdoc: true
76
+ homepage: http://github.com/christian.hellsten/ruby-google-ajax-language
77
+ licenses: []
78
+
79
+ post_install_message:
80
+ rdoc_options:
81
+ - --charset=UTF-8
82
+ require_paths:
83
+ - lib
84
+ required_ruby_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ version:
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: "0"
95
+ version:
96
+ requirements: []
97
+
98
+ rubyforge_project:
99
+ rubygems_version: 1.3.5
100
+ signing_key:
101
+ specification_version: 3
102
+ summary: A Ruby client for the Google AJAX Language API
103
+ test_files:
104
+ - test/test_google_language.rb
105
+ - test/test_helper.rb