From 90594b90c1e7095218c925686dfaee1d9791ae73 Mon Sep 17 00:00:00 2001 From: Ben McFadden Date: Thu, 7 Jul 2022 10:49:50 -0400 Subject: [PATCH] Move IS_COLOUR validator to ParamValidator module and use throughout --- lib/dragonfly/image_magick/generators/plain.rb | 3 ++- lib/dragonfly/image_magick/generators/text.rb | 6 +----- lib/dragonfly/param_validators.rb | 7 +++++++ .../image_magick/generators/plain_spec.rb | 4 ++++ .../image_magick/generators/text_spec.rb | 14 -------------- spec/dragonfly/param_validators_spec.rb | 16 ++++++++++++++++ 6 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/dragonfly/image_magick/generators/plain.rb b/lib/dragonfly/image_magick/generators/plain.rb index f0ed8455..d9c0e415 100644 --- a/lib/dragonfly/image_magick/generators/plain.rb +++ b/lib/dragonfly/image_magick/generators/plain.rb @@ -9,7 +9,8 @@ class Plain def call(content, width, height, opts = {}) validate_all!([width, height], &is_number) - validate_all_keys!(opts, %w(colour color format), &is_word) + validate!(opts["format"], &is_word) + validate_all_keys!(opts, %w(colour color), &is_colour) format = extract_format(opts) colour = opts["colour"] || opts["color"] || "white" diff --git a/lib/dragonfly/image_magick/generators/text.rb b/lib/dragonfly/image_magick/generators/text.rb index 8f1e5a3b..400fa4b0 100644 --- a/lib/dragonfly/image_magick/generators/text.rb +++ b/lib/dragonfly/image_magick/generators/text.rb @@ -42,17 +42,13 @@ class Text "900" => 900, } - IS_COLOUR = ->(param) { - /\A(#\w+|rgba?\([\d\.,]+\)|\w+)\z/ === param - } - def update_url(url_attributes, string, opts = {}) url_attributes.name = "text.#{extract_format(opts)}" end def call(content, string, opts = {}) validate_all_keys!(opts, %w(font font_family), &is_words) - validate_all_keys!(opts, %w(color background_color stroke_color), &IS_COLOUR) + validate_all_keys!(opts, %w(color background_color stroke_color), &is_colour) validate!(opts["format"], &is_word) opts = HashWithCssStyleKeys[opts] diff --git a/lib/dragonfly/param_validators.rb b/lib/dragonfly/param_validators.rb index ac17a23b..40816b8e 100644 --- a/lib/dragonfly/param_validators.rb +++ b/lib/dragonfly/param_validators.rb @@ -8,6 +8,10 @@ class InvalidParameter < RuntimeError; end param.is_a?(Numeric) || /\A[\d\.]+\z/ === param } + IS_COLOUR = ->(param) { + /\A(#\w+|rgba?\([\d\.,]+\)|\w+)\z/ === param + } + IS_WORD = ->(param) { /\A\w+\z/ === param } @@ -17,9 +21,12 @@ class InvalidParameter < RuntimeError; end } def is_number; IS_NUMBER; end + def is_colour; IS_COLOUR; end def is_word; IS_WORD; end def is_words; IS_WORDS; end + alias is_color is_colour + def validate!(parameter, &validator) return if parameter.nil? raise InvalidParameter unless validator.(parameter) diff --git a/spec/dragonfly/image_magick/generators/plain_spec.rb b/spec/dragonfly/image_magick/generators/plain_spec.rb index 7eafe863..1dcd61a2 100644 --- a/spec/dragonfly/image_magick/generators/plain_spec.rb +++ b/spec/dragonfly/image_magick/generators/plain_spec.rb @@ -33,6 +33,10 @@ generator.call(image, 1, 1, "color" => "red") end + it "works with Hex colors" do + generator.call(image, 1, 1, "color" => "#FF0000") + end + it "blows up with a bad colour" do expect { generator.call(image, 1, 1, "colour" => "lardoin") diff --git a/spec/dragonfly/image_magick/generators/text_spec.rb b/spec/dragonfly/image_magick/generators/text_spec.rb index 8ef5f895..da9f3aea 100644 --- a/spec/dragonfly/image_magick/generators/text_spec.rb +++ b/spec/dragonfly/image_magick/generators/text_spec.rb @@ -90,19 +90,5 @@ }.to raise_error(Dragonfly::ParamValidators::InvalidParameter) end end - - ["rgb(33,33,33)", "rgba(33,33,33,0.5)", "rgb(33.5,33.5,33.5)", "#fff", "#efefef", "blue"].each do |colour| - it "allows #{colour.inspect} as a colour specification" do - generator.call(image, "mmm", "color" => colour) - end - end - - ["rgb(33, 33, 33)", "something else", "blue:", "f#ff"].each do |colour| - it "disallows #{colour.inspect} as a colour specification" do - expect { - generator.call(image, "mmm", "color" => colour) - }.to raise_error(Dragonfly::ParamValidators::InvalidParameter) - end - end end end diff --git a/spec/dragonfly/param_validators_spec.rb b/spec/dragonfly/param_validators_spec.rb index 3907f6d5..5a6e165e 100644 --- a/spec/dragonfly/param_validators_spec.rb +++ b/spec/dragonfly/param_validators_spec.rb @@ -71,6 +71,22 @@ end end + describe "is_colour" do + ["rgb(33,33,33)", "rgba(33,33,33,0.5)", "rgb(33.5,33.5,33.5)", "#fff", "#efefef", "blue"].each do |val| + it "validates #{val.inspect}" do + validate!(val, &is_colour) + end + end + + ["rgb(33, 33, 33)", "something else", "blue:", "f#ff"].each do |val| + it "validates #{val.inspect}" do + expect { + validate!(val, &is_colour) + }.to raise_error(Dragonfly::ParamValidators::InvalidParameter) + end + end + end + describe "is_words" do ["hello there", "Hi", " What is Up "].each do |val| it "validates #{val.inspect}" do