web_translate_it 1.10.2 → 2.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/history.md +13 -0
- data/lib/web_translate_it.rb +2 -0
- data/lib/web_translate_it/string.rb +197 -33
- data/lib/web_translate_it/term.rb +175 -29
- data/lib/web_translate_it/term_translation.rb +68 -14
- data/lib/web_translate_it/translation.rb +46 -24
- data/lib/web_translate_it/util.rb +1 -2
- data/lib/web_translate_it/util/hash_util.rb +24 -4
- data/readme.md +20 -22
- data/spec/web_translate_it/string_spec.rb +127 -0
- data/spec/web_translate_it/term_spec.rb +114 -0
- data/version +1 -0
- metadata +94 -81
- data/spec/web_translate_it/configuration_spec.rb +0 -60
- data/spec/web_translate_it/translation_file_spec.rb +0 -52
- data/spec/web_translate_it/util_spec.rb +0 -10
- data/spec/web_translate_it_spec.rb +0 -26
- data/version.yml +0 -4
data/history.md
CHANGED
@@ -1,3 +1,16 @@
|
|
1
|
+
## Edge
|
2
|
+
|
3
|
+
* Backport of compatibility fix for ruby 1.8.7 from 1.x serie.
|
4
|
+
* Fix issue with new version system, which broke the gem. #86
|
5
|
+
|
6
|
+
## Version 2.0.0.rc1 / 2012-04-16 (yanked)
|
7
|
+
|
8
|
+
**Important**: This release candidate introduce breaking changes in the String, Translation, Term and TermTranslation APIs. See [Extras](https://github.com/AtelierConvivialite/webtranslateit/wiki/Extras) if you use the String, Translation, Term and TermTranslation APIs to programmatically manage strings, translations and terms.
|
9
|
+
|
10
|
+
* Remove undeclared dependency on ActiveSupport. ActiveSupport was required to use the `WebTranslateIt::String`, `Translation`, `Term` and `TermTranslation` classes. #84
|
11
|
+
* Refactor `WebTranslateIt::String` and `WebTranslateIt::Translation` classes and add specs. #85
|
12
|
+
* Refactor `WebTranslateIt::Term` and `WebTranslateIt::TermTranslation` classes and add specs.
|
13
|
+
|
1
14
|
## Version 1.10.2 / 2012-04-17
|
2
15
|
|
3
16
|
* Compatibility fix for ruby 1.8.7.
|
data/lib/web_translate_it.rb
CHANGED
@@ -8,6 +8,8 @@ require 'web_translate_it/configuration'
|
|
8
8
|
require 'web_translate_it/translation_file'
|
9
9
|
require 'web_translate_it/string'
|
10
10
|
require 'web_translate_it/translation'
|
11
|
+
require 'web_translate_it/term'
|
12
|
+
require 'web_translate_it/term_translation'
|
11
13
|
require 'web_translate_it/auto_fetch'
|
12
14
|
require 'web_translate_it/command_line'
|
13
15
|
require 'web_translate_it/project'
|
@@ -2,26 +2,76 @@
|
|
2
2
|
module WebTranslateIt
|
3
3
|
class String
|
4
4
|
require 'net/https'
|
5
|
+
require 'json'
|
5
6
|
|
6
|
-
attr_accessor :id, :
|
7
|
+
attr_accessor :api_key, :id, :key, :plural, :type, :dev_comment, :word_count, :status, :category, :label, :file,
|
8
|
+
:created_at, :updated_at, :translations, :new_record
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
# Initialize a new WebTranslateIt::String
|
11
|
+
# The only mandatory parameter is `api_key`
|
12
|
+
#
|
13
|
+
# Implementation Example:
|
14
|
+
#
|
15
|
+
# WebTranslateIt::String.new('secret_api_token', { "key" => "product_name_123" })
|
16
|
+
#
|
17
|
+
# to instantiate a new String without any text.
|
18
|
+
#
|
19
|
+
# translation_en = WebTranslateIt::Translation.new({ "locale" => "en", "text" => "Hello" })
|
20
|
+
# translation_fr = WebTranslateIt::Translation.new({ "locale" => "fr", "text" => "Bonjour" })
|
21
|
+
# WebTranslateIt::String.new('secret_api_token', { "key" => "product_name_123", "translations" => [translation_en, translation_fr]})
|
22
|
+
#
|
23
|
+
# to instantiate a new String with a source and target translation.
|
24
|
+
|
25
|
+
def initialize(api_key, params = {})
|
26
|
+
self.api_key = api_key
|
27
|
+
self.id = params["id"] || nil
|
28
|
+
self.key = params["key"] || nil
|
29
|
+
self.plural = params["plural"] || nil
|
30
|
+
self.type = params["type"] || nil
|
31
|
+
self.dev_comment = params["dev_comment"] || nil
|
32
|
+
self.word_count = params["word_count"] || nil
|
33
|
+
self.status = params["status"] || nil
|
34
|
+
self.category = params["category"] || nil
|
35
|
+
self.label = params["label"] || nil
|
36
|
+
self.file = params["file"] || nil
|
37
|
+
self.created_at = params["created_at"] || nil
|
38
|
+
self.updated_at = params["updated_at"] || nil
|
39
|
+
self.translations = params["translations"] || []
|
40
|
+
self.new_record = true
|
11
41
|
end
|
12
42
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
43
|
+
# Find a String based on filters
|
44
|
+
# Needs a HTTPS Connection
|
45
|
+
#
|
46
|
+
# Implementation Example:
|
47
|
+
#
|
48
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
49
|
+
# strings = WebTranslateIt::String.find_all(connection, 'secret_api_token', { "key" => "product_name_123" })
|
50
|
+
# end
|
51
|
+
#
|
52
|
+
# puts strings.inspect #=> An array of WebTranslateIt::String objects
|
53
|
+
#
|
54
|
+
# to find and instantiate an array of String which key is like `product_name_123`.
|
55
|
+
#
|
56
|
+
# TODO: Implement pagination
|
57
|
+
|
58
|
+
def self.find_all(http_connection, api_key, params = {})
|
59
|
+
url = "/api/projects/#{api_key}/strings.yaml"
|
60
|
+
url += '?' + HashUtil.to_params("filters" => params) unless params.empty?
|
18
61
|
|
19
62
|
request = Net::HTTP::Get.new(url)
|
20
63
|
request.add_field("X-Client-Name", "web_translate_it")
|
21
64
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
22
65
|
|
23
66
|
begin
|
24
|
-
Util.handle_response(http_connection.request(request), true)
|
67
|
+
response = Util.handle_response(http_connection.request(request), true)
|
68
|
+
strings = []
|
69
|
+
YAML.load(response).each do |string_response|
|
70
|
+
string = WebTranslateIt::String.new(api_key, string_response)
|
71
|
+
string.new_record = false
|
72
|
+
strings.push(string)
|
73
|
+
end
|
74
|
+
return strings
|
25
75
|
rescue Timeout::Error
|
26
76
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
27
77
|
sleep(5)
|
@@ -29,31 +79,73 @@ module WebTranslateIt
|
|
29
79
|
end
|
30
80
|
end
|
31
81
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
82
|
+
# Find a String based on its ID
|
83
|
+
# Needs a HTTPS Connection
|
84
|
+
#
|
85
|
+
# Implementation Example:
|
86
|
+
#
|
87
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
88
|
+
# string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
|
89
|
+
# end
|
90
|
+
#
|
91
|
+
# puts string.inspect #=> A WebTranslateIt::String object
|
92
|
+
#
|
93
|
+
# to find and instantiate the String which ID is `1234`.
|
94
|
+
#
|
95
|
+
|
96
|
+
def self.find(http_connection, api_key, id)
|
97
|
+
request = Net::HTTP::Get.new("/api/projects/#{api_key}/strings/#{id}.yaml")
|
36
98
|
request.add_field("X-Client-Name", "web_translate_it")
|
37
99
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
38
100
|
|
39
101
|
begin
|
40
|
-
|
102
|
+
response = http_connection.request(request)
|
103
|
+
return nil if response.code.to_i == 404
|
104
|
+
string = WebTranslateIt::String.new(api_key, YAML.load(response.body))
|
105
|
+
string.new_record = false
|
106
|
+
return string
|
41
107
|
rescue Timeout::Error
|
42
108
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
43
109
|
sleep(5)
|
44
110
|
retry
|
45
111
|
end
|
46
112
|
end
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
113
|
+
|
114
|
+
# Update or create a String to WebTranslateIt.com
|
115
|
+
# Needs a HTTPS Connection
|
116
|
+
#
|
117
|
+
# Implementation Example:
|
118
|
+
#
|
119
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
120
|
+
# string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
|
121
|
+
# string.status = "status_obsolete"
|
122
|
+
# string.save(http_connection)
|
123
|
+
# end
|
124
|
+
#
|
125
|
+
|
126
|
+
def save(http_connection)
|
127
|
+
if self.new_record
|
128
|
+
self.create(http_connection)
|
129
|
+
else
|
130
|
+
self.update(http_connection)
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
# Delete a String on WebTranslateIt.com
|
135
|
+
# Needs a HTTPS Connection
|
136
|
+
#
|
137
|
+
# Implementation Example:
|
138
|
+
#
|
139
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
140
|
+
# string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
|
141
|
+
# string.delete(http_connection)
|
142
|
+
# end
|
143
|
+
#
|
144
|
+
|
145
|
+
def delete(http_connection)
|
146
|
+
request = Net::HTTP::Delete.new("/api/projects/#{self.api_key}/strings/#{self.id}")
|
52
147
|
request.add_field("X-Client-Name", "web_translate_it")
|
53
148
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
54
|
-
request.add_field("Content-Type", "application/json")
|
55
|
-
|
56
|
-
request.body = params.to_json
|
57
149
|
|
58
150
|
begin
|
59
151
|
Util.handle_response(http_connection.request(request), true)
|
@@ -64,14 +156,55 @@ module WebTranslateIt
|
|
64
156
|
end
|
65
157
|
end
|
66
158
|
|
67
|
-
|
68
|
-
|
159
|
+
# Gets a Translation for a String
|
160
|
+
# Needs a HTTPS Connection
|
161
|
+
#
|
162
|
+
# Implementation Example:
|
163
|
+
#
|
164
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
165
|
+
# string = WebTranslateIt::String.find(connection, 'secret_api_token', 1234)
|
166
|
+
# puts string.translation_for(connection, "fr") #=> A Translation object
|
167
|
+
# end
|
168
|
+
#
|
169
|
+
|
170
|
+
def translation_for(http_connection, locale)
|
171
|
+
return self.translations unless self.translations == []
|
172
|
+
request = Net::HTTP::Get.new("/api/projects/#{self.api_key}/strings/#{self.id}/locales/#{locale}/translations.yaml")
|
69
173
|
request.add_field("X-Client-Name", "web_translate_it")
|
70
174
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
71
|
-
request.add_field("Content-Type", "application/json")
|
72
|
-
|
73
|
-
request.body = params.to_json
|
74
175
|
|
176
|
+
begin
|
177
|
+
response = Util.handle_response(http_connection.request(request), true)
|
178
|
+
hash = YAML.load(response)
|
179
|
+
return nil if hash.empty?
|
180
|
+
translation = WebTranslateIt::Translation.new(api_key, hash)
|
181
|
+
return translation
|
182
|
+
|
183
|
+
rescue Timeout::Error
|
184
|
+
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
185
|
+
sleep(5)
|
186
|
+
retry
|
187
|
+
end
|
188
|
+
end
|
189
|
+
|
190
|
+
protected
|
191
|
+
|
192
|
+
# Save the changes made to a String to WebTranslateIt.com
|
193
|
+
# Needs a HTTPS Connection
|
194
|
+
#
|
195
|
+
|
196
|
+
def update(http_connection)
|
197
|
+
request = Net::HTTP::Put.new("/api/projects/#{self.api_key}/strings/#{self.id}.yaml")
|
198
|
+
request.add_field("X-Client-Name", "web_translate_it")
|
199
|
+
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
200
|
+
request.add_field("Content-Type", "application/json")
|
201
|
+
request.body = self.to_json
|
202
|
+
|
203
|
+
self.translations.each do |translation|
|
204
|
+
translation.string_id = self.id
|
205
|
+
translation.save(http_connection)
|
206
|
+
end
|
207
|
+
|
75
208
|
begin
|
76
209
|
Util.handle_response(http_connection.request(request), true)
|
77
210
|
rescue Timeout::Error
|
@@ -80,20 +213,51 @@ module WebTranslateIt
|
|
80
213
|
retry
|
81
214
|
end
|
82
215
|
end
|
83
|
-
|
84
|
-
|
85
|
-
|
216
|
+
|
217
|
+
# Create a new String to WebTranslateIt.com
|
218
|
+
# Needs a HTTPS Connection
|
219
|
+
#
|
220
|
+
|
221
|
+
def create(http_connection)
|
222
|
+
request = Net::HTTP::Post.new("/api/projects/#{self.api_key}/strings")
|
86
223
|
request.add_field("X-Client-Name", "web_translate_it")
|
87
224
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
225
|
+
request.add_field("Content-Type", "application/json")
|
226
|
+
request.body = self.to_json(true)
|
88
227
|
|
89
228
|
begin
|
90
|
-
Util.handle_response(http_connection.request(request), true)
|
229
|
+
response = YAML.load(Util.handle_response(http_connection.request(request), true))
|
230
|
+
self.id = response["id"]
|
231
|
+
self.new_record = false
|
232
|
+
return true
|
91
233
|
rescue Timeout::Error
|
92
234
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
93
235
|
sleep(5)
|
94
236
|
retry
|
95
237
|
end
|
96
238
|
end
|
97
|
-
|
239
|
+
|
240
|
+
def to_json(with_translations = false)
|
241
|
+
hash = {
|
242
|
+
"id" => id,
|
243
|
+
"key" => key,
|
244
|
+
"plural" => plural,
|
245
|
+
"type" => type,
|
246
|
+
"dev_comment" => dev_comment,
|
247
|
+
"status" => status,
|
248
|
+
"label" => label,
|
249
|
+
"category" => category,
|
250
|
+
"file" => {
|
251
|
+
"id" => file
|
252
|
+
}
|
253
|
+
}
|
254
|
+
if self.translations.any? && with_translations
|
255
|
+
hash.update({ "translations" => [] })
|
256
|
+
translations.each do |translation|
|
257
|
+
hash["translations"].push(translation.to_hash)
|
258
|
+
end
|
259
|
+
end
|
260
|
+
hash.to_json
|
261
|
+
end
|
98
262
|
end
|
99
263
|
end
|
@@ -2,26 +2,67 @@
|
|
2
2
|
module WebTranslateIt
|
3
3
|
class Term
|
4
4
|
require 'net/https'
|
5
|
+
require 'json'
|
5
6
|
|
6
|
-
attr_accessor :id, :
|
7
|
+
attr_accessor :api_key, :id, :text, :description, :created_at, :updated_at, :translations, :new_record
|
7
8
|
|
8
|
-
|
9
|
-
|
10
|
-
|
9
|
+
# Initialize a new WebTranslateIt::Term
|
10
|
+
# The only mandatory parameter is `api_key`
|
11
|
+
#
|
12
|
+
# Implementation Example:
|
13
|
+
#
|
14
|
+
# WebTranslateIt::Term.new('secret_api_token', { "text" => "Term Name" })
|
15
|
+
#
|
16
|
+
# to instantiate a new Term.
|
17
|
+
#
|
18
|
+
# translation_es = WebTranslateIt::TermTranslation.new({ "locale" => "es", "text" => "Hola" })
|
19
|
+
# translation_fr = WebTranslateIt::TermTranslation.new({ "locale" => "fr", "text" => "Bonjour" })
|
20
|
+
# WebTranslateIt::Term.new('secret_api_token', { "text" => "Hello", "translations" => [translation_es, translation_fr]})
|
21
|
+
#
|
22
|
+
# to instantiate a new Term with a Term Translations in Spanish and French.
|
23
|
+
|
24
|
+
def initialize(api_key, params = {})
|
25
|
+
self.api_key = api_key
|
26
|
+
self.id = params["id"] || nil
|
27
|
+
self.text = params["text"] || nil
|
28
|
+
self.description = params["description"] || nil
|
29
|
+
self.created_at = params["created_at"] || nil
|
30
|
+
self.updated_at = params["updated_at"] || nil
|
31
|
+
self.translations = params["translations"] || []
|
32
|
+
self.new_record = true
|
11
33
|
end
|
12
34
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
35
|
+
# Fetch all terms
|
36
|
+
# Needs a HTTPS Connection
|
37
|
+
#
|
38
|
+
# Implementation Example:
|
39
|
+
#
|
40
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
41
|
+
# terms = WebTranslateIt::Term.find_all(connection, 'secret_api_token')
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# puts terms.inspect #=> An array of WebTranslateIt::Term objects
|
45
|
+
#
|
46
|
+
# TODO: Implement pagination
|
47
|
+
|
48
|
+
def self.find_all(http_connection, api_key, params = {})
|
49
|
+
url = "/api/projects/#{api_key}/terms.yaml"
|
50
|
+
url += '?' + HashUtil.to_params(params) unless params.empty?
|
18
51
|
|
19
52
|
request = Net::HTTP::Get.new(url)
|
20
53
|
request.add_field("X-Client-Name", "web_translate_it")
|
21
54
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
22
55
|
|
23
56
|
begin
|
24
|
-
Util.handle_response(http_connection.request(request), true)
|
57
|
+
response = Util.handle_response(http_connection.request(request), true)
|
58
|
+
terms = []
|
59
|
+
YAML.load(response).each do |term_response|
|
60
|
+
term = WebTranslateIt::Term.new(api_key, term_response)
|
61
|
+
term.new_record = false
|
62
|
+
terms.push(term)
|
63
|
+
end
|
64
|
+
return terms
|
65
|
+
|
25
66
|
rescue Timeout::Error
|
26
67
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
27
68
|
sleep(5)
|
@@ -29,15 +70,31 @@ module WebTranslateIt
|
|
29
70
|
end
|
30
71
|
end
|
31
72
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
73
|
+
# Find a Term based on its ID
|
74
|
+
# Needs a HTTPS Connection
|
75
|
+
#
|
76
|
+
# Implementation Example:
|
77
|
+
#
|
78
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
79
|
+
# term = WebTranslateIt::Term.find(connection, 'secret_api_token', 1234)
|
80
|
+
# end
|
81
|
+
#
|
82
|
+
# puts term.inspect #=> A Term object
|
83
|
+
#
|
84
|
+
# to find and instantiate the Term which ID is `1234`.
|
85
|
+
#
|
86
|
+
|
87
|
+
def self.find(http_connection, api_key, term_id)
|
88
|
+
request = Net::HTTP::Get.new("/api/projects/#{api_key}/terms/#{term_id}.yaml")
|
36
89
|
request.add_field("X-Client-Name", "web_translate_it")
|
37
90
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
38
91
|
|
39
92
|
begin
|
40
|
-
|
93
|
+
response = http_connection.request(request)
|
94
|
+
return nil if response.code.to_i == 404
|
95
|
+
term = WebTranslateIt::Term.new(api_key, YAML.load(response.body))
|
96
|
+
term.new_record = false
|
97
|
+
return term
|
41
98
|
rescue Timeout::Error
|
42
99
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
43
100
|
sleep(5)
|
@@ -45,15 +102,41 @@ module WebTranslateIt
|
|
45
102
|
end
|
46
103
|
end
|
47
104
|
|
48
|
-
|
49
|
-
|
105
|
+
# Update or create a Term to WebTranslateIt.com
|
106
|
+
# Needs a HTTPS Connection
|
107
|
+
#
|
108
|
+
# Implementation Example:
|
109
|
+
#
|
110
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
111
|
+
# term = WebTranslateIt::Term.find(connection, 'secret_api_token', 1234)
|
112
|
+
# term.text = "Hello"
|
113
|
+
# term.save(http_connection)
|
114
|
+
# end
|
115
|
+
#
|
50
116
|
|
51
|
-
|
117
|
+
def save(http_connection)
|
118
|
+
if self.new_record
|
119
|
+
self.create(http_connection)
|
120
|
+
else
|
121
|
+
self.update(http_connection)
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
# Delete a Term on WebTranslateIt.com
|
126
|
+
# Needs a HTTPS Connection
|
127
|
+
#
|
128
|
+
# Implementation Example:
|
129
|
+
#
|
130
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
131
|
+
# term = WebTranslateIt::Term.find(connection, 'secret_api_token', 1234)
|
132
|
+
# term.delete(http_connection)
|
133
|
+
# end
|
134
|
+
#
|
135
|
+
|
136
|
+
def delete(http_connection)
|
137
|
+
request = Net::HTTP::Delete.new("/api/projects/#{self.api_key}/terms/#{self.id}")
|
52
138
|
request.add_field("X-Client-Name", "web_translate_it")
|
53
139
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
54
|
-
request.add_field("Content-Type", "application/json")
|
55
|
-
|
56
|
-
request.body = params.to_json
|
57
140
|
|
58
141
|
begin
|
59
142
|
Util.handle_response(http_connection.request(request), true)
|
@@ -64,16 +147,34 @@ module WebTranslateIt
|
|
64
147
|
end
|
65
148
|
end
|
66
149
|
|
67
|
-
|
68
|
-
|
150
|
+
# Gets a Translation for a Term
|
151
|
+
# Needs a HTTPS Connection
|
152
|
+
#
|
153
|
+
# Implementation Example:
|
154
|
+
#
|
155
|
+
# WebTranslateIt::Util.http_connection do |connection|
|
156
|
+
# term = WebTranslateIt::Term.find(connection, 'secret_api_token', 1234)
|
157
|
+
# puts term.translation_for(connection, "fr") #=> A TermTranslation object
|
158
|
+
# end
|
159
|
+
#
|
160
|
+
|
161
|
+
def translation_for(http_connection, locale)
|
162
|
+
return self.translations unless self.translations == []
|
163
|
+
request = Net::HTTP::Get.new("/api/projects/#{self.api_key}/terms/#{self.id}/locales/#{locale}/translations.yaml")
|
69
164
|
request.add_field("X-Client-Name", "web_translate_it")
|
70
165
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
71
|
-
request.add_field("Content-Type", "application/json")
|
72
|
-
|
73
|
-
request.body = params.to_json
|
74
166
|
|
75
167
|
begin
|
76
|
-
Util.handle_response(http_connection.request(request), true)
|
168
|
+
response = Util.handle_response(http_connection.request(request), true)
|
169
|
+
array = YAML.load(response)
|
170
|
+
return nil if array.empty?
|
171
|
+
translations = []
|
172
|
+
array.each do |translation|
|
173
|
+
term_translation = WebTranslateIt::TermTranslation.new(api_key, translation)
|
174
|
+
translations.push(term_translation)
|
175
|
+
end
|
176
|
+
return translations
|
177
|
+
|
77
178
|
rescue Timeout::Error
|
78
179
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
79
180
|
sleep(5)
|
@@ -81,10 +182,20 @@ module WebTranslateIt
|
|
81
182
|
end
|
82
183
|
end
|
83
184
|
|
84
|
-
|
85
|
-
|
185
|
+
protected
|
186
|
+
|
187
|
+
def update(http_connection)
|
188
|
+
request = Net::HTTP::Put.new("/api/projects/#{self.api_key}/terms/#{self.id}.yaml")
|
86
189
|
request.add_field("X-Client-Name", "web_translate_it")
|
87
190
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
191
|
+
request.add_field("Content-Type", "application/json")
|
192
|
+
|
193
|
+
request.body = self.to_json
|
194
|
+
|
195
|
+
self.translations.each do |translation|
|
196
|
+
translation.term_id = self.id
|
197
|
+
translation.save(http_connection)
|
198
|
+
end
|
88
199
|
|
89
200
|
begin
|
90
201
|
Util.handle_response(http_connection.request(request), true)
|
@@ -94,6 +205,41 @@ module WebTranslateIt
|
|
94
205
|
retry
|
95
206
|
end
|
96
207
|
end
|
208
|
+
|
209
|
+
def create(http_connection)
|
210
|
+
request = Net::HTTP::Post.new("/api/projects/#{api_key}/terms")
|
211
|
+
request.add_field("X-Client-Name", "web_translate_it")
|
212
|
+
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
213
|
+
request.add_field("Content-Type", "application/json")
|
214
|
+
|
215
|
+
request.body = self.to_json(true)
|
97
216
|
|
217
|
+
begin
|
218
|
+
response = YAML.load(Util.handle_response(http_connection.request(request), true))
|
219
|
+
self.id = response["id"]
|
220
|
+
self.new_record = false
|
221
|
+
return true
|
222
|
+
|
223
|
+
rescue Timeout::Error
|
224
|
+
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
225
|
+
sleep(5)
|
226
|
+
retry
|
227
|
+
end
|
228
|
+
end
|
229
|
+
|
230
|
+
def to_json(with_translations = false)
|
231
|
+
hash = {
|
232
|
+
"id" => id,
|
233
|
+
"text" => text,
|
234
|
+
"description" => description
|
235
|
+
}
|
236
|
+
if self.translations.any? && with_translations
|
237
|
+
hash.update({ "translations" => [] })
|
238
|
+
translations.each do |translation|
|
239
|
+
hash["translations"].push(translation.to_hash)
|
240
|
+
end
|
241
|
+
end
|
242
|
+
hash.to_json
|
243
|
+
end
|
98
244
|
end
|
99
245
|
end
|