bulldog 0.0.2 → 0.0.3
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.
- data/CHANGELOG +7 -2
- data/VERSION +1 -1
- data/bulldog.gemspec +1 -1
- data/lib/bulldog/attachment/base.rb +14 -2
- data/lib/bulldog/attachment/none.rb +4 -1
- data/lib/bulldog/processor/base.rb +2 -2
- data/lib/bulldog/processor/ffmpeg.rb +3 -2
- data/lib/bulldog/processor/image_magick.rb +5 -3
- data/lib/bulldog/processor/one_shot.rb +2 -2
- data/spec/unit/attachment/base_spec.rb +59 -0
- data/spec/unit/has_attachment_spec.rb +7 -8
- data/spec/unit/processor/ffmpeg_spec.rb +15 -1
- data/spec/unit/processor/image_magick_spec.rb +14 -0
- metadata +1 -1
data/CHANGELOG
CHANGED
@@ -1,6 +1,11 @@
|
|
1
|
-
== 0.0.
|
1
|
+
== 0.0.3 2009-11-16
|
2
|
+
|
3
|
+
* Add a way to respond to processing errors:
|
4
|
+
- #process returns true/false.
|
5
|
+
- #process! raises a AR::InvalidRecord if any errors.
|
6
|
+
- ...just like ActiveRecord::Base::save.
|
2
7
|
|
3
|
-
|
8
|
+
== 0.0.2 2009-11-16
|
4
9
|
|
5
10
|
* #process_once was stopping other processors from running.
|
6
11
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.3
|
data/bulldog.gemspec
CHANGED
@@ -21,7 +21,9 @@ module Bulldog
|
|
21
21
|
#
|
22
22
|
# Run the processors for the named event.
|
23
23
|
#
|
24
|
-
|
24
|
+
# Return true if no errors were encountered, false otherwise.
|
25
|
+
#
|
26
|
+
def process(event_name)
|
25
27
|
reflection.events[event_name].each do |event|
|
26
28
|
if (types = event.attachment_types)
|
27
29
|
next unless types.include?(type)
|
@@ -29,8 +31,18 @@ module Bulldog
|
|
29
31
|
processor_type = event.processor_type || default_processor_type
|
30
32
|
processor_class = Processor.const_get(processor_type.to_s.camelize)
|
31
33
|
processor = processor_class.new(self, styles_for_event(event), stream.path)
|
32
|
-
processor.process(
|
34
|
+
processor.process(&event.callback)
|
33
35
|
end
|
36
|
+
record.errors.empty?
|
37
|
+
end
|
38
|
+
|
39
|
+
#
|
40
|
+
# Like #process, but raise ActiveRecord::RecordInvalid if there
|
41
|
+
# are any errors.
|
42
|
+
#
|
43
|
+
def process!(event_name, &block)
|
44
|
+
process(event_name, &block) or
|
45
|
+
raise ActiveRecord::RecordInvalid, record
|
34
46
|
end
|
35
47
|
|
36
48
|
#
|
@@ -61,12 +61,12 @@ module Bulldog
|
|
61
61
|
# #style will be set to the current style each time the block is
|
62
62
|
# called.
|
63
63
|
#
|
64
|
-
def process(
|
64
|
+
def process(&block)
|
65
65
|
return if styles.empty?
|
66
66
|
styles.each do |style|
|
67
67
|
@style = style
|
68
68
|
begin
|
69
|
-
process_style(
|
69
|
+
process_style(&block)
|
70
70
|
ensure
|
71
71
|
@style = nil
|
72
72
|
end
|
@@ -14,7 +14,7 @@ module Bulldog
|
|
14
14
|
@still_frame_callbacks = style_list_map
|
15
15
|
end
|
16
16
|
|
17
|
-
def process
|
17
|
+
def process
|
18
18
|
return if styles.empty?
|
19
19
|
super
|
20
20
|
run_ffmpeg
|
@@ -158,7 +158,8 @@ module Bulldog
|
|
158
158
|
command = [self.class.ffmpeg_command]
|
159
159
|
command << '-i' << input_file
|
160
160
|
command.concat(arguments)
|
161
|
-
Bulldog.run(*command)
|
161
|
+
Bulldog.run(*command) or
|
162
|
+
record.errors.add name, "convert failed (status #$?)"
|
162
163
|
end
|
163
164
|
end
|
164
165
|
|
@@ -16,7 +16,7 @@ module Bulldog
|
|
16
16
|
@tree = ArgumentTree.new(styles)
|
17
17
|
end
|
18
18
|
|
19
|
-
def process
|
19
|
+
def process
|
20
20
|
return if styles.empty?
|
21
21
|
super
|
22
22
|
run_convert
|
@@ -105,7 +105,7 @@ module Bulldog
|
|
105
105
|
def run_convert
|
106
106
|
add_final_style_arguments
|
107
107
|
output = run_convert_command and
|
108
|
-
|
108
|
+
run_convert_callbacks(output)
|
109
109
|
end
|
110
110
|
|
111
111
|
def add_final_style_arguments
|
@@ -120,7 +120,9 @@ module Bulldog
|
|
120
120
|
|
121
121
|
def run_convert_command
|
122
122
|
command = [self.class.convert_command, "#{input_file}[0]", *@tree.arguments].flatten
|
123
|
-
Bulldog.run(*command)
|
123
|
+
output = Bulldog.run(*command) or
|
124
|
+
record.errors.add name, "convert failed (status #$?)"
|
125
|
+
output
|
124
126
|
end
|
125
127
|
|
126
128
|
def run_convert_callbacks(output)
|
@@ -219,6 +219,17 @@ describe Attachment::Base do
|
|
219
219
|
end
|
220
220
|
end
|
221
221
|
|
222
|
+
def with_test_processor(options, &block)
|
223
|
+
test_processor_class = Class.new(Processor::Base) do
|
224
|
+
define_method :process do
|
225
|
+
if options[:error]
|
226
|
+
record.errors.add name, "error"
|
227
|
+
end
|
228
|
+
end
|
229
|
+
end
|
230
|
+
with_temporary_constant_value(Processor, :Test, test_processor_class, &block)
|
231
|
+
end
|
232
|
+
|
222
233
|
describe "#process" do
|
223
234
|
use_model_class(:Thing)
|
224
235
|
|
@@ -280,6 +291,54 @@ describe Attachment::Base do
|
|
280
291
|
styles.should be_a(StyleSet)
|
281
292
|
styles.map(&:name).should == [:small]
|
282
293
|
end
|
294
|
+
|
295
|
+
it "should return true if no errors were encountered" do
|
296
|
+
with_test_processor(:error => false) do
|
297
|
+
Thing.has_attachment :attachment do
|
298
|
+
style :one
|
299
|
+
process(:on => :event, :with => :test){}
|
300
|
+
end
|
301
|
+
thing = Thing.new(:attachment => test_empty_file)
|
302
|
+
thing.attachment.process(:event).should be_true
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
it "should return false if an error was encountered" do
|
307
|
+
with_test_processor(:error => true) do
|
308
|
+
Thing.has_attachment :attachment do
|
309
|
+
style :one
|
310
|
+
process(:on => :event, :with => :test){}
|
311
|
+
end
|
312
|
+
thing = Thing.new(:attachment => test_empty_file)
|
313
|
+
thing.attachment.process(:event).should be_false
|
314
|
+
end
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
318
|
+
describe "#process!" do
|
319
|
+
use_model_class(:Thing)
|
320
|
+
|
321
|
+
it "should raise ActiveRecord::RecordInvalid if there are any errors present" do
|
322
|
+
with_test_processor(:error => true) do
|
323
|
+
Thing.has_attachment :attachment do
|
324
|
+
style :one
|
325
|
+
process :on => :event, :with => :test
|
326
|
+
end
|
327
|
+
thing = Thing.new(:attachment => test_empty_file)
|
328
|
+
lambda{thing.attachment.process!(:event)}.should raise_error(ActiveRecord::RecordInvalid)
|
329
|
+
end
|
330
|
+
end
|
331
|
+
|
332
|
+
it "should not raise ActiveRecord::RecordInvalid if there are no errors present" do
|
333
|
+
with_test_processor(:error => false) do
|
334
|
+
Thing.has_attachment :attachment do
|
335
|
+
style :one
|
336
|
+
process :on => :event, :with => :test
|
337
|
+
end
|
338
|
+
thing = Thing.new(:attachment => test_empty_file)
|
339
|
+
lambda{thing.attachment.process!(:event)}.should_not raise_error(ActiveRecord::RecordInvalid)
|
340
|
+
end
|
341
|
+
end
|
283
342
|
end
|
284
343
|
|
285
344
|
describe "storable attributes" do
|
@@ -66,14 +66,14 @@ describe HasAttachment do
|
|
66
66
|
|
67
67
|
describe "when there is an attachment set" do
|
68
68
|
it "should trigger the configured callbacks" do
|
69
|
-
|
69
|
+
calls = []
|
70
70
|
Thing.has_attachment :photo do
|
71
71
|
style :normal
|
72
|
-
process(:on => :my_event){
|
72
|
+
process(:on => :my_event){calls << 1}
|
73
73
|
end
|
74
74
|
thing = Thing.new(:photo => uploaded_file)
|
75
|
-
thing.process_attachment(:photo, :my_event
|
76
|
-
|
75
|
+
thing.process_attachment(:photo, :my_event)
|
76
|
+
calls.should == [1]
|
77
77
|
end
|
78
78
|
end
|
79
79
|
|
@@ -85,20 +85,19 @@ describe HasAttachment do
|
|
85
85
|
process(:on => :my_event){|*args|}
|
86
86
|
end
|
87
87
|
thing = Thing.new(:photo => nil)
|
88
|
-
thing.process_attachment(:photo, :my_event
|
88
|
+
thing.process_attachment(:photo, :my_event)
|
89
89
|
args.should be_nil
|
90
90
|
end
|
91
91
|
end
|
92
92
|
|
93
93
|
it "should raise an ArgumentError if the attachment name is invalid" do
|
94
|
-
args = nil
|
95
94
|
Thing.has_attachment :photo do
|
96
95
|
style :normal
|
97
|
-
process(:on => :my_event){
|
96
|
+
process(:on => :my_event){}
|
98
97
|
end
|
99
98
|
thing = Thing.new
|
100
99
|
lambda do
|
101
|
-
thing.process_attachment(:fail, :my_event
|
100
|
+
thing.process_attachment(:fail, :my_event)
|
102
101
|
end.should raise_error(ArgumentError)
|
103
102
|
end
|
104
103
|
|
@@ -54,7 +54,7 @@ describe Processor::Ffmpeg do
|
|
54
54
|
@thing.video.process(:event)
|
55
55
|
end
|
56
56
|
|
57
|
-
describe "
|
57
|
+
describe "#process" do
|
58
58
|
before do
|
59
59
|
video_style :output
|
60
60
|
end
|
@@ -72,6 +72,20 @@ describe Processor::Ffmpeg do
|
|
72
72
|
end
|
73
73
|
File.read(log_path).should include("[Bulldog] Running: #{ffmpeg}")
|
74
74
|
end
|
75
|
+
|
76
|
+
it "should add an error to the record if convert fails" do
|
77
|
+
video_style :output
|
78
|
+
Bulldog.stubs(:run).returns(nil)
|
79
|
+
process_video
|
80
|
+
@thing.errors.should be_present
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should not add an error to the record if convert succeeds" do
|
84
|
+
video_style :output
|
85
|
+
Bulldog.stubs(:run).returns('')
|
86
|
+
process_video
|
87
|
+
@thing.errors.should_not be_present
|
88
|
+
end
|
75
89
|
end
|
76
90
|
|
77
91
|
describe "#encode" do
|
@@ -78,6 +78,20 @@ describe Processor::ImageMagick do
|
|
78
78
|
process
|
79
79
|
end
|
80
80
|
|
81
|
+
it "should add an error to the record if convert fails" do
|
82
|
+
style :output
|
83
|
+
Bulldog.stubs(:run).returns(nil)
|
84
|
+
process
|
85
|
+
@thing.errors.should be_present
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should not add an error to the record if convert succeeds" do
|
89
|
+
style :output
|
90
|
+
Bulldog.stubs(:run).returns('')
|
91
|
+
process
|
92
|
+
@thing.errors.should_not be_present
|
93
|
+
end
|
94
|
+
|
81
95
|
it "should use the given :quality style attribute" do
|
82
96
|
style :output, :quality => 50
|
83
97
|
Bulldog.expects(:run).once.with(convert, "#{original_path}[0]", '-quality', '50', output_path).returns('')
|