google-translate 0.8.5 → 0.8.6

Sign up to get free protection for your applications and to get access to all the features.
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ google-translate
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-1.9.3
data/CHANGES CHANGED
@@ -43,4 +43,8 @@
43
43
 
44
44
  == Version 0.8.5
45
45
 
46
- * Stop using translate API, start using scraping google translate
46
+ * Stop using translate API, start using scraping google translate
47
+
48
+ == Version 0.8.6
49
+
50
+ * Fix auto detect source language; fix countries list; refactoring code
data/Gemfile ADDED
@@ -0,0 +1,21 @@
1
+ source "https://rubygems.org"
2
+
3
+ group :default do
4
+ gem "json_pure"
5
+ end
6
+
7
+ group :development do
8
+ gem "gemspec_deps_gen"
9
+ gem "gemcutter"
10
+ end
11
+
12
+ group :test do
13
+ gem "rspec"
14
+ end
15
+
16
+ group :debug do
17
+ gem "ruby-debug-base19x", "0.11.30.pre12"
18
+ gem "ruby-debug-ide", "0.4.17"
19
+ end
20
+
21
+
data/Gemfile.lock ADDED
@@ -0,0 +1,35 @@
1
+ GEM
2
+ remote: https://rubygems.org/
3
+ specs:
4
+ debugger-ruby_core_source (1.2.3)
5
+ diff-lcs (1.1.3)
6
+ file_utils (1.0.6)
7
+ gemcutter (0.7.1)
8
+ gemspec_deps_gen (1.0.5)
9
+ file_utils
10
+ json_pure (1.6.5)
11
+ rake (10.1.0)
12
+ rspec (2.8.0)
13
+ rspec-core (~> 2.8.0)
14
+ rspec-expectations (~> 2.8.0)
15
+ rspec-mocks (~> 2.8.0)
16
+ rspec-core (2.8.0)
17
+ rspec-expectations (2.8.0)
18
+ diff-lcs (~> 1.1.2)
19
+ rspec-mocks (2.8.0)
20
+ ruby-debug-base19x (0.11.30.pre12)
21
+ debugger-ruby_core_source (>= 1.1.4)
22
+ rake (>= 0.8.1)
23
+ ruby-debug-ide (0.4.17)
24
+ rake (>= 0.8.1)
25
+
26
+ PLATFORMS
27
+ ruby
28
+
29
+ DEPENDENCIES
30
+ gemcutter
31
+ gemspec_deps_gen
32
+ json_pure
33
+ rspec
34
+ ruby-debug-base19x (= 0.11.30.pre12)
35
+ ruby-debug-ide (= 0.4.17)
data/Rakefile CHANGED
@@ -1,104 +1,37 @@
1
- # Rakefile for google-translate
1
+ #!/usr/bin/env rake
2
2
 
3
- require 'rubygems' unless RUBY_VERSION =~ /1.9.*/
3
+ $LOAD_PATH.unshift File.expand_path("lib", File.dirname(__FILE__))
4
4
 
5
- require 'rake/gempackagetask'
6
- require 'rake/testtask'
5
+ require "rspec/core/rake_task"
6
+ require "google_translate/version"
7
+ require "gemspec_deps_gen/gemspec_deps_gen"
7
8
 
8
- task :default => :gemspec
9
-
10
- begin
11
- require 'bundler'
12
-
13
- begin
14
- require 'jeweler'
15
-
16
- Jeweler::Tasks.new do |gemspec|
17
- gemspec.name = "google-translate"
18
- gemspec.summary = "Simple client for Google Translate API (Summary)."
19
- gemspec.description = "Simple client for Google Translate API."
20
- gemspec.email = "alexander.shvets@gmail.com"
21
- gemspec.homepage = "http://github.com/shvets/google-translate"
22
- gemspec.authors = ["Alexander Shvets"]
23
- gemspec.files = FileList["CHANGES", "google-translate.gemspec", "Rakefile", "README", "VERSION",
24
- "lib/**/*", "bin/**/*"]
25
- gemspec.add_dependency "json_pure", ">= 1.1.4"
26
-
27
- gemspec.executables = ['translate', 't']
28
- gemspec.requirements = ["none"]
29
- gemspec.bindir = "bin"
30
-
31
- gemspec.add_bundler_dependencies
32
- end
33
- rescue LoadError
34
- puts "Jeweler not available. Install it s with: [sudo] gem install jeweler"
35
- end
36
- rescue LoadError
37
- puts "Bundler not available. Install it s with: [sudo] gem install bundler"
9
+ def version
10
+ GoogleTranslate::VERSION
38
11
  end
39
12
 
