sishen-rtranslate 1.3 → 1.4

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ .svn
2
+ **/.svn
3
+ **/**/.svn
4
+ pkg
data/README.txt CHANGED
@@ -1,4 +1,4 @@
1
- = google-api-transalte-ruby
1
+ = google-api-translate-ruby
2
2
  A simple, unofficial, ruby client API for using Google Translate.
3
3
 
4
4
  == Install
data/Rakefile CHANGED
@@ -1,12 +1,19 @@
1
1
  require 'rubygems'
2
- require 'hoe'
3
2
 
4
- Hoe.new('sishen-rtranslate', '1.3') do |p|
5
- p.rubyforge_name = 'sishen-rtranslate'
6
- p.author = 'Ye Dingding'
7
- p.email = 'yedingding@gmail.com'
8
- p.url = 'http://github.com/sishen/rtranslate'
9
- p.summary = 'Google translate ruby client api'
10
- p.description = 'A simple, unofficial, ruby client API for using Google Translate.'
11
- p.extra_deps = [['json', '>= 1.1.3']]
3
+ begin
4
+ require 'jeweler'
5
+ Jeweler::Tasks.new do |p|
6
+ p.name = 'sishen-rtranslate'
7
+ p.version = '1.4'
8
+ p.date = "2010-08-31"
9
+ p.authors = ['Ye Dingding']
10
+ p.email = 'yedingding@gmail.com'
11
+ p.homepage = 'http://github.com/sishen/rtranslate'
12
+ p.summary = 'Google translate ruby client api'
13
+ p.description = 'A simple, unofficial, ruby client API for using Google Translate.'
14
+ p.add_dependency 'json', '>= 1.1.3'
15
+ p.add_dependency 'activesupport', '>= 2.2.0'
16
+ end
17
+ rescue LoadError
18
+ puts "Jeweler not available. Install it with: gem install jeweler"
12
19
  end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.4
@@ -45,18 +45,18 @@ module Translate
45
45
  @key = key
46
46
  end
47
47
 
48
- def detect(text, details = false)
49
- url = "#{GOOGLE_DETECTION_URL}?q=#{text}&v=#{@version}"
50
- if @key
51
- url << "&key=#{@key}"
52
- end
48
+ def detect(text, options = {:details => false})
49
+ url = "#{GOOGLE_DETECTION_URL}?q=#{CGI.escape(text)}&v=#{@version}"
50
+ url << "&key=#{@key}" if @key
51
+ url << "&userip=#{options[:userip]}" if options[:userip]
52
+
53
53
  detection_response = do_detect(url)
54
- details ? detection_response : detection_response.abbrev
54
+ options[:details] ? detection_response : detection_response.abbrev
55
55
  end
56
56
 
57
57
  private
58
58
  def do_detect(url)
59
- jsondoc = open(URI.escape(url)).read
59
+ jsondoc = Net::HTTP.get(URI.parse(url))
60
60
  response = JSON.parse(jsondoc)
61
61
  if response["responseStatus"] == 200
62
62
  DetectionResponse.new(response["responseData"])
@@ -58,6 +58,7 @@ module Translate
58
58
  # Configuration options:
59
59
  # * <tt>:from</tt> - The source language
60
60
  # * <tt>:to</tt> - The target language
61
+ # * <tt>:userip</tt> - The IP address of the end-user on whose behalf the request is being made
61
62
  def translate(text, options = { })
62
63
  from = options[:from] || @default_from
63
64
  to = options[:to] || @default_to
@@ -66,11 +67,11 @@ module Translate
66
67
  to = Google::Language.abbrev(to)
67
68
  langpair = "#{from}|#{to}"
68
69
 
69
- text.mb_chars.scan(/(.{1,500})/).inject("") do |result, st|
70
- url = "#{GOOGLE_TRANSLATE_URL}?q=#{st}&langpair=#{langpair}&v=#{@version}"
71
- if @key
72
- url << "&key=#{@key}"
73
- end
70
+ text.mb_chars.scan(/(.{1,300})/).inject("") do |result, st|
71
+ url = "#{GOOGLE_TRANSLATE_URL}?q=#{CGI.escape(st.to_s)}&langpair=#{CGI.escape(langpair)}&v=#{@version}"
72
+ url << "&key=#{@key}" if @key
73
+ url << "&userip=#{options[:userip]}" if options[:userip]
74
+
74
75
  result += do_translate(url)
