txgh 3.0.0 → 4.0.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2fdd0fc2aef582a990ec6d4e3ba910272c983b5c
4
- data.tar.gz: a23ff4651a74b4d83fa8aa6568000651e77092b0
3
+ metadata.gz: 500294b033cfebc1c6c9c52a28b75cc5e564bd13
4
+ data.tar.gz: 5a3b866c7976246ec5e0c5532c69d044ff7b7dc2
5
5
  SHA512:
6
- metadata.gz: 742668090fc7458a79091b64d4c1ddfc4cf83c8de2a70579b970639a3145c55f2fdff056951ccf1a9626086c28b96faed39cca8ba6f1e16c0aece5835bd13c2b
7
- data.tar.gz: 2cda6f1fe1938a253585beef2e806e8c7cf9ddada727735bbfd568c754e337d1cfd1cd16a2838c0ca0e604ae3a9fbf6832ed2dd9cc0723ab2cf24944ef198065
6
+ metadata.gz: 56e81d08e7020c5c9aa292d5eb7801da5afab2d28509cffbe8b261c0248d0e48c9dea7aeb694b7694f6a8e689efa480454d325e6570caaeb1a4a386086d9ade2
7
+ data.tar.gz: c2b1b2ff73af79c5aa0bfd0acc29e7fbfba37ef839c30ef7c4c7c90241fc9024e280f33fc1bb2eba007368fb47cf323cb65e37297dd04276c920c9796ef022aa
@@ -15,7 +15,7 @@ module Txgh
15
15
  def delete_resources
16
16
  tx_resources.each do |tx_resource|
17
17
  logger.info("Deleting #{tx_resource.resource_slug}")
18
- project.api.delete(tx_resource)
18
+ project.api.delete_resource(tx_resource)
19
19
  end
20
20
  end
21
21
 
@@ -65,93 +65,105 @@ module Txgh
65
65
  }
66
66
 
67
67
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resources/"
68
- response = connection.post(url, payload)
69
- raise_error!(response)
68
+ post(url, payload)
70
69
  end
71
70
 
72
- def delete(tx_resource)
71
+ def delete_resource(tx_resource)
73
72
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resource/#{tx_resource.resource_slug}/"
74
- connection.delete(url)
73
+ delete(url)
75
74
  end
76
75
 
77
76
  def update_content(tx_resource, content)
78
77
  content_io = get_content_io(tx_resource, content)
79
78
  payload = { content: content_io }
80
79
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resource/#{tx_resource.resource_slug}/content/"
81
- response = connection.put(url, payload)
82
- raise_error!(response)
80
+ put(url, payload)
83
81
  end
84
82
 
85
83
  def update_details(tx_resource, details = {})
86
84
  url = "#{API_ROOT}/project/#{tx_resource.project_slug}/resource/#{tx_resource.resource_slug}/"
87
- response = connection.put(url, details)
88
- raise_error!(response)
85
+ put(url, details)
89
86
  end
90
87
 
91
88
  def resource_exists?(tx_resource)
92
89
  project = tx_resource.project_slug
93
90
  slug = tx_resource.resource_slug
94
- response = connection.get("#{API_ROOT}/project/#{project}/resource/#{slug}/")
91
+ response = get("#{API_ROOT}/project/#{project}/resource/#{slug}/")
95
92
  response.status == 200
93
+ rescue TransifexNotFoundError
94
+ false
96
95
  end
97
96
 
98
97
  def download(tx_resource, lang)
99
98
  project_slug = tx_resource.project_slug
100
99
  resource_slug = tx_resource.resource_slug