40
- desc "Release the gem"
41
- task :"release:gem" do
42
- %x(
43
- rake gemspec
44
- rake build
45
- rake install
46
- git add .
47
- )
48
- puts "Commit message:"
49
- message = STDIN.gets
13
+ def project_name
14
+ File.basename(Dir.pwd)
15
+ end
50
16
 
51
- version = "#{File.open(File::dirname(__FILE__) + "/VERSION").readlines().first}"
17
+ task :build do
18
+ system "rm #{project_name}.gemspec"
19
+ generator = GemspecDepsGen.new
52
20
 
53
- %x(
54
- git commit -m "#{message}"
55
-
56
- git push origin master
21
+ generator.generate_dependencies "#{project_name}.gemspec.erb", "#{project_name}.gemspec"
57
22
 
58
- gem push pkg/google-translate-#{version}.gem
59
- )
23
+ system "gem build #{project_name}.gemspec"
60
24
  end
61
25
 
62
- desc "Run gem code locally"
63
- task :"run:gem" do
64
- command = "bin/translate " + (ENV['params'].nil? ? '' : ENV['params'])
65
- puts ruby("#{command}")
26
+ task :install do
27
+ system "gem install #{project_name}-#{version}.gem"
66
28
  end
67
29
 
68
- #Spec::Rake::SpecTask.new do |task|
69
- # task.libs << 'lib'
70
- # task.pattern = 'spec/**/*_spec.rb'
71
- # task.verbose = false
72
- #end
73
-
74
- if defined? RSpec::Core::RakeTask
75
- require 'rspec/core/rake_task'
76
-
77
- RSpec::Core::RakeTask.new do |task|
78
- task.pattern = 'spec/**/*_spec.rb'
79
- task.verbose = false
80
- end
81
- end
82
-
83
- if defined? Rake::RDocTask
84
- require 'rake/rdoctask'
85
-
86
- Rake::RDocTask.new do |rdoc|
87
- rdoc.rdoc_dir = 'rdoc'
88
- rdoc.title = 'teststuff'
89
- rdoc.options << '--line-numbers' << '--inline-source'
90
- rdoc.rdoc_files.include('README*')
91
- rdoc.rdoc_files.include('lib/**/*.rb')
92
- end
30
+ task :release => :build do
31
+ system "gem push #{project_name}-#{version}.gem"
93
32
  end
94
33
 
95
- if defined? Rcov::RcovTask
96
- require 'rcov/rcovtask'
97
-
98
- Rcov::RcovTask.new do |task|
99
- task.libs << 'test'
100
- task.test_files = FileList['test/**/*_test.rb']
101
- task.verbose = true
102
- end
34
+ RSpec::Core::RakeTask.new do |task|
35
+ task.pattern = 'spec/**/*_spec.rb'
36
+ task.verbose = false
103
37
  end
104
-
data/bin/translate CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  $:.unshift(File::join(File::dirname(File::dirname(__FILE__)), "lib"))
4
4
 
5
- require 'rubygems' unless RUBY_VERSION =~ /1.9.*/
5
+ require 'rubygems' unless defined?(Gem)
6
6
  require 'google_translate'
7
7
 
8
8
  #$KCODE='u'
@@ -17,7 +17,7 @@ class Translate
17
17
  TEXT
18
18
 
19
19
  def initialize
20
- @translator = Google::Translator.new
20
+ @translator = GoogleTranslate.new
21
21
  end
22
22
 
23
23
  def print_languages list, title
@@ -59,27 +59,23 @@ class Translate
59
59
 
60
60
  print_languages hash[:from_languages], "From Languages:"
61
61
  print_languages hash[:to_languages], "To Languages:"
62
- #when 'detect' then
63
- # language = @translator.detect_language(ARGV.shift)
64
- #
65
- # puts "Language: #{language.inspect}"
66
62
  when /(.*):(.*)/ then
67
63
  from_text = ARGV.join(' ')
68
64
  from = $1
69
65
  to = $2
70
66
 
71
67
  display(@translator.translate(from.to_sym, to.to_sym, from_text))
72
- #when /(.*)/ then
73
- # from_text = ARGV.join(' ')
74
- #
75
- # from = @translator.detect_language(from_text)['language']
76
- # to = $1
77
- #
78
- # begin
79
- # display(@translator.translate(from.to_sym, to.to_sym, from_text))
80
- # rescue Exception => e
81
- # puts "Error: " + e.message
82
- # end
68
+ when /(.*)/ then
69
+ from_text = ARGV.join(' ')
70
+
71
+ from = "auto"
72
+ to = $1
73
+
74
+ begin
75
+ display(@translator.translate(from.to_sym, to.to_sym, from_text))
76
+ rescue Exception => e
77
+ puts "Error: " + e.message
78
+ end
83
79
  end
