From bdd9ee6a0b2943d035f710b12751715df9ca3cc4 Mon Sep 17 00:00:00 2001 From: Kiriakos Velissariou Date: Sat, 13 Mar 2021 17:57:20 +0200 Subject: [PATCH] Refactoring: Move allowed characters out of hardcoded regex This refactoring will allow for an easier addition/deletion of allowed characters, without directly manipulating the regular expression. --- lib/misc/allowed_chars.txt | 1 + lib/sepa_king/converter.rb | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 lib/misc/allowed_chars.txt diff --git a/lib/misc/allowed_chars.txt b/lib/misc/allowed_chars.txt new file mode 100644 index 0000000..e3f4880 --- /dev/null +++ b/lib/misc/allowed_chars.txt @@ -0,0 +1 @@ +abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789ÄÖÜäöüß&*$% ':?,-(+.)/ diff --git a/lib/sepa_king/converter.rb b/lib/sepa_king/converter.rb index 4141964..9f345d3 100644 --- a/lib/sepa_king/converter.rb +++ b/lib/sepa_king/converter.rb @@ -15,6 +15,8 @@ def convert(*attributes, options) end module InstanceMethods + ALLOWED_CHARS = Regexp.escape(File.read('lib/misc/allowed_chars.txt')).freeze + def convert_text(value) return unless value @@ -29,7 +31,7 @@ def convert_text(value) gsub(/\n+/,' '). # Remove all invalid characters - gsub(/[^a-zA-Z0-9ÄÖÜäöüß&*$%\ \'\:\?\,\-\(\+\.\)\/]/, ''). + gsub(/[^#{ALLOWED_CHARS}]/, ''). # Remove leading and trailing spaces strip