75
76
  end
76
77
  else
@@ -116,7 +117,7 @@ module Translate
116
117
 
117
118
  private
118
119
  def do_translate(url) #:nodoc:
119
- jsondoc = open(URI.escape(url)).read
120
+ jsondoc = Net::HTTP.get(URI.parse(url))
120
121
  response = JSON.parse(jsondoc)
121
122
  if response["responseStatus"] == 200
122
123
  response["responseData"]["translatedText"]
data/lib/rtranslate.rb CHANGED
@@ -1,8 +1,11 @@
1
+ # encoding: utf-8
2
+
1
3
  require File.join(File.dirname(__FILE__), 'rtranslate/language')
2
4
  require File.join(File.dirname(__FILE__), 'rtranslate/rtranslate')
3
5
  require File.join(File.dirname(__FILE__), 'rtranslate/detection')
4
6
  require 'uri'
5
- require 'open-uri'
7
+ require 'cgi'
8
+ require 'net/http'
6
9
 
7
10
  begin
8
11
  require 'json'
@@ -11,21 +14,21 @@ rescue LoadError
11
14
  require 'json'
12
15
  end
13
16
 
17
+ $KCODE = 'u'
18
+
14
19
  unless defined?(ActiveSupport)
15
- require 'activesupport'
20
+ require 'active_support'
16
21
  end
17
22
 
18
- $KCODE = 'u'
19
-
20
23
  include Translate
21
24
  def Translate.t(text, from, to)
22
25
  RTranslate.translate(text, from, to)
23
26
  rescue
24
- "Error: " + $!
27
+ "Error: #{$!}"
25
28
  end
26
29
 
27
30
  def Translate.d(text)
28
31
  Detection.detect(text)
29
32
  rescue
30
- "Error: " + $!
33
+ "Error: #{$!}"
31
34
  end
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sishen-rtranslate
3
3
  version: !ruby/object:Gem::Version
4
- version: "1.3"
4
+ hash: 7
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 4
9
+ version: "1.4"
5
10
  platform: ruby
6
11
  authors:
7
12
  - Ye Dingding
@@ -9,29 +14,41 @@ autorequire:
9
14
  bindir: bin
10
15
  cert_chain: []
11
16
 
12
- date: 2009-12-18 00:00:00 +08:00
13
- default_executable:
17
+ date: 2010-08-31 00:00:00 +08:00
18
+ default_executable: rtranslate
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: json
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
20
25
  requirements:
21
26
  - - ">="
22
27
  - !ruby/object:Gem::Version
28
+ hash: 21
29
+ segments:
30
+ - 1
31
+ - 1
32
+ - 3
23
33
  version: 1.1.3
24
- version:
34
+ type: :runtime
35
+ version_requirements: *id001
25
36
  - !ruby/object:Gem::Dependency
26
- name: hoe
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
37
+ name: activesupport
38
+ prerelease: false
39
+ requirement: &id002 !ruby/object:Gem::Requirement
40
+ none: false
30
41
  requirements:
31
42
  - - ">="
32
43
  - !ruby/object:Gem::Version
33
- version: 2.3.3
34
- version:
44
+ hash: 7
45
+ segments:
46
+ - 2
47
+ - 2
48
+ - 0
49
+ version: 2.2.0
50
+ type: :runtime
51
+ version_requirements: *id002
35
52
  description: A simple, unofficial, ruby client API for using Google Translate.
36
53
  email: yedingding@gmail.com
37
54
  executables:
@@ -39,49 +56,53 @@ executables:
39
56
  extensions: []
40
57
 
41
58
  extra_rdoc_files:
42
- - History.txt
43
- - Manifest.txt
44
59
  - README.txt
45
60
  files:
61
+ - .gitignore
46
62
  - History.txt
47
63
  - MIT-LICENSE
48
64
  - Manifest.txt
49
65
  - README.txt
50
66
  - Rakefile
67
+ - VERSION
51
68
  - bin/rtranslate
