web_translate_it 2.0.0.rc2 → 2.0.0.rc3
Sign up to get free protection for your applications and to get access to all the features.
- data/bin/wti +11 -25
- data/history.md +7 -1
- data/lib/web_translate_it.rb +2 -1
- data/lib/web_translate_it/command_line.rb +31 -41
- data/lib/web_translate_it/configuration.rb +2 -1
- data/lib/web_translate_it/connection.rb +62 -0
- data/lib/web_translate_it/project.rb +14 -18
- data/lib/web_translate_it/string.rb +42 -58
- data/lib/web_translate_it/term.rb +41 -52
- data/lib/web_translate_it/term_translation.rb +14 -22
- data/lib/web_translate_it/translation.rb +9 -12
- data/lib/web_translate_it/util.rb +1 -39
- data/spec/web_translate_it/string_spec.rb +57 -58
- data/spec/web_translate_it/term_spec.rb +53 -54
- data/version +1 -1
- metadata +3 -2
@@ -4,25 +4,23 @@ module WebTranslateIt
|
|
4
4
|
require 'net/https'
|
5
5
|
require 'json'
|
6
6
|
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :id, :text, :description, :created_at, :updated_at, :translations, :new_record
|
8
8
|
|
9
9
|
# Initialize a new WebTranslateIt::Term
|
10
|
-
# The only mandatory parameter is `api_key`
|
11
10
|
#
|
12
11
|
# Implementation Example:
|
13
12
|
#
|
14
|
-
# WebTranslateIt::Term.new(
|
13
|
+
# WebTranslateIt::Term.new({ "text" => "Term Name" })
|
15
14
|
#
|
16
15
|
# to instantiate a new Term.
|
17
16
|
#
|
18
17
|
# translation_es = WebTranslateIt::TermTranslation.new({ "locale" => "es", "text" => "Hola" })
|
19
18
|
# translation_fr = WebTranslateIt::TermTranslation.new({ "locale" => "fr", "text" => "Bonjour" })
|
20
|
-
# WebTranslateIt::Term.new(
|
19
|
+
# WebTranslateIt::Term.new({ "text" => "Hello", "translations" => [translation_es, translation_fr]})
|
21
20
|
#
|
22
21
|
# to instantiate a new Term with a Term Translations in Spanish and French.
|
23
22
|
|
24
|
-
def initialize(
|
25
|
-
self.api_key = api_key
|
23
|
+
def initialize(params = {})
|
26
24
|
self.id = params["id"] || nil
|
27
25
|
self.text = params["text"] || nil
|
28
26
|
self.description = params["description"] || nil
|
@@ -33,20 +31,19 @@ module WebTranslateIt
|
|
33
31
|
end
|
34
32
|
|
35
33
|
# Fetch all terms
|
36
|
-
# Needs a HTTPS Connection
|
37
34
|
#
|
38
35
|
# Implementation Example:
|
39
36
|
#
|
40
|
-
# WebTranslateIt::
|
41
|
-
# terms = WebTranslateIt::Term.find_all
|
37
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
38
|
+
# terms = WebTranslateIt::Term.find_all
|
42
39
|
# end
|
43
40
|
#
|
44
41
|
# puts terms.inspect #=> An array of WebTranslateIt::Term objects
|
45
42
|
#
|
46
43
|
# TODO: Implement pagination
|
47
44
|
|
48
|
-
def self.find_all(
|
49
|
-
url = "/api/projects/#{api_key}/terms.yaml"
|
45
|
+
def self.find_all(params = {})
|
46
|
+
url = "/api/projects/#{Connection.api_key}/terms.yaml"
|
50
47
|
url += '?' + HashUtil.to_params(params) unless params.empty?
|
51
48
|
|
52
49
|
request = Net::HTTP::Get.new(url)
|
@@ -54,10 +51,10 @@ module WebTranslateIt
|
|
54
51
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
55
52
|
|
56
53
|
begin
|
57
|
-
response = Util.handle_response(http_connection.request(request), true)
|
54
|
+
response = Util.handle_response(Connection.http_connection.request(request), true)
|
58
55
|
terms = []
|
59
56
|
YAML.load(response).each do |term_response|
|
60
|
-
term = WebTranslateIt::Term.new(
|
57
|
+
term = WebTranslateIt::Term.new(term_response)
|
61
58
|
term.new_record = false
|
62
59
|
terms.push(term)
|
63
60
|
end
|
@@ -71,12 +68,11 @@ module WebTranslateIt
|
|
71
68
|
end
|
72
69
|
|
73
70
|
# Find a Term based on its ID
|
74
|
-
# Needs a HTTPS Connection
|
75
71
|
#
|
76
72
|
# Implementation Example:
|
77
73
|
#
|
78
|
-
# WebTranslateIt::
|
79
|
-
# term = WebTranslateIt::Term.find(
|
74
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
75
|
+
# term = WebTranslateIt::Term.find(1234)
|
80
76
|
# end
|
81
77
|
#
|
82
78
|
# puts term.inspect #=> A Term object
|
@@ -84,15 +80,15 @@ module WebTranslateIt
|
|
84
80
|
# to find and instantiate the Term which ID is `1234`.
|
85
81
|
#
|
86
82
|
|
87
|
-
def self.find(
|
88
|
-
request = Net::HTTP::Get.new("/api/projects/#{api_key}/terms/#{term_id}.yaml")
|
83
|
+
def self.find(term_id)
|
84
|
+
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{term_id}.yaml")
|
89
85
|
request.add_field("X-Client-Name", "web_translate_it")
|
90
86
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
91
87
|
|
92
88
|
begin
|
93
|
-
response = http_connection.request(request)
|
89
|
+
response = Connection.http_connection.request(request)
|
94
90
|
return nil if response.code.to_i == 404
|
95
|
-
term = WebTranslateIt::Term.new(
|
91
|
+
term = WebTranslateIt::Term.new(YAML.load(response.body))
|
96
92
|
term.new_record = false
|
97
93
|
return term
|
98
94
|
rescue Timeout::Error
|
@@ -103,43 +99,37 @@ module WebTranslateIt
|
|
103
99
|
end
|
104
100
|
|
105
101
|
# Update or create a Term to WebTranslateIt.com
|
106
|
-
# Needs a HTTPS Connection
|
107
102
|
#
|
108
103
|
# Implementation Example:
|
109
104
|
#
|
110
|
-
# WebTranslateIt::
|
111
|
-
# term = WebTranslateIt::Term.find(
|
105
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
106
|
+
# term = WebTranslateIt::Term.find(1234)
|
112
107
|
# term.text = "Hello"
|
113
|
-
# term.save
|
108
|
+
# term.save
|
114
109
|
# end
|
115
110
|
#
|
116
111
|
|
117
|
-
def save
|
118
|
-
|
119
|
-
self.create(http_connection)
|
120
|
-
else
|
121
|
-
self.update(http_connection)
|
122
|
-
end
|
112
|
+
def save
|
113
|
+
self.new_record ? self.create : self.update
|
123
114
|
end
|
124
115
|
|
125
116
|
# Delete a Term on WebTranslateIt.com
|
126
|
-
# Needs a HTTPS Connection
|
127
117
|
#
|
128
118
|
# Implementation Example:
|
129
119
|
#
|
130
|
-
# WebTranslateIt::
|
131
|
-
# term = WebTranslateIt::Term.find(
|
132
|
-
# term.delete
|
120
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
121
|
+
# term = WebTranslateIt::Term.find(1234)
|
122
|
+
# term.delete
|
133
123
|
# end
|
134
124
|
#
|
135
125
|
|
136
|
-
def delete
|
137
|
-
request = Net::HTTP::Delete.new("/api/projects/#{
|
126
|
+
def delete
|
127
|
+
request = Net::HTTP::Delete.new("/api/projects/#{Connection.api_key}/terms/#{self.id}")
|
138
128
|
request.add_field("X-Client-Name", "web_translate_it")
|
139
129
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
140
130
|
|
141
131
|
begin
|
142
|
-
Util.handle_response(http_connection.request(request), true)
|
132
|
+
Util.handle_response(Connection.http_connection.request(request), true)
|
143
133
|
rescue Timeout::Error
|
144
134
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
145
135
|
sleep(5)
|
@@ -148,29 +138,28 @@ module WebTranslateIt
|
|
148
138
|
end
|
149
139
|
|
150
140
|
# Gets a Translation for a Term
|
151
|
-
# Needs a HTTPS Connection
|
152
141
|
#
|
153
142
|
# Implementation Example:
|
154
143
|
#
|
155
|
-
# WebTranslateIt::
|
156
|
-
# term = WebTranslateIt::Term.find(
|
157
|
-
# puts term.translation_for(
|
144
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
145
|
+
# term = WebTranslateIt::Term.find(1234)
|
146
|
+
# puts term.translation_for("fr") #=> A TermTranslation object
|
158
147
|
# end
|
159
148
|
#
|
160
149
|
|
161
|
-
def translation_for(
|
150
|
+
def translation_for(locale)
|
162
151
|
return self.translations unless self.translations == []
|
163
|
-
request = Net::HTTP::Get.new("/api/projects/#{
|
152
|
+
request = Net::HTTP::Get.new("/api/projects/#{Connection.api_key}/terms/#{self.id}/locales/#{locale}/translations.yaml")
|
164
153
|
request.add_field("X-Client-Name", "web_translate_it")
|
165
154
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
166
155
|
|
167
156
|
begin
|
168
|
-
response = Util.handle_response(http_connection.request(request), true)
|
157
|
+
response = Util.handle_response(Connection.http_connection.request(request), true)
|
169
158
|
array = YAML.load(response)
|
170
159
|
return nil if array.empty?
|
171
160
|
translations = []
|
172
161
|
array.each do |translation|
|
173
|
-
term_translation = WebTranslateIt::TermTranslation.new(
|
162
|
+
term_translation = WebTranslateIt::TermTranslation.new(translation)
|
174
163
|
translations.push(term_translation)
|
175
164
|
end
|
176
165
|
return translations
|
@@ -184,8 +173,8 @@ module WebTranslateIt
|
|
184
173
|
|
185
174
|
protected
|
186
175
|
|
187
|
-
def update
|
188
|
-
request = Net::HTTP::Put.new("/api/projects/#{
|
176
|
+
def update
|
177
|
+
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{self.id}.yaml")
|
189
178
|
request.add_field("X-Client-Name", "web_translate_it")
|
190
179
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
191
180
|
request.add_field("Content-Type", "application/json")
|
@@ -194,11 +183,11 @@ module WebTranslateIt
|
|
194
183
|
|
195
184
|
self.translations.each do |translation|
|
196
185
|
translation.term_id = self.id
|
197
|
-
translation.save
|
186
|
+
translation.save
|
198
187
|
end
|
199
188
|
|
200
189
|
begin
|
201
|
-
Util.handle_response(http_connection.request(request), true)
|
190
|
+
Util.handle_response(Connection.http_connection.request(request), true)
|
202
191
|
rescue Timeout::Error
|
203
192
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
204
193
|
sleep(5)
|
@@ -206,8 +195,8 @@ module WebTranslateIt
|
|
206
195
|
end
|
207
196
|
end
|
208
197
|
|
209
|
-
def create
|
210
|
-
request = Net::HTTP::Post.new("/api/projects/#{api_key}/terms")
|
198
|
+
def create
|
199
|
+
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms")
|
211
200
|
request.add_field("X-Client-Name", "web_translate_it")
|
212
201
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
213
202
|
request.add_field("Content-Type", "application/json")
|
@@ -215,7 +204,7 @@ module WebTranslateIt
|
|
215
204
|
request.body = self.to_json(true)
|
216
205
|
|
217
206
|
begin
|
218
|
-
response = YAML.load(Util.handle_response(http_connection.request(request), true))
|
207
|
+
response = YAML.load(Util.handle_response(Connection.http_connection.request(request), true))
|
219
208
|
self.id = response["id"]
|
220
209
|
self.new_record = false
|
221
210
|
return true
|
@@ -4,20 +4,18 @@ module WebTranslateIt
|
|
4
4
|
require 'net/https'
|
5
5
|
require 'json'
|
6
6
|
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :id, :locale, :text, :description, :status, :new_record, :term_id
|
8
8
|
|
9
9
|
# Initialize a new WebTranslateIt::TermTranslation
|
10
|
-
# Mandatory parameter is `api_key`
|
11
10
|
#
|
12
11
|
# Implementation Example:
|
13
12
|
#
|
14
|
-
# WebTranslateIt::TermTranslation.new(
|
13
|
+
# WebTranslateIt::TermTranslation.new({ "text" => "Super!" })
|
15
14
|
#
|
16
15
|
# to instantiate a new TermTranslation.
|
17
16
|
#
|
18
17
|
|
19
|
-
def initialize(
|
20
|
-
self.api_key = api_key
|
18
|
+
def initialize(params = {})
|
21
19
|
self.id = params["id"] || nil
|
22
20
|
self.locale = params["locale"] || nil
|
23
21
|
self.text = params["text"] || nil
|
@@ -31,19 +29,14 @@ module WebTranslateIt
|
|
31
29
|
#
|
32
30
|
# Implementation Example:
|
33
31
|
#
|
34
|
-
# translation = WebTranslateIt::TermTranslation.new(
|
35
|
-
#
|
36
|
-
#
|
37
|
-
# translation.save(connection)
|
32
|
+
# translation = WebTranslateIt::TermTranslation.new({ "term_id" => "1234", "text" => "Super!" })
|
33
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
34
|
+
# translation.save
|
38
35
|
# end
|
39
36
|
#
|
40
37
|
|
41
|
-
def save
|
42
|
-
|
43
|
-
self.create(http_connection)
|
44
|
-
else
|
45
|
-
self.update(http_connection)
|
46
|
-
end
|
38
|
+
def save
|
39
|
+
self.new_record ? self.create : self.update
|
47
40
|
end
|
48
41
|
|
49
42
|
def to_hash
|
@@ -58,8 +51,8 @@ module WebTranslateIt
|
|
58
51
|
|
59
52
|
protected
|
60
53
|
|
61
|
-
def create
|
62
|
-
request = Net::HTTP::Post.new("/api/projects/#{
|
54
|
+
def create
|
55
|
+
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/terms/#{self.term_id}/locales/#{self.locale}/translations")
|
63
56
|
request.add_field("X-Client-Name", "web_translate_it")
|
64
57
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
65
58
|
request.add_field("Content-Type", "application/json")
|
@@ -67,7 +60,7 @@ module WebTranslateIt
|
|
67
60
|
request.body = self.to_hash.to_json
|
68
61
|
|
69
62
|
begin
|
70
|
-
response = Util.handle_response(http_connection.request(request), true)
|
63
|
+
response = Util.handle_response(Connection.http_connection.request(request), true)
|
71
64
|
response = YAML.load(response)
|
72
65
|
self.id = response["id"]
|
73
66
|
self.new_record = false
|
@@ -80,8 +73,8 @@ module WebTranslateIt
|
|
80
73
|
end
|
81
74
|
end
|
82
75
|
|
83
|
-
def update
|
84
|
-
request = Net::HTTP::Put.new("/api/projects/#{
|
76
|
+
def update
|
77
|
+
request = Net::HTTP::Put.new("/api/projects/#{Connection.api_key}/terms/#{self.id}/locales/#{self.locale}/translations/#{self.id}")
|
85
78
|
request.add_field("X-Client-Name", "web_translate_it")
|
86
79
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
87
80
|
request.add_field("Content-Type", "application/json")
|
@@ -89,7 +82,7 @@ module WebTranslateIt
|
|
89
82
|
request.body = self.to_hash.to_json
|
90
83
|
|
91
84
|
begin
|
92
|
-
Util.handle_response(http_connection.request(request), true)
|
85
|
+
Util.handle_response(Connection.http_connection.request(request), true)
|
93
86
|
|
94
87
|
rescue Timeout::Error
|
95
88
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
@@ -97,6 +90,5 @@ module WebTranslateIt
|
|
97
90
|
retry
|
98
91
|
end
|
99
92
|
end
|
100
|
-
|
101
93
|
end
|
102
94
|
end
|
@@ -4,20 +4,18 @@ module WebTranslateIt
|
|
4
4
|
require 'net/https'
|
5
5
|
require 'json'
|
6
6
|
|
7
|
-
attr_accessor :
|
7
|
+
attr_accessor :id, :locale, :text, :status, :created_at, :updated_at, :version, :string_id
|
8
8
|
|
9
9
|
# Initialize a new WebTranslateIt::Translation
|
10
|
-
# Mandatory parameters are `api_key` and { "string_id" => "1234" }
|
11
10
|
#
|
12
11
|
# Implementation Example:
|
13
12
|
#
|
14
|
-
# WebTranslateIt::Translation.new(
|
13
|
+
# WebTranslateIt::Translation.new({ "string_id" => "1234", "text" => "Super!" })
|
15
14
|
#
|
16
15
|
# to instantiate a new Translation without any text.
|
17
16
|
#
|
18
17
|
|
19
|
-
def initialize(
|
20
|
-
self.api_key = api_key
|
18
|
+
def initialize(params = {})
|
21
19
|
self.id = params["id"] || nil
|
22
20
|
self.locale = params["locale"] || nil
|
23
21
|
self.text = params["text"] || nil
|
@@ -36,22 +34,21 @@ module WebTranslateIt
|
|
36
34
|
#
|
37
35
|
# Implementation Example:
|
38
36
|
#
|
39
|
-
# translation = WebTranslateIt::Translation.new(
|
40
|
-
#
|
41
|
-
#
|
42
|
-
# translation.save(connection)
|
37
|
+
# translation = WebTranslateIt::Translation.new({ "string_id" => "1234", "text" => "Super!" })
|
38
|
+
# WebTranslateIt::Connection.new('secret_api_token') do
|
39
|
+
# translation.save
|
43
40
|
# end
|
44
41
|
#
|
45
42
|
|
46
|
-
def save
|
47
|
-
request = Net::HTTP::Post.new("/api/projects/#{
|
43
|
+
def save
|
44
|
+
request = Net::HTTP::Post.new("/api/projects/#{Connection.api_key}/strings/#{self.string_id}/locales/#{self.locale}/translations")
|
48
45
|
request.add_field("X-Client-Name", "web_translate_it")
|
49
46
|
request.add_field("X-Client-Version", WebTranslateIt::Util.version)
|
50
47
|
request.add_field("Content-Type", "application/json")
|
51
48
|
request.body = self.to_hash.to_json
|
52
49
|
|
53
50
|
begin
|
54
|
-
Util.handle_response(http_connection.request(request), true)
|
51
|
+
Util.handle_response(Connection.http_connection.request(request), true)
|
55
52
|
rescue Timeout::Error
|
56
53
|
puts "The request timed out. The service may be overloaded. We will retry in 5 seconds."
|
57
54
|
sleep(5)
|
@@ -1,9 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
2
|
module WebTranslateIt
|
3
|
-
require 'net/http'
|
4
|
-
require 'net/https'
|
5
|
-
require 'uri'
|
6
|
-
require 'ostruct'
|
7
3
|
|
8
4
|
# A few useful functions
|
9
5
|
class Util
|
@@ -13,41 +9,7 @@ module WebTranslateIt
|
|
13
9
|
def self.version
|
14
10
|
IO.read(File.expand_path('../../../version', __FILE__))
|
15
11
|
end
|
16
|
-
|
17
|
-
# Yields a HTTP connection over SSL to Web Translate It.
|
18
|
-
# This is used for the connections to the API throughout the library.
|
19
|
-
# Use it like so:
|
20
|
-
#
|
21
|
-
# WebTranslateIt::Util.http_connection do |http|
|
22
|
-
# request = Net::HTTP::Get.new(api_url)
|
23
|
-
# response = http.request(request)
|
24
|
-
# end
|
25
|
-
#
|
26
|
-
# This method will try to connect through a proxy if `http_proxy` is set.
|
27
|
-
#
|
28
|
-
def self.http_connection
|
29
|
-
proxy = ENV['http_proxy'] ? URI.parse(ENV['http_proxy']) : OpenStruct.new
|
30
|
-
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
|
31
|
-
http.use_ssl = true
|
32
|
-
http.open_timeout = http.read_timeout = 30
|
33
|
-
begin
|
34
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
35
|
-
if File.exists?('/etc/ssl/certs') # Ubuntu
|
36
|
-
http.ca_path = '/etc/ssl/certs'
|
37
|
-
else
|
38
|
-
http.ca_file = File.expand_path('cacert.pem', __FILE__)
|
39
|
-
end
|
40
|
-
yield http.start
|
41
|
-
rescue OpenSSL::SSL::SSLError
|
42
|
-
puts "Unable to verify SSL certificate."
|
43
|
-
http = Net::HTTP::Proxy(proxy.host, proxy.port, proxy.user, proxy.password).new('webtranslateit.com', 443)
|
44
|
-
http.use_ssl = true
|
45
|
-
http.open_timeout = http.read_timeout = 30
|
46
|
-
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
47
|
-
yield http.start
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
12
|
+
|
51
13
|
def self.calculate_percentage(processed, total)
|
52
14
|
return 0 if total == 0
|
53
15
|
((processed*10)/total).to_f.ceil*10
|
@@ -6,8 +6,7 @@ describe WebTranslateIt::String do
|
|
6
6
|
|
7
7
|
describe "#initialize" do
|
8
8
|
it "should assign api_key and many parameters" do
|
9
|
-
string = WebTranslateIt::String.new(
|
10
|
-
string.api_key.should == api_key
|
9
|
+
string = WebTranslateIt::String.new({ "id" => 1234, "key" => "bacon"})
|
11
10
|
string.id.should == 1234
|
12
11
|
string.key.should == "bacon"
|
13
12
|
end
|
@@ -15,80 +14,80 @@ describe WebTranslateIt::String do
|
|
15
14
|
|
16
15
|
describe "#save" do
|
17
16
|
it "should create a new String" do
|
18
|
-
WebTranslateIt::
|
19
|
-
string = WebTranslateIt::String.new(
|
20
|
-
string.save
|
17
|
+
WebTranslateIt::Connection.new(api_key) do
|
18
|
+
string = WebTranslateIt::String.new({ "key" => "bacon" })
|
19
|
+
string.save
|
21
20
|
string.id.should_not be_nil
|
22
|
-
string_fetched = WebTranslateIt::String.find(
|
21
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
23
22
|
string_fetched.should_not be_nil
|
24
23
|
string_fetched.should be_a(WebTranslateIt::String)
|
25
24
|
string_fetched.id.should == string.id
|
26
|
-
string.delete
|
25
|
+
string.delete
|
27
26
|
end
|
28
27
|
end
|
29
28
|
|
30
29
|
it "should update an existing String" do
|
31
|
-
WebTranslateIt::
|
32
|
-
string = WebTranslateIt::String.new(
|
33
|
-
string.save
|
30
|
+
WebTranslateIt::Connection.new(api_key) do
|
31
|
+
string = WebTranslateIt::String.new({ "key" => "bacony" })
|
32
|
+
string.save
|
34
33
|
string.key = "bacon changed"
|
35
|
-
string.save
|
36
|
-
string_fetched = WebTranslateIt::String.find(
|
34
|
+
string.save
|
35
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
37
36
|
string_fetched.key.should == "bacon changed"
|
38
|
-
string.delete
|
37
|
+
string.delete
|
39
38
|
end
|
40
39
|
end
|
41
40
|
|
42
41
|
it "should create a new String with Translation" do
|
43
|
-
translation1 = WebTranslateIt::Translation.new(
|
44
|
-
translation2 = WebTranslateIt::Translation.new(
|
42
|
+
translation1 = WebTranslateIt::Translation.new({ "locale" => "en", "text" => "Hello" })
|
43
|
+
translation2 = WebTranslateIt::Translation.new({ "locale" => "fr", "text" => "Bonjour" })
|
45
44
|
|
46
|
-
string = WebTranslateIt::String.new(
|
47
|
-
WebTranslateIt::
|
48
|
-
string.save
|
49
|
-
string_fetched = WebTranslateIt::String.find(
|
50
|
-
string_fetched.translation_for(
|
51
|
-
string_fetched.translation_for(
|
52
|
-
string_fetched.translation_for(
|
53
|
-
string_fetched.translation_for(
|
54
|
-
string_fetched.translation_for(
|
55
|
-
string.delete
|
45
|
+
string = WebTranslateIt::String.new({ "key" => "bacon", "translations" => [translation1, translation2] })
|
46
|
+
WebTranslateIt::Connection.new(api_key) do
|
47
|
+
string.save
|
48
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
49
|
+
string_fetched.translation_for("en").should_not be_nil
|
50
|
+
string_fetched.translation_for("en").text.should == "Hello"
|
51
|
+
string_fetched.translation_for("fr").should_not be_nil
|
52
|
+
string_fetched.translation_for("fr").text.should == "Bonjour"
|
53
|
+
string_fetched.translation_for("es").should be_nil
|
54
|
+
string.delete
|
56
55
|
end
|
57
56
|
end
|
58
57
|
|
59
58
|
it "should update a String and save its Translation" do
|
60
|
-
translation1 = WebTranslateIt::Translation.new(
|
61
|
-
translation2 = WebTranslateIt::Translation.new(
|
59
|
+
translation1 = WebTranslateIt::Translation.new({ "locale" => "en", "text" => "Hello" })
|
60
|
+
translation2 = WebTranslateIt::Translation.new({ "locale" => "fr", "text" => "Bonjour" })
|
62
61
|
|
63
|
-
string = WebTranslateIt::String.new(
|
64
|
-
WebTranslateIt::
|
65
|
-
string.save
|
66
|
-
string_fetched = WebTranslateIt::String.find(
|
67
|
-
string_fetched.translation_for(
|
62
|
+
string = WebTranslateIt::String.new({ "key" => "bacon" })
|
63
|
+
WebTranslateIt::Connection.new(api_key) do
|
64
|
+
string.save
|
65
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
66
|
+
string_fetched.translation_for("fr").should be_nil
|
68
67
|
|
69
68
|
string_fetched.translations = [translation1, translation2]
|
70
|
-
string_fetched.save
|
69
|
+
string_fetched.save
|
71
70
|
|
72
|
-
string_fetched = WebTranslateIt::String.find(
|
73
|
-
string_fetched.translation_for(
|
74
|
-
string_fetched.translation_for(
|
75
|
-
string_fetched.translation_for(
|
76
|
-
string_fetched.translation_for(
|
77
|
-
string.delete
|
71
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
72
|
+
string_fetched.translation_for("en").should_not be_nil
|
73
|
+
string_fetched.translation_for("en").text.should == "Hello"
|
74
|
+
string_fetched.translation_for("fr").should_not be_nil
|
75
|
+
string_fetched.translation_for("fr").text.should == "Bonjour"
|
76
|
+
string.delete
|
78
77
|
end
|
79
78
|
end
|
80
79
|
end
|
81
80
|
|
82
81
|
describe "#delete" do
|
83
82
|
it "should delete a String" do
|
84
|
-
string = WebTranslateIt::String.new(
|
85
|
-
WebTranslateIt::
|
86
|
-
string.save
|
87
|
-
string_fetched = WebTranslateIt::String.find(
|
83
|
+
string = WebTranslateIt::String.new({ "key" => "bacon" })
|
84
|
+
WebTranslateIt::Connection.new(api_key) do
|
85
|
+
string.save
|
86
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
88
87
|
string_fetched.should_not be_nil
|
89
88
|
|
90
|
-
string_fetched.delete
|
91
|
-
string_fetched = WebTranslateIt::String.find(
|
89
|
+
string_fetched.delete
|
90
|
+
string_fetched = WebTranslateIt::String.find(string.id)
|
92
91
|
string_fetched.should be_nil
|
93
92
|
end
|
94
93
|
end
|
@@ -96,31 +95,31 @@ describe WebTranslateIt::String do
|
|
96
95
|
|
97
96
|
describe "#find_all" do
|
98
97
|
it "should find many strings" do
|
99
|
-
WebTranslateIt::
|
100
|
-
string1 = WebTranslateIt::String.new(
|
101
|
-
string1.save
|
102
|
-
string2 = WebTranslateIt::String.new(
|
103
|
-
string2.save
|
104
|
-
string3 = WebTranslateIt::String.new(
|
105
|
-
string3.save
|
98
|
+
WebTranslateIt::Connection.new(api_key) do
|
99
|
+
string1 = WebTranslateIt::String.new({ "key" => "bacon" })
|
100
|
+
string1.save
|
101
|
+
string2 = WebTranslateIt::String.new({ "key" => "tart" })
|
102
|
+
string2.save
|
103
|
+
string3 = WebTranslateIt::String.new({ "key" => "bacon tart" })
|
104
|
+
string3.save
|
106
105
|
|
107
|
-
strings = WebTranslateIt::String.find_all(
|
106
|
+
strings = WebTranslateIt::String.find_all({ "key" => "bacon" })
|
108
107
|
strings.count.should == 2
|
109
108
|
strings[0].key.should == "bacon"
|
110
109
|
strings[1].key.should == "bacon tart"
|
111
110
|
|
112
|
-
strings = WebTranslateIt::String.find_all(
|
111
|
+
strings = WebTranslateIt::String.find_all({ "key" => "tart" })
|
113
112
|
strings.count.should == 2
|
114
113
|
strings[0].key.should == "tart"
|
115
114
|
strings[1].key.should == "bacon tart"
|
116
115
|
|
117
|
-
strings = WebTranslateIt::String.find_all(
|
116
|
+
strings = WebTranslateIt::String.find_all({ "key" => "bacon tart" })
|
118
117
|
strings.count.should == 1
|
119
118
|
strings[0].key.should == "bacon tart"
|
120
119
|
|
121
|
-
string1.delete
|
122
|
-
string2.delete
|
123
|
-
string3.delete
|
120
|
+
string1.delete
|
121
|
+
string2.delete
|
122
|
+
string3.delete
|
124
123
|
end
|
125
124
|
end
|
126
125
|
end
|