Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use is_color validator in plain generator #521

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/dragonfly/image_magick/generators/plain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 1 addition & 5 deletions lib/dragonfly/image_magick/generators/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
7 changes: 7 additions & 0 deletions lib/dragonfly/param_validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions spec/dragonfly/image_magick/generators/plain_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
14 changes: 0 additions & 14 deletions spec/dragonfly/image_magick/generators/text_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions spec/dragonfly/param_validators_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down