From 682f66f476c4fa7b72641eda1226995a49e2b73d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A2u=20Cao?= Date: Wed, 14 Aug 2024 16:12:28 +0200 Subject: [PATCH] Allow selecting image type via Regexp For example, SVG images might be of type "image/svg" or "image/svg+xml" --- lib/manifique/metadata.rb | 8 +++++++- spec/manifique/metadata_spec.rb | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/manifique/metadata.rb b/lib/manifique/metadata.rb index 64a8ac2..944c6f4 100644 --- a/lib/manifique/metadata.rb +++ b/lib/manifique/metadata.rb @@ -43,7 +43,13 @@ module Manifique end if options[:type] - results.reject! { |r| r["type"] != options[:type] } + if options[:type].is_a?(String) + results.reject! { |r| r["type"] != options[:type] } + elsif options[:type].is_a?(Regexp) + results.reject! { |r| r["type"].match(options[:type]).nil? } + else + raise ArgumentError, "Type must be a string or a regular expression" + end end if options[:sizes] diff --git a/spec/manifique/metadata_spec.rb b/spec/manifique/metadata_spec.rb index adf025c..49ffd6e 100644 --- a/spec/manifique/metadata_spec.rb +++ b/spec/manifique/metadata_spec.rb @@ -68,6 +68,16 @@ RSpec.describe Manifique::Metadata do icon = metadata.select_icon(type: "image/png") expect(icon["sizes"]).to eq("512x512") end + + it "matches strings exactly" do + icon = metadata.select_icon(type: "image/svg") + expect(icon["type"]).to eq("image/svg") + end + + it "works with a regex" do + icon = metadata.select_icon(type: /image\/svg/) + expect(icon["type"]).to eq("image/svg+xml") + end end describe "by size" do -- 2.25.1