84
80
  end
85
81
  end
@@ -1,67 +1,22 @@
1
- # Generated by jeweler
2
- # DO NOT EDIT THIS FILE DIRECTLY
3
- # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
1
  # -*- encoding: utf-8 -*-
5
2
 
6
- Gem::Specification.new do |s|
7
- s.name = %q{google-translate}
8
- s.version = "0.8.5"
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/google_translate/version')
9
4
 
10
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = ["Alexander Shvets"]
12
- s.date = %q{2011-10-13}
13
- s.description = %q{Simple client for Google Translate API.}
14
- s.email = %q{alexander.shvets@gmail.com}
15
- s.executables = ["translate", "t"]
16
- s.extra_rdoc_files = [
17
- "README"
18
- ]
19
- s.files = [
20
- "CHANGES",
21
- "README",
22
- "Rakefile",
23
- "VERSION",
24
- "bin/t",
25
- "bin/t.bat",
26
- "bin/translate",
27
- "bin/translate.bat",
28
- "google-translate.gemspec",
29
- "lib/google_translate.rb"
30
- ]
31
- s.homepage = %q{http://github.com/shvets/google-translate}
32
- s.require_paths = ["lib"]
33
- s.requirements = ["none"]
34
- s.rubygems_version = %q{1.6.2}
35
- s.summary = %q{Simple client for Google Translate API (Summary).}
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "google-translate"
7
+ spec.summary = %q{Simple client for Google Translate API.}
8
+ spec.description = %q{Simple client for Google Translate API.}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/google-translate"
12
+ spec.executables = ["translate", "t"]
36
13
 
37
- if s.respond_to? :specification_version then
38
- s.specification_version = 3
39
-
40
- if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
41
- s.add_runtime_dependency(%q<json_pure>, [">= 0"])
42
- s.add_development_dependency(%q<jeweler>, [">= 0"])
43
- s.add_development_dependency(%q<gemcutter>, [">= 0"])
44
- s.add_runtime_dependency(%q<json_pure>, [">= 1.1.4"])
45
- s.add_runtime_dependency(%q<json_pure>, [">= 0"])
46
- s.add_development_dependency(%q<jeweler>, [">= 0"])
47
- s.add_development_dependency(%q<gemcutter>, [">= 0"])
48
- else
49
- s.add_dependency(%q<json_pure>, [">= 0"])
50
- s.add_dependency(%q<jeweler>, [">= 0"])
51
- s.add_dependency(%q<gemcutter>, [">= 0"])
52
- s.add_dependency(%q<json_pure>, [">= 1.1.4"])
53
- s.add_dependency(%q<json_pure>, [">= 0"])
54
- s.add_dependency(%q<jeweler>, [">= 0"])
55
- s.add_dependency(%q<gemcutter>, [">= 0"])
56
- end
57
- else
58
- s.add_dependency(%q<json_pure>, [">= 0"])
59
- s.add_dependency(%q<jeweler>, [">= 0"])
60
- s.add_dependency(%q<gemcutter>, [">= 0"])
61
- s.add_dependency(%q<json_pure>, [">= 1.1.4"])
62
- s.add_dependency(%q<json_pure>, [">= 0"])
63
- s.add_dependency(%q<jeweler>, [">= 0"])
64
- s.add_dependency(%q<gemcutter>, [">= 0"])
65
- end
14
+ spec.files = `git ls-files`.split($\)
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+ spec.version = GoogleTranslate::VERSION
66
18
  end
67
19
 
20
+
21
+
22
+
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ require File.expand_path(File.dirname(__FILE__) + '/lib/google_translate/version')
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "google-translate"
7
+ spec.summary = %q{Simple client for Google Translate API.}
8
+ spec.description = %q{Simple client for Google Translate API.}
9
+ spec.email = "alexander.shvets@gmail.com"
10
+ spec.authors = ["Alexander Shvets"]
11
+ spec.homepage = "http://github.com/shvets/google-translate"
12
+ spec.executables = ["translate", "t"]
13
+
14
+ spec.files = `git ls-files`.split($\)
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ["lib"]
17
+ spec.version = GoogleTranslate::VERSION
18
+ end
19
+
20
+
21
+
22
+
@@ -1,132 +1,2 @@
1
- # google_translate.rb
2
-
3
- require 'open-uri'
4
-
5
- require 'cgi'
6
- require 'json'
7
-
8
- module Google
9
-
10
- Language = Struct.new(:name, :code) do
11
- def to_s
12
- "(" + code + ": " + name + ")"
13
- end
14
- end
15
-
16
- class Translator
17
- GOOGLE_TRANSLATE_SERVICE_URL = "http://translate.google.com"
18
-
19
- def self.Exception(*names)
20
- cl = Module === self ? self : Object
21
- names.each {|n| cl.const_set(n, Class.new(Exception))}
22
- end
23
-
24
- Exception :MissingFromLanguage, :MissingToLanguage, :MissingTextLanguage,
25
- :TranslateServerIsDown, :InvalidResponse, :MissingText, :MissingTestText
26
-
27
- def translate(from, to, from_text, options={})
28
- raise(MissingFromLanguage) if from.nil?
29
- raise(MissingToLanguage) if to.nil?
30
- raise(MissingTextLanguage) if from_text.nil?
31
-
32
- begin
33
- url = GOOGLE_TRANSLATE_SERVICE_URL + "/translate_a/t?client=t&text=#{from_text}&hl=#{from}&sl=auto&tl=#{to}&multires=1&prev=btn&ssel=0&tsel=4&uptl=#{to}&alttl=#{from}&sc=1"
34
-
35
- open(URI.escape(url)) do |stream|
36
- content = stream.read
37
-
38
- s = content.split(',').collect {|s| s == '' ? "\"\"" : s}.join(",")
39
-
40
- result = JSON.parse(s)
41
-
42
- raise(TranslateServerIsDown) if (!result || result.empty?)
43
-
44
- # raise(InvalidResponse, result["responseDetails"]) if response.code.to_i != 200 # success
45
-
46
- r1 = result[0][0][0]
47
- r2 = result[0][0][2]
48
-
49
- [r1, r2]
50
- end
51
- rescue Exception => e
52
- raise(TranslateServerIsDown)
53
- end
54
- end
55
-
56
- # def detect_language(test_text)
57
-
58
- # raise(MissingTestText) if test_text.nil?
59
-
60
- # begin
61
- # response = self.class.get("/detect", :query => {:q => test_text})
62
- # response = (response && response.parsed_response) ? response.parsed_response : nil
63
- # raise(TranslateServerIsDown) if (!response || response.empty?)
64
- # response_data = response["responseData"]
65
- # return response_data
66
- # rescue HTTParty::ResponseError
67
- # raise(TranslateServerIsDown)
68
- # end
69
- #end
70
-
71
- def supported_languages
72
- fetch_languages(GOOGLE_TRANSLATE_SERVICE_URL , [])
73
- end
74
-
75
- private
76
-
77
- def fetch_languages(request, keys)
78
- response = {}
79
-
80
- open(URI.escape(request)) do |stream|
81
- content = stream.read
82
-
83
- from_languages = collect_languages content, 0, 'sl', 'gt-sl'
84
- to_languages = collect_languages content, 1, 'tl', 'gt-tl'
85
-
86
- response[:from_languages] = from_languages
87
- response[:to_languages] = to_languages
88
- end
89
-
90
- response
91
- end
92
-
93
- def collect_languages buffer, index, tag_name, tag_id
94
- languages = []
95
-
96
- spaces = '\s?'
97
- quote = '(\s|\'|")?'
98
-
99
-
100
- id_part = "id#{spaces}=#{spaces}#{quote}#{tag_id}#{quote}"
101
- name_part = "name#{spaces}=#{spaces}#{quote}#{tag_name}#{quote}"
102
- tabindex_part = "tabindex#{spaces}=#{spaces}#{quote}0#{quote}"
103
- phrase = "#{spaces}#{id_part}#{spaces}#{name_part}#{spaces}#{tabindex_part}#{spaces}"
104
-
105
- re1 = buffer.split(%r{<select#{phrase}>(.*)?</select>}).select{|x| x =~ %r{<option} }
106
-
107
- stopper = "</select></div>"
108
-
109
- text = re1[index]
110
-
111
- if index == 0
112
- pos = text.index(stopper)
113
- text = text[0..pos]
114
- end
115
-
116
- re2 = /<option(\s*)value="([a-z|A-Z|-]*)">([a-z|A-Z|\(|\)|\s]*)<\/option>/
117
- matches = text.gsub(/selected/i, '').squeeze.scan(re2)
118
-
119
- if matches.size == 0
120
- re2 = /<option(\s*)value=([a-z|A-Z|-]*)>([a-z|A-Z|\(|\)|\s]*)<\/option>/
121
- matches = text.gsub(/selected/i, '').squeeze.scan(re2)
122
- end
123
-
124
- matches.each do |m|
125
- languages << Language.new(m[2], m[1])
126
- end
127
-
128
- languages
129
- end
130
- end
131
-
132
- end
1
+ require 'google_translate/language'
2
+ require 'google_translate/google_translate'