goobalize3 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/goobalize3/version.rb +1 -1
- data/lib/google_translate.rb +8 -6
- metadata +8 -4
data/lib/goobalize3/version.rb
CHANGED
data/lib/google_translate.rb
CHANGED
@@ -2,8 +2,9 @@ module GoogleTranslate
|
|
2
2
|
require 'net/http'
|
3
3
|
require 'json'
|
4
4
|
|
5
|
-
HOST
|
6
|
-
SERVICE
|
5
|
+
HOST = 'www.googleapis.com'
|
6
|
+
SERVICE = '/language/translate/v2'
|
7
|
+
QUERY_LIMIT = 5000
|
7
8
|
|
8
9
|
def self.get_api
|
9
10
|
config_file = "#{Rails.root}/config/google_translate.yml"
|
@@ -20,19 +21,20 @@ module GoogleTranslate
|
|
20
21
|
|
21
22
|
def self.perform(params)
|
22
23
|
@@goole_translate_api ||= get_api
|
23
|
-
params[:q] = CGI::escape(params[:q])
|
24
|
-
params.merge!(:key => @@goole_translate_api)
|
24
|
+
params[:q] = CGI::escape(params[:q].to_s[0..QUERY_LIMIT])
|
25
|
+
params.merge!(:key => @@goole_translate_api, :format => 'html')
|
25
26
|
data = []
|
26
27
|
params.each_pair { |k,v| data << "#{k}=#{v}" }
|
27
28
|
query_string = data.join('&')
|
28
29
|
http = Net::HTTP.new(HOST, 443)
|
29
30
|
http.use_ssl = true
|
30
31
|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
31
|
-
response, data = http.
|
32
|
+
response, data = http.post(SERVICE, query_string, 'X-HTTP-Method-Override' => 'GET')
|
32
33
|
json = JSON.parse(data)
|
33
|
-
raise GoogleTranslateException.new(json['error']['errors'].first['message']) if json['error']
|
34
34
|
if json['data'] && json['data']['translations'] && json['data']['translations'].first['translatedText']
|
35
35
|
return CGI::unescapeHTML(json['data']['translations'].first['translatedText'])
|
36
|
+
elsif json['error'] && json['error']['errors'] && json['error']['errors'].first['message']
|
37
|
+
raise GoogleTranslateException.new(json['error']['errors'].first['message'])
|
36
38
|
else
|
37
39
|
raise GoogleTranslateException.new('Unknown error')
|
38
40
|
end
|
metadata
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: goobalize3
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
5
6
|
segments:
|
6
7
|
- 0
|
7
8
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
9
|
+
- 2
|
10
|
+
version: 0.0.2
|
10
11
|
platform: ruby
|
11
12
|
authors:
|
12
13
|
- Enrico Pilotto
|
@@ -25,6 +26,7 @@ dependencies:
|
|
25
26
|
requirements:
|
26
27
|
- - ">="
|
27
28
|
- !ruby/object:Gem::Version
|
29
|
+
hash: 3
|
28
30
|
segments:
|
29
31
|
- 0
|
30
32
|
version: "0"
|
@@ -63,6 +65,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
63
65
|
requirements:
|
64
66
|
- - ">="
|
65
67
|
- !ruby/object:Gem::Version
|
68
|
+
hash: 3
|
66
69
|
segments:
|
67
70
|
- 0
|
68
71
|
version: "0"
|
@@ -71,13 +74,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
71
74
|
requirements:
|
72
75
|
- - ">="
|
73
76
|
- !ruby/object:Gem::Version
|
77
|
+
hash: 3
|
74
78
|
segments:
|
75
79
|
- 0
|
76
80
|
version: "0"
|
77
81
|
requirements: []
|
78
82
|
|
79
83
|
rubyforge_project: goobalize3
|
80
|
-
rubygems_version: 1.
|
84
|
+
rubygems_version: 1.4.2
|
81
85
|
signing_key:
|
82
86
|
specification_version: 3
|
83
87
|
summary: Auto translate with Google Translate the Globalize3 attributes
|