101
- response = connection.get(
100
+
101
+ json_data = get_json(
102
102
  "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/translation/#{lang}/"
103
103
  )
104
104
 
105
- raise_error!(response)
106
-
107
- json_data = JSON.parse(response.body)
108
105
  json_data['content']
109
106
  end
110
107
 
111
108
  def get_resource(project_slug, resource_slug)
112
109
  url = "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/"
113
- response = connection.get(url)
114
- raise_error!(response)
115
- JSON.parse(response.body)
110
+ get_json(url)
116
111
  end
117
112
 
118
113
  def get_resources(project_slug)
119
114
  url = "#{API_ROOT}/project/#{project_slug}/resources/"
120
- response = connection.get(url)
121
- raise_error!(response)
122
- JSON.parse(response.body)
115
+ get_json(url)
123
116
  end
124
117
 
125
118
  def get_languages(project_slug)
126
119
  url = "#{API_ROOT}/project/#{project_slug}/languages/"
127
- response = connection.get(url)
128
- raise_error!(response)
129
- JSON.parse(response.body)
120
+ get_json(url)
130
121
  end
131
122
 
132
123
  def get_project(project_slug)
133
124
  url = "#{API_ROOT}/project/#{project_slug}/"
134
- response = connection.get(url)
135
- raise_error!(response)
136
- JSON.parse(response.body)
125
+ get_json(url)
137
126
  end
138
127
 
139
128
  def get_formats
140
129
  url = "#{API_ROOT}/formats/"
141
- response = connection.get(url)
142
- raise_error!(response)
143
- JSON.parse(response.body)
130
+ get_json(url)
144
131
  end
145
132
 
146
133
  def get_stats(project_slug, resource_slug)
147
134
  url = "#{API_ROOT}/project/#{project_slug}/resource/#{resource_slug}/stats/"
148
- response = connection.get(url)
149
- raise_error!(response)
150
- JSON.parse(response.body)
135
+ get_json(url)
151
136
  end
152
137
 
153
138
  private
154
139
 
140
+ def get(url)
141
+ act(:get, url)
142
+ end
143
+
144
+ def post(url, body)
145
+ act(:post, url, body)
146
+ end
147
+
148
+ def put(url, body)
149
+ act(:put, url, body)
150
+ end
151
+
152
+ def delete(url)
153
+ act(:delete, url)
154
+ end
155
+
156
+ def get_json(url)
157
+ response = get(url)
158
+ JSON.parse(response.body)
159
+ end
160
+
161
+ def act(verb, url, body = nil)
162
+ response = connection.send(verb, url, body)
163
+ raise_error!(response)
164
+ response
165
+ end
166
+
155
167
  def get_content_io(tx_resource, content)
156
168
  content_io = StringIO::new(content)
157
169
  content_io.set_encoding(Encoding::UTF_8.name)
@@ -163,16 +175,15 @@ module Txgh
163
175
  def raise_error!(response)
164
176
  case response.status
165
177
  when 401
166
- raise TransifexUnauthorizedError
178
+ raise TransifexUnauthorizedError, "401 Unauthorized: #{response.env.url}"
167
179
  when 404
168
- raise TransifexNotFoundError
180
+ raise TransifexNotFoundError, "404 Not Found: #{response.env.url}"
169
181
  else
170
182
  if (response.status / 100) != 2
171
183
  raise TransifexApiError,
172
- "Failed Transifex API call - returned status code: #{response.status}, body: #{response.body}"
184
+ "HTTP #{response.status}: #{response.env.url}, body: #{response.body}"
173
185
  end
174
186
  end
175
187
  end
176
188
  end
177
189
  end
178
-
data/lib/txgh/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Txgh
2
- VERSION = '3.0.0'
2
+ VERSION = '4.0.0'
3
3
  end
@@ -39,7 +39,7 @@ describe DeleteHandler do
39
39
  receive(:get_resources).and_return(resource_api_response)
40
40
  )
41
41
 
42
- expect(transifex_api).to receive(:delete) do |tx_resource|
42
+ expect(transifex_api).to receive(:delete_resource) do |tx_resource|
43
43
  expect(tx_resource.project_slug).to eq(project_name)
44
44
  expect(tx_resource.resource_slug).to eq(resource_slug_with_branch)
