easy_translate 0.0.4 → 0.1
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/easy_translate.rb +5 -4
- data/lib/easy_translate/param_builder.rb +23 -0
- data/lib/easy_translate/translator.rb +41 -21
- data/lib/easy_translate/version.rb +1 -1
- metadata +4 -4
data/lib/easy_translate.rb
CHANGED
@@ -1,13 +1,14 @@
|
|
1
1
|
module EasyTranslate
|
2
|
-
|
3
|
-
require 'easy_translate/translator'
|
4
|
-
|
2
|
+
|
5
3
|
require 'uri'
|
6
4
|
require 'net/http'
|
7
5
|
|
8
6
|
require 'rubygems'
|
9
7
|
require 'json'
|
10
8
|
|
9
|
+
require 'easy_translate/param_builder'
|
10
|
+
require 'easy_translate/translator'
|
11
|
+
|
11
12
|
API_URL = 'ajax.googleapis.com'
|
12
13
|
API_TRANSLATE_PATH = '/ajax/services/language/translate'
|
13
14
|
API_DETECT_PATH = '/ajax/services/language/detect'
|
@@ -42,4 +43,4 @@ module EasyTranslate
|
|
42
43
|
'hu' => 'hungarian', 'ko' => 'korean', 'tg' => 'tajik', 'cs' => 'czech',
|
43
44
|
'hi' => 'hindi', 'hr' => 'croatian' }
|
44
45
|
|
45
|
-
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module EasyTranslate
|
2
|
+
|
3
|
+
class ParamBuilder
|
4
|
+
|
5
|
+
def initialize
|
6
|
+
@str = ''
|
7
|
+
end
|
8
|
+
|
9
|
+
def add(param, value)
|
10
|
+
if @str.empty?
|
11
|
+
@str << "#{param.to_s}=#{value}"
|
12
|
+
else
|
13
|
+
@str << "&#{param.to_s}=#{value}"
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
@str
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -15,7 +15,9 @@ module EasyTranslate
|
|
15
15
|
# This API (since it focuses more on tranlations/ease, does not return
|
16
16
|
# confidence ratings)
|
17
17
|
def self.detect(text, options = {})
|
18
|
-
|
18
|
+
params = base_params(text, options)
|
19
|
+
params.add :q, URI.escape(text)
|
20
|
+
json = api_call EasyTranslate::API_DETECT_PATH, params, :type => :get
|
19
21
|
json['responseData']['language']
|
20
22
|
end
|
21
23
|
|
@@ -32,8 +34,6 @@ module EasyTranslate
|
|
32
34
|
# Returns:
|
33
35
|
# string / an array of the translated strings
|
34
36
|
def self.translate(text, options)
|
35
|
-
# TODO failing because we run options[:to] through get langauge and it strips it to nil
|
36
|
-
|
37
37
|
# what type of call is this?
|
38
38
|
multi_call = options[:to].class == Array || text.class == Array
|
39
39
|
all_multi_call = options[:to].class == Array && text.class == Array
|
@@ -41,19 +41,21 @@ module EasyTranslate
|
|
41
41
|
to_lang = (options[:to].class == Array) ? options[:to].map { |to| get_language(to) } : get_language(options[:to])
|
42
42
|
from_lang = get_language(options[:from])
|
43
43
|
# make the call
|
44
|
-
|
45
|
-
|
44
|
+
params = base_params(text, options)
|
45
|
+
params.add :format, (options[:html] ? 'html' : 'text')
|
46
46
|
# for each to language, put all of the q's
|
47
47
|
to_lang.each do |tol|
|
48
48
|
escaped_lang_pair = URI.escape "#{from_lang}|#{tol}"
|
49
|
+
# because ruby let's us call .each on a string with newlines
|
50
|
+
text = [text] if text.is_a?(String) # sorry, TODO separate
|
49
51
|
text.each do |t|
|
50
|
-
|
51
|
-
|
52
|
+
params.add :q, URI.escape(t)
|
53
|
+
params.add :langpair, escaped_lang_pair
|
52
54
|
end
|
53
55
|
end
|
54
56
|
# TODO cleanup
|
55
57
|
# get the proper response and return
|
56
|
-
json = api_call
|
58
|
+
json = api_call EasyTranslate::API_TRANSLATE_PATH, params, :type => :post
|
57
59
|
# single argument is returned
|
58
60
|
if !multi_call
|
59
61
|
single_single(json)
|
@@ -62,7 +64,7 @@ module EasyTranslate
|
|
62
64
|
single_multiple(json)
|
63
65
|
# the big badass case
|
64
66
|
else
|
65
|
-
multiple_multiple(json, options[:to].
|
67
|
+
multiple_multiple(json, options[:to].size)
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
@@ -78,7 +80,7 @@ module EasyTranslate
|
|
78
80
|
else
|
79
81
|
translations = []
|
80
82
|
responseData = json['responseData'].map { |r| r['responseData']['translatedText'] }
|
81
|
-
per_bucket = responseData.
|
83
|
+
per_bucket = responseData.size / translation_count # should always be integer
|
82
84
|
0.upto(translation_count - 1) { |i| translations[i] = responseData.slice(i * per_bucket, per_bucket) }
|
83
85
|
end
|
84
86
|
translations
|
@@ -101,28 +103,46 @@ module EasyTranslate
|
|
101
103
|
raise ArgumentError.new('multiple :from not allowed') if options[:from] && options[:from].class == Array
|
102
104
|
raise ArgumentError.new('no string given') if text.empty?
|
103
105
|
key = options[:key] || @api_key || nil
|
104
|
-
params =
|
105
|
-
params
|
106
|
-
params
|
107
|
-
params
|
106
|
+
params = ParamBuilder.new
|
107
|
+
params.add :v, API_VERSION
|
108
|
+
params.add :user_ip, URI.escape(options[:user_ip]) if options.has_key?(:user_ip)
|
109
|
+
params.add :hl, URI.escape(get_language(options[:host_language])) if options.has_key?(:host_language)
|
110
|
+
params.add :key, URI.escape(key) if key
|
108
111
|
# key is standard - but left to individual methods
|
109
112
|
params
|
110
113
|
end
|
111
114
|
|
112
115
|
# make a call to the api and throw an error on non-200 response
|
113
|
-
# TODO
|
114
|
-
def self.api_call(path)
|
115
|
-
response =
|
116
|
-
|
117
|
-
|
116
|
+
# TODO expand error handling
|
117
|
+
def self.api_call(path, params, options = {:type => :get})
|
118
|
+
response = case options[:type]
|
119
|
+
when :get ; api_get_call(path, params)
|
120
|
+
when :post ; api_post_call(path, params)
|
121
|
+
else ; ArgumentError.new('Bad HTTP type')
|
122
|
+
end
|
123
|
+
# if we got a response, use it - otherwise, fail town
|
124
|
+
json = JSON.parse(response) if response
|
125
|
+
raise EasyTranslateException.new(json['responseDetails']) unless json && json['responseStatus'] == 200
|
118
126
|
json
|
119
127
|
end
|
120
|
-
|
128
|
+
|
129
|
+
def self.api_post_call(path, params)
|
130
|
+
http = Net::HTTP.new(EasyTranslate::API_URL)
|
131
|
+
response = http.post(path, params.to_s)
|
132
|
+
response.body if response
|
133
|
+
end
|
134
|
+
|
135
|
+
def self.api_get_call(path, params)
|
136
|
+
http = Net::HTTP.new(EasyTranslate::API_URL)
|
137
|
+
response = http.get("#{path}?#{params.to_s}")
|
138
|
+
response.body if response
|
139
|
+
end
|
140
|
+
|
121
141
|
# a function used to get the lang code of any input.
|
122
142
|
# can take -- :english, 'english', :en, 'en'
|
123
143
|
def self.get_language(lang)
|
124
144
|
lang = lang.to_s
|
125
|
-
lang = LANGUAGES.include?(lang) ? lang : LANGUAGES.index(lang)
|
145
|
+
lang = EasyTranslate::LANGUAGES.include?(lang) ? lang : EasyTranslate::LANGUAGES.index(lang)
|
126
146
|
raise ArgumentError.new('please supply a valid language') unless lang
|
127
147
|
lang
|
128
148
|
end
|
metadata
CHANGED
@@ -4,9 +4,8 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
|
9
|
-
version: 0.0.4
|
7
|
+
- 1
|
8
|
+
version: "0.1"
|
10
9
|
platform: ruby
|
11
10
|
authors:
|
12
11
|
- John Crepezzi
|
@@ -14,7 +13,7 @@ autorequire:
|
|
14
13
|
bindir: bin
|
15
14
|
cert_chain: []
|
16
15
|
|
17
|
-
date: 2010-
|
16
|
+
date: 2010-06-18 00:00:00 -04:00
|
18
17
|
default_executable:
|
19
18
|
dependencies:
|
20
19
|
- !ruby/object:Gem::Dependency
|
@@ -50,6 +49,7 @@ extensions: []
|
|
50
49
|
extra_rdoc_files: []
|
51
50
|
|
52
51
|
files:
|
52
|
+
- lib/easy_translate/param_builder.rb
|
53
53
|
- lib/easy_translate/translator.rb
|
54
54
|
- lib/easy_translate/version.rb
|
55
55
|
- lib/easy_translate.rb
|