Skip to content

Commit cbd03a2

Browse files
authored
Fix Array<Hash> support for steps (#74)
1 parent 9372cf4 commit cbd03a2

2 files changed

Lines changed: 86 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: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,79 @@
213213
end
214214
end
215215

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

0 commit comments

Comments
 (0)