52
69
  - lib/rtranslate.rb
70
+ - lib/rtranslate/detection.rb
53
71
  - lib/rtranslate/language.rb
54
72
  - lib/rtranslate/rtranslate.rb
55
- - lib/rtranslate/detection.rb
56
- - rtranslate.gemspec
57
- - tests/ts_translate.rb
58
73
  - tests/ts_detection.rb
74
+ - tests/ts_translate.rb
59
75
  has_rdoc: true
60
76
  homepage: http://github.com/sishen/rtranslate
61
77
  licenses: []
62
78
 
63
79
  post_install_message:
64
80
  rdoc_options:
65
- - --main
66
- - README.txt
81
+ - --charset=UTF-8
67
82
  require_paths:
68
83
  - lib
69
84
  required_ruby_version: !ruby/object:Gem::Requirement
85
+ none: false
70
86
  requirements:
71
87
  - - ">="
72
88
  - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
73
92
  version: "0"
74
- version:
75
93
  required_rubygems_version: !ruby/object:Gem::Requirement
94
+ none: false
76
95
  requirements:
77
96
  - - ">="
78
97
  - !ruby/object:Gem::Version
98
+ hash: 3
99
+ segments:
100
+ - 0
79
101
  version: "0"
80
- version:
81
102
  requirements: []
82
103
 
83
- rubyforge_project: sishen-rtranslate
84
- rubygems_version: 1.3.5
104
+ rubyforge_project:
105
+ rubygems_version: 1.3.7
85
106
  signing_key:
86
107
  specification_version: 3
87
108
  summary: Google translate ruby client api
data/rtranslate.gemspec DELETED
@@ -1,43 +0,0 @@
1
- Gem::Specification.new do |s|
2
- s.name = %q{rtranslate}
3
- s.version = "1.3"
4
-
5
- s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
6
- s.authors = ["Ye Dingding"]
7
- s.date = %q{2009-07-11}
8
- s.description = %q{rtranslate is a simple, unofficial, ruby client API for using Google Translate.}
9
- s.email = %q{yedingding@gmail.com}
10
- s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt"]
11
- s.files = ["History.txt", "MIT-LICENSE", "Manifest.txt", "README.txt", "Rakefile", "rtranslate.gemspec", "bin/rtranslate",
12
- "lib/rtranslate.rb", "lib/rtranslate/language.rb", "lib/rtranslate/rtranslate.rb", "tests/ts_translate.rb",
13
- "bin/rtranslate", "lib/rtranslate.rb", "lib/rtranslate/language.rb",
14
- "lib/rtranslate/rtranslate.rb", "lib/rtranslate/detection.rb", "tests/ts_detection.rb"]
15
- s.has_rdoc = true
16
- s.homepage = %q{http://github.com/sishen}
17
- s.rdoc_options = ["--main", "README.txt"]
18
- s.require_paths = ["lib"]
19
- s.rubygems_version = %q{1.2.0}
20
- s.summary = %q{a simple, unofficial, ruby client API for using Google Translate}
21
-
22
- if s.respond_to? :specification_version then
23
- current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
24
- s.specification_version = 2
25
-
26
- if current_version >= 3 then
27
- s.add_development_dependency(%q<hoe>, [">= 1.7.0"])
28
- s.add_development_dependency(%q<rspec>, [">= 1.1.4"])
29
- s.add_development_dependency(%q<json>, [">= 1.1.3"])
30
- s.add_development_dependency(%q<activesupprt>, [">= 2.2.0"])
31
- else
32
- s.add_dependency(%q<hoe>, [">= 1.7.0"])
33
- s.add_dependency(%q<rspec>, [">= 1.1.4"])
34
- s.add_dependency(%q<json>, [">= 1.1.3"])
35
- s.add_dependency(%q<activesupport>, [">= 2.2.0"])
36
- end
37
- else
38
- s.add_dependency(%q<hoe>, [">= 1.7.0"])
39
- s.add_dependency(%q<rspec>, [">= 1.1.4"])
40
- s.add_dependency(%q<json>, [">= 1.1.3"])
41
- s.add_dependency(%q<activesupport>, [">= 2.2.0"])
42
- end
43
- end