Skip to content

Commit 88dc744

Browse files
committed
Fix Array<Hash> support for steps
1 parent d8b81c7 commit 88dc744

2 files changed

Lines changed: 90 additions & 2 deletions

File tree

lib/transloadit/assembly.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,22 @@ def _wrap_steps_in_hash(steps)
143143
when nil then steps
144144
when Hash then steps
145145
when Transloadit::Step then steps.to_hash
146-
else
147-
if steps.uniq(&:name) != steps
146+
when Array
147+
names = steps.inject([]) do |array, step|
148+
case step
149+
when Transloadit::Step then array << step.name
150+
when Hash then array + step.keys.map(&:to_s)
151+
else
152+
raise ArgumentError, "Unsupported array element"
153+
end
154+
end
155+
156+
if names.uniq != names
148157
raise ArgumentError, "There are different Assembly steps using the same name"
149158
end
150159
steps.inject({}) { |h, s| h.update s }
160+
else
161+
raise ArgumentError, "Unsupported steps class"
151162
end
152163
end
153164

test/unit/transloadit/test_assembly.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,83 @@
211211
end
212212
end
213213

214+
describe "with multiple hash steps" do
215+
it "must wrap its steps into one hash" do
216+
hash_1 = {encode: {robot: "/video/encode"}, resize: {robot: "/image/resize"}}
217+
hash_2 = {thumbs: {robot: "/video/thumbs"}}
218+
219+
assembly = Transloadit::Assembly.new @transloadit,
220+
steps: [hash_1, hash_2]
221+
(hash_1.keys + hash_2.keys).each do |key|
222+
_(assembly.to_hash[:steps].keys).must_include key
223+
end
224+
end
225+
226+
it "must not allow duplicate steps" do
227+
thumbs = {thumbs: {robot: "/video/thumbs"}}
228+
thumbs_duplicate = {thumbs: {robot: "/video/encode"}}
229+
assembly = Transloadit::Assembly.new @transloadit,
230+
steps: [thumbs, thumbs_duplicate]
231+
assert_raises ArgumentError do
232+
assembly.create! open("lib/transloadit/version.rb")
233+
end
234+
end
235+
end
236+
237+
describe "with mixture of hashes and steps" do
238+
it "must wrap its steps into one hash" do
239+
hash = {encode: {robot: "/video/encode"}, resize: {robot: "/image/resize"}}
240+
step = @transloadit.step "thumbs", "/video/thumbs"
241+
242+
assembly = Transloadit::Assembly.new @transloadit,
243+
steps: [hash, step]
244+
245+
hash.keys.each do |key|
246+
_(assembly.to_hash[:steps].keys).must_include key
247+
end
248+
249+
_(assembly.to_hash[:steps].keys).must_include step.name
250+
end
251+
252+
it "must not allow duplicate steps" do
253+
hash = {thumbs: {robot: "/video/thumbs"}}
254+
step = @transloadit.step "thumbs", "/video/thumbs"
255+
256+
assembly = Transloadit::Assembly.new @transloadit,
257+
steps: [hash, step]
258+
259+
assert_raises ArgumentError do
260+
assembly.create! open("lib/transloadit/version.rb")
261+
end
262+
end
263+
end
264+
265+
describe 'with unsupported step class' do
266+
it "raises error" do
267+
Unsupported = Class.new
268+
269+
assembly = Transloadit::Assembly.new @transloadit,
270+
steps: Unsupported.new
271+
272+
assert_raises ArgumentError do
273+
assembly.create! open("lib/transloadit/version.rb")
274+
end
275+
end
276+
end
277+
278+
describe 'with unsupported array element' do
279+
it "raises error" do
280+
Unsupported = Class.new
281+
282+
assembly = Transloadit::Assembly.new @transloadit,
283+
steps: [Unsupported.new]
284+
285+
assert_raises ArgumentError do
286+
assembly.create! open("lib/transloadit/version.rb")
287+
end
288+
end
289+
end
290+
214291
describe "using assembly API methods" do
215292
include WebMock::API
216293

0 commit comments

Comments
 (0)