45
45
  end
@@ -4,12 +4,37 @@ require 'helpers/standard_txgh_setup'
4
4
  include Txgh
5
5
 
6
6
  describe TransifexApi do
7
+ FakeEnv = Struct.new(:url)
8
+ FakeRequest = Struct.new(:body)
9
+ FakeResponse = Struct.new(:env, :status, :body)
10
+
11
+ class FakeConnection
12
+ %w(get post put delete).each do |verb|
13
+ define_method(:"on_#{verb}") do |&block|
14
+ callbacks[verb] = block
15
+ end
16
+
17
+ define_method(verb) do |url, body = nil|
18
+ env = FakeEnv.new(url)
19
+ request = FakeRequest.new(body)
20
+ response = FakeResponse.new(env)
21
+ callbacks[verb].call(request, response)
22
+ response
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def callbacks
29
+ @callbacks ||= {}
30
+ end
31
+ end
32
+
7
33
  include StandardTxghSetup
8
34
 
9
- let(:connection) { double(:connection) }
35
+ let(:connection) { FakeConnection.new }
10
36
  let(:api) { TransifexApi.create_from_connection(connection) }
11
37
  let(:resource) { tx_config.resource(resource_slug) }
12
- let(:response) { double(:response) }
13
38
 
14
39
  describe '#create_or_update' do
15
40
  context 'with a preexisting resource' do
@@ -48,159 +73,188 @@ describe TransifexApi do
48
73
  context 'with a non-existent resource' do
49
74
  before(:each) do
50
75
  expect(api).to receive(:resource_exists?).and_return(false)
76
+ expect(connection).to receive(:post).and_call_original
51
77
  end
52
78
 
53
79
  it 'makes a request with the correct parameters' do
