Skip to content

Commit

Permalink
Validating quality
Browse files Browse the repository at this point in the history
  • Loading branch information
knaveofdiamonds committed Dec 8, 2009
1 parent 397aec4 commit 0c48b06
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
8 changes: 7 additions & 1 deletion lib/encoding_dot_com/image_format.rb
Expand Up @@ -13,6 +13,7 @@ def initialize(attributes={})
def validate_attributes
validate_image_format
validate_resize_method
validate_quality
end

def validate_image_format
Expand All @@ -26,6 +27,11 @@ def validate_resize_method
raise IllegalFormatAttribute.new("resize_method should be one of resize, crop or combine.")
end
end


def validate_quality
unless quality.nil? || (quality.to_i >= 1 && quality.to_i <= 100)
raise IllegalFormatAttribute.new("quality should be between 1 and 100.")
end
end
end
end
21 changes: 16 additions & 5 deletions spec/image_format_spec.rb
Expand Up @@ -30,27 +30,38 @@
format_xml("keep_aspect_ratio" => true).should have_xpath("/format/keep_aspect_ratio[text()='yes']")
end

describe "valid attributes" do
describe "valid resize method" do
%w{resize crop combine}.each do |method|
it "should allow '#{method}' as a resize_method" do
it "should allow '#{method}'" do
lambda { format_xml("resize_method" => method) }.should_not raise_error(EncodingDotCom::IllegalFormatAttribute)
end
end

it "should not allow anything else as a resize_method" do
it "should not allow anything else" do
lambda { format_xml("resize_method" => "foo") }.should raise_error(EncodingDotCom::IllegalFormatAttribute)
end
end

describe "valid image format" do
%w{jpg png gif}.each do |format|
it "should allow '#{format}' as an image_format" do
it "should allow '#{format}'" do
lambda { format_xml("image_format" => format) }.should_not raise_error(EncodingDotCom::IllegalFormatAttribute)
end
end

it "should not allow anything else as an image_format" do
it "should not allow anything else" do
lambda { format_xml("image_format" => "foo") }.should raise_error(EncodingDotCom::IllegalFormatAttribute)
end
end

describe "valid quality" do
it "should be between 1 and 100" do
lambda { format_xml("quality" => 0) }.should raise_error(EncodingDotCom::IllegalFormatAttribute)
lambda { format_xml("quality" => 101) }.should raise_error(EncodingDotCom::IllegalFormatAttribute)
lambda { format_xml("quality" => 1) }.should_not raise_error(EncodingDotCom::IllegalFormatAttribute)
lambda { format_xml("quality" => 100) }.should_not raise_error(EncodingDotCom::IllegalFormatAttribute)
end
end

def format_xml(attributes={})
format = EncodingDotCom::ImageFormat.new(attributes)
Expand Down

0 comments on commit 0c48b06

Please sign in to comment.