54
- expect(connection).to receive(:post) do |url, payload|
55
- expect(url).to(
80
+ connection.on_post do |request, response|
81
+ response.status = 200
82
+ response.body = '{}'
83
+
84
+ expect(response.env.url).to(
56
85
  end_with("project/#{project_name}/resources/")
57
86
  )
58
87
 
59
- expect(payload[:slug]).to eq(resource_slug)
60
- expect(payload[:name]).to eq(resource.source_file)
61
- expect(payload[:i18n_type]).to eq('YML')
62
-
63
- response
88
+ expect(request.body[:slug]).to eq(resource_slug)
89
+ expect(request.body[:name]).to eq(resource.source_file)
90
+ expect(request.body[:i18n_type]).to eq('YML')
64
91
  end
65
92
 
66
- allow(response).to receive(:status).and_return(200)
67
- allow(response).to receive(:body).and_return("{}")
68
93
  api.create_or_update(resource, 'new_content')
69
94
  end
70
95
  end
71
96
  end
72
97
 
73
98
  describe '#create' do
99
+ before(:each) do
100
+ expect(connection).to receive(:post).and_call_original
101
+ end
102
+
74
103
  it 'makes a request with the correct parameters' do
75
- expect(connection).to receive(:post) do |url, payload|
76
- expect(url).to(
104
+ connection.on_post do |request, response|
105
+ response.status = 200
106
+
107
+ expect(response.env.url).to(
77
108
  end_with("project/#{project_name}/resources/")
78
109
  )
79
110
 
80
- expect(payload[:name]).to eq('sample.yml')
81
- expect(payload[:content].io.string).to eq('new_content')
82
- expect(payload[:categories]).to eq('abc def')
83
- response
111
+ expect(request.body[:name]).to eq('sample.yml')
112
+ expect(request.body[:content].io.string).to eq('new_content')
113
+ expect(request.body[:categories]).to eq('abc def')
84
114
  end
85
115
 
86
- allow(response).to receive(:status).and_return(200)
87
116
  api.create(resource, 'new_content', ['abc', 'def'])
88
117
  end
89
118
 
90
119
  it 'submits de-duped categories' do
91
- expect(connection).to receive(:post) do |url, payload|
92
- expect(payload[:categories]).to eq('abc')
93
- response
120
+ connection.on_post do |request, response|
121
+ response.status = 200
122
+ expect(request.body[:categories]).to eq('abc')
94
123
  end
95
124
 
96
- allow(response).to receive(:status).and_return(200)
97
125
  api.create(resource, 'new_content', ['abc', 'abc'])
98
126
  end
99
127
 
100
128
  it 'raises an exception if the api responds with an error code' do
101
- allow(connection).to receive(:post).and_return(response)
102
- allow(response).to receive(:status).and_return(404)
103
- allow(response).to receive(:body).and_return('{}')
104
- expect { api.create(resource, 'new_content') }.to raise_error(TransifexApiError)
129
+ connection.on_post do |_, response|
130
+ response.status = 404
131
+ response.body = '{}'
132
+ end
133
+
134
+ expect { api.create(resource, 'new_content') }.to(
135
+ raise_error(TransifexApiError)
136
+ )
105
137
  end
106
138
 
107
139
  context 'with a branch-based resource' do
108
140
  let(:resource) { tx_config.resource(resource_slug, ref) }
109
141
 
110
142
  it "includes the branch in the resource's name" do
111
- expect(connection).to receive(:post) do |url, payload|
112
- expect(payload[:name]).to eq('sample.yml (heads/master)')
113
- response
143
+ connection.on_post do |request, response|
144
+ response.status = 200
145
+ expect(request.body[:name]).to eq('sample.yml (heads/master)')
114
146
  end
115
147
 
116
- allow(response).to receive(:status).and_return(200)
117
148
  api.create(resource, 'new_content')
118
149
  end
119
150
  end
120
151
  end
121
152
 
122
- describe '#delete' do
153
+ describe '#delete_resource' do
154
+ before(:each) do
155
+ expect(connection).to receive(:delete).and_call_original
156
+ end
157
+
123
158
  it 'deletes the given resource' do
124
- expect(connection).to receive(:delete) do |url|
125
- expect(url).to(
159
+ connection.on_delete do |_, response|
160
+ response.status = 200
161
+
162
+ expect(response.env.url).to(
126
163
  end_with("project/#{project_name}/resource/#{resource_slug}/")
127
164
  )
128
165
  end
129
166
 
130
- api.delete(resource)
167
+ api.delete_resource(resource)
131
168
  end
132
169
  end
133
170
 
134
171
  describe '#update_content' do
172
+ before(:each) do
173
+ expect(connection).to receive(:put).and_call_original
174
+ end
175
+
135
176
  it 'makes a request with the correct parameters' do
136
- expect(connection).to receive(:put) do |url, payload|
137
- expect(url).to(
177
+ connection.on_put do |request, response|
178
+ response.status = 200
179
+
180
+ expect(response.env.url).to(
138
181
  end_with("project/#{project_name}/resource/#{resource_slug}/content/")
139
182
  )
140
183
 
141
- expect(payload[:content].io.string).to eq('new_content')
142
- response
184
+ expect(request.body[:content].io.string).to eq('new_content')
143
185
  end
144
186
 
145
- allow(response).to receive(:status).and_return(200)
146
187
  api.update_content(resource, 'new_content')
147
188
  end
148
189
 
149
190
  it 'raises an exception if the api responds with an error code' do
150
- allow(connection).to receive(:put).and_return(response)
151
- allow(response).to receive(:status).and_return(404)
152
- allow(response).to receive(:body).and_return('{}')
153
- expect { api.update_content(resource, 'new_content') }.to raise_error(TransifexApiError)
191
+ connection.on_put do |_, response|
192
+ response.status = 404
193
+ response.body = '{}'
194
+ end
195
+
196
+ expect { api.update_content(resource, 'new_content') }.to(
197
+ raise_error(TransifexApiError)
198
+ )
154
199
  end
155
200
  end
156
201
 
157
202
  describe '#update_details' do
203
+ before(:each) do
204
+ expect(connection).to receive(:put).and_call_original
205
+ end
206
+
158
207
  it 'makes a request with the correct parameters' do
159
- expect(connection).to receive(:put) do |url, payload|
160
- expect(url).to(
208
+ connection.on_put do |request, response|
209
+ response.status = 200
210
+
211
+ expect(response.env.url).to(
161
212
  end_with("project/#{project_name}/resource/#{resource_slug}/")
162
213
  )
163
214
 
164
- expect(payload[:i18n_type]).to eq('FOO')
165
- expect(payload[:categories]).to eq(['abc'])
166
- response
215
+ expect(request.body[:i18n_type]).to eq('FOO')
216
+ expect(request.body[:categories]).to eq(['abc'])
167
217
  end
168
218
 
169
- allow(response).to receive(:status).and_return(200)
170
219
  api.update_details(resource, i18n_type: 'FOO', categories: ['abc'])
171
220
  end
172
221
 
173
222
  it 'raises an exception if the api responds with an error code' do
174
- allow(connection).to receive(:put).and_return(response)
175
- allow(response).to receive(:status).and_return(404)
176
- allow(response).to receive(:body).and_return('{}')
177
- expect { api.update_details(resource, {}) }.to raise_error(TransifexApiError)
223
+ connection.on_put do |_, response|
224
+ response.status = 404
225
+ response.body = '{}'
226
+ end
227
+
228
+ expect { api.update_details(resource, {}) }.to(
229
+ raise_error(TransifexApiError)
230
+ )
178
231
  end
179
232
  end
180
233
 
181
234
  describe '#resource_exists?' do
235
+ before(:each) do
236
+ expect(connection).to receive(:get).and_call_original
237
+ end
238
+
182
239
  it 'makes a request with the correct parameters' do
183
- expect(connection).to receive(:get) do |url|
184
- expect(url).to(
240
+ connection.on_get do |_, response|
241
+ response.status = 200
242
+
243
+ expect(response.env.url).to(
185
244
  end_with("project/#{project_name}/resource/#{resource_slug}/")
186
245
  )
187
-
188
- response
189
246
  end
190
247
 
191
- allow(response).to receive(:status).and_return(200)
192
248
  api.resource_exists?(resource)
193
249
  end
194
250
 
195
251
  it 'returns true if the api responds with a 200 status code' do
196
- allow(connection).to receive(:get).and_return(response)
197
- allow(response).to receive(:status).and_return(200)
252
+ connection.on_get { |_, response| response.status = 200 }
198
253
  expect(api.resource_exists?(resource)).to eq(true)
199
254
  end
200
255
 
201
256
  it 'returns false if the api does not respond with a 200 status code' do
202
- allow(connection).to receive(:get).and_return(response)
203
- allow(response).to receive(:status).and_return(404)
257
+ connection.on_get { |_, response| response.status = 404 }
204
258
  expect(api.resource_exists?(resource)).to eq(false)
205
259
  end
206
260
  end
@@ -208,38 +262,40 @@ describe TransifexApi do
208
262
  describe '#download' do
209
263
  let(:language) { 'pt-BR' }
210
264
 
265
+ before(:each) do
266
+ expect(connection).to receive(:get).and_call_original
267
+ end
268
+
211
269
  it 'makes a request with the correct parameters' do
212
- expect(connection).to receive(:get) do |url|
213
- expect(url).to(
270
+ connection.on_get do |_, response|
271
+ response.status = 200
272
+ response.body = '{"content": "foobar"}'
273
+
274
+ expect(response.env.url).to(
214
275
  end_with("project/#{project_name}/resource/#{resource_slug}/translation/#{language}/")
215
276
  )
216
-
217
- response
218
277
  end
219
278
 
220
- allow(response).to receive(:status).and_return(200)
221
- allow(response).to receive(:body).and_return('{}')
222
- api.download(resource, language)
223
- end
224
-
225
- it 'parses and returns the response content' do
226
- allow(connection).to receive(:get).and_return(response)
227
- allow(response).to receive(:status).and_return(200)
228
- allow(response).to receive(:body).and_return('{"content": "foobar"}')
229
279
  expect(api.download(resource, language)).to eq('foobar')
230
280
  end
231
281
 
232
282
  it 'raises an exception if the api responds with an error code' do
233
- allow(connection).to receive(:get).and_return(response)
234
- allow(response).to receive(:status).and_return(401)
235
- allow(response).to receive(:body).and_return('{}')
236
- expect { api.download(resource, language) }.to raise_error(TransifexApiError)
283
+ connection.on_get do |_, response|
284
+ response.status = 401
285
+ response.body = '{}'
286
+ end
287
+
288
+ expect { api.download(resource, language) }.to(
289
+ raise_error(TransifexApiError)
290
+ )
237
291
  end
238
292
 
239
293
  it 'raises a specific exception if the api responds with a 404 not found' do
240
- allow(connection).to receive(:get).and_return(response)
241
- allow(response).to receive(:status).and_return(404)
242
- allow(response).to receive(:body).and_return('{}')
294
+ connection.on_get do |_, response|
295
+ response.status = 404
296
+ response.body = '{}'
297
+ end
298
+
243
299
  expect { api.download(resource, language) }.to raise_error(
244
300
  TransifexNotFoundError
245
301
  )
@@ -247,136 +303,218 @@ describe TransifexApi do
247
303
  end
248
304
 
249
305
  describe '#get_resource' do
306
+ before(:each) do
307
+ expect(connection).to receive(:get).and_call_original
308
+ end
309
+
250
310
  it 'makes a request with the correct parameters' do
251
- expect(connection).to receive(:get) do |url, payload|
252
- expect(url).to(
311
+ connection.on_get do |_, response|
312
+ response.status = 200
313
+ response.body = '{"foo":"bar"}'
314
+
315
+ expect(response.env.url).to(
253
316
  end_with("project/#{project_name}/resource/#{resource_slug}/")
254
317
  )
255
-
256
- response
257
318
  end
258
319
 
259
- allow(response).to receive(:status).and_return(200)
260
- allow(response).to receive(:body).and_return('{"foo":"bar"}')
261
320
  expect(api.get_resource(*resource.slugs)).to eq({ 'foo' => 'bar' })
262
321
  end
263
322
 
264
323
  it 'raises an exception if the api responds with an error code' do
265
- allow(connection).to receive(:get).and_return(response)
266
- allow(response).to receive(:status).and_return(404)
267
- allow(response).to receive(:body).and_return('{}')
268
- expect { api.get_resource(*resource.slugs) }.to raise_error(TransifexApiError)
324
+ connection.on_get do |_, response|
325
+ response.status = 404
326
+ response.body = '{}'
327
+ end
328
+
329
+ expect { api.get_resource(*resource.slugs) }.to(
330
+ raise_error(TransifexApiError)
331
+ )
269
332
  end
270
333
  end
271
334
 
272
335
  describe '#get_resources' do
336
+ before(:each) do
337
+ expect(connection).to receive(:get).and_call_original
338
+ end
339
+
273
340
  it 'makes a request with the correct parameters' do
274
- expect(connection).to receive(:get) do |url, payload|
275
- expect(url).to(
341
+ connection.on_get do |_, response|
342
+ response.status = 200
343
+ response.body = '{"foo":"bar"}'
344
+
345
+ expect(response.env.url).to(
276
346
  end_with("project/#{project_name}/resources/")
277
347
  )
278
-
279
- response
280
348
  end
281
349
 
282
- allow(response).to receive(:status).and_return(200)
283
- allow(response).to receive(:body).and_return('{"foo":"bar"}')
284
350
  expect(api.get_resources(project_name)).to eq({ 'foo' => 'bar' })
285
351
  end
286
352
 
287
353
  it 'raises an exception if the api responds with an error code' do
288
- allow(connection).to receive(:get).and_return(response)
289
- allow(response).to receive(:status).and_return(404)
290
- allow(response).to receive(:body).and_return('{}')
291
- expect { api.get_resources(project_name) }.to raise_error(TransifexApiError)
354
+ connection.on_get do |_, response|
355
+ response.status = 404
356
+ response.body = '{}'
357
+ end
358
+
359
+ expect { api.get_resources(project_name) }.to(
360
+ raise_error(TransifexApiError)
361
+ )
292
362
  end
293
363
  end
294
364
 
295
365
  describe '#get_languages' do
366
+ before(:each) do
367
+ expect(connection).to receive(:get).and_call_original
368
+ end
369
+
296
370
  it 'makes a request with the correct parameters' do
297
- expect(connection).to receive(:get) do |url, payload|
298
- expect(url).to(
371
+ connection.on_get do |_, response|
372
+ response.status = 200
373
+ response.body = '[{"language_code":"de"}]'
374
+
375
+ expect(response.env.url).to(
299
376
  end_with("project/#{project_name}/languages/")
300
377
  )
301
-
302
- response
303
378
  end
304
379
 
305
- allow(response).to receive(:status).and_return(200)
306
- allow(response).to receive(:body).and_return('[{"language_code":"de"}]')
307
380
  expect(api.get_languages(project_name)).to eq([{ 'language_code' => 'de' }])
308
381
  end
309
382
 
310
383
  it 'raises an exception if the api responds with an error code' do
311
- allow(connection).to receive(:get).and_return(response)
312
- allow(response).to receive(:status).and_return(404)
313
- allow(response).to receive(:body).and_return('{}')
314
- expect { api.get_languages(project_name) }.to raise_error(TransifexApiError)
384
+ connection.on_get do |_, response|
385
+ response.status = 404
386
+ response.body = '{}'
387
+ end
388
+
389
+ expect { api.get_languages(project_name) }.to(
390
+ raise_error(TransifexApiError)
391
+ )
315
392
  end
316
393
  end
317
394
 
318
395
  describe '#get_project' do
396
+ before(:each) do
397
+ expect(connection).to receive(:get).and_call_original
398
+ end
399
+
319
400
  it 'makes a request with the correct parameters' do
320
- expect(connection).to receive(:get) do |url, payload|
321
- expect(url).to(
401
+ connection.on_get do |_, response|
402
+ response.status = 200
403
+ response.body = '{"slug":"projectslug"}'
404
+
405
+ expect(response.env.url).to(
322
406
  end_with("project/#{project_name}/")
323
407
  )
324
-
325
- response
326
408
  end
327
409
 
328
- allow(response).to receive(:status).and_return(200)
329
- allow(response).to receive(:body).and_return('{"slug":"projectslug"}')
330
410
  expect(api.get_project(project_name)).to eq({ 'slug' => 'projectslug' })
331
411
  end
332
412
 
333
413
  it 'raises an exception if the api responds with an error code' do
334
- allow(connection).to receive(:get).and_return(response)
335
- allow(response).to receive(:status).and_return(404)
336
- allow(response).to receive(:body).and_return('{}')
337
- expect { api.get_project(project_name) }.to raise_error(TransifexApiError)
414
+ connection.on_get do |_, response|
415
+ response.status = 404
416
+ response.body = '{}'
417
+ end
418
+
419
+ expect { api.get_project(project_name) }.to(
420
+ raise_error(TransifexApiError)
421
+ )
338
422
  end
339
423
  end
340
424
 
341
425
  describe '#get_formats' do
426
+ before(:each) do
427
+ expect(connection).to receive(:get).and_call_original
428
+ end
429
+
342
430
  it 'makes a request with the correct parameters' do
343
- expect(connection).to receive(:get) do |url, payload|
344
- expect(url).to end_with("formats/")
345
- response
431
+ connection.on_get do |_, response|
432
+ response.status = 200
433
+ response.body = '{}'
434
+
435
+ expect(response.env.url).to(
436
+ end_with('formats/')
437
+ )
346
438
  end
347
439
 
348
- allow(response).to receive(:status).and_return(200)
349
- allow(response).to receive(:body).and_return('{}')
350
440
  expect(api.get_formats).to eq({})
351
441
  end
352
442
 
353
443
  it 'raises an exception if the api responds with an error code' do
354
- allow(connection).to receive(:get).and_return(response)
355
- allow(response).to receive(:status).and_return(404)
356
- allow(response).to receive(:body).and_return('{}')
444
+ connection.on_get do |_, response|
445
+ response.status = 404
446
+ response.body = '{}'
447
+ end
448
+
357
449
  expect { api.get_formats }.to raise_error(TransifexApiError)
358
450
  end
359
451
  end
360
452
 
361
453
  describe '#get_stats' do
454
+ before(:each) do
455
+ expect(connection).to receive(:get).and_call_original
456
+ end
457
+
362
458
  it 'makes a request with the correct parameters' do
363
- expect(connection).to receive(:get) do |url, payload|
364
- expect(url).to end_with("stats/")
365
- response
459
+ connection.on_get do |_, response|
460
+ response.status = 200
461
+ response.body = '{}'
462
+
463
+ expect(response.env.url).to(
464
+ end_with('stats/')
465
+ )
366
466
  end
367
467
 
368
- allow(response).to receive(:status).and_return(200)
369
- allow(response).to receive(:body).and_return('{}')
370
468
  expect(api.get_stats(project_name, resource_slug)).to eq({})
371
469
  end
372
470
 
373
471
  it 'raises an exception if the api responds with an error code' do
374
- allow(connection).to receive(:get).and_return(response)
375
- allow(response).to receive(:status).and_return(404)
376
- allow(response).to receive(:body).and_return('{}')
472
+ connection.on_get do |_, response|
473
+ response.status = 404
474
+ response.body = '{}'
475
+ end
476
+
377
477
  expect { api.get_stats(project_name, resource_slug) }.to(
378
478
  raise_error(TransifexApiError)
379
479
  )
380
480
  end
381
481
  end
482
+
483
+ describe 'errors' do
484
+ it 'includes the URL in the error message on 401' do
485
+ connection.on_get do |_, response|
486
+ response.status = 401
487
+ response.body = '{}'
488
+ end
489
+
490
+ expect { api.get_formats }.to raise_error do |error|
491
+ expect(error).to be_a(TransifexUnauthorizedError)
492
+ expect(error.message).to eq('401 Unauthorized: /api/2/formats/')
493
+ end
494
+ end
495
+
496
+ it 'includes the URL in the error message on 404' do
497
+ connection.on_get do |_, response|
498
+ response.status = 404
499
+ response.body = '{}'
500
+ end
501
+
502
+ expect { api.get_formats }.to raise_error do |error|
503
+ expect(error).to be_a(TransifexNotFoundError)
504
+ expect(error.message).to eq('404 Not Found: /api/2/formats/')
505
+ end
506
+ end
507
+
508
+ it 'includes the URL in the error message on unexpected error' do
509
+ connection.on_get do |_, response|
510
+ response.status = 422
511
+ response.body = '{}'
512
+ end
513
+
514
+ expect { api.get_formats }.to raise_error do |error|
515
+ expect(error).to be_a(TransifexApiError)
516
+ expect(error.message).to eq('HTTP 422: /api/2/formats/, body: {}')
517
+ end
518
+ end
519
+ end
382
520
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: txgh
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.0
4
+ version: 4.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Jackowski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-05-23 00:00:00.000000000 Z
12
+ date: 2016-05-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: abroad