MetadataMapping: restructuration légère du code pour lisibilité…

This commit is contained in:
Jérémy Lecour 2021-01-20 14:09:02 +01:00 committed by Jérémy Lecour
parent a265b84efc
commit 0a6fb01008

View file

@ -23,8 +23,11 @@ module EmailAction
binding.pry binding.pry
end end
private
def metadata_inputs(email) def metadata_inputs(email)
inputs = [] inputs = []
# add mail addresses and hostnames from headers # add mail addresses and hostnames from headers
inputs << ["To", "Delivered-To", "X-Original-To", "From", "Subject"].filter_map { |header_name| inputs << ["To", "Delivered-To", "X-Original-To", "From", "Subject"].filter_map { |header_name|
email.header_values(header_name) email.header_values(header_name)
@ -33,31 +36,43 @@ module EmailAction
}.flatten.filter_map() { |value| }.flatten.filter_map() { |value|
clean_subdomains(value) clean_subdomains(value)
}.flatten }.flatten
# add other values from headers # add other values from headers
inputs << email.header_values("X-Client-Id") inputs << ["X-Client-Id", "X-Entity-Id"].filter_map { |header_name|
email.header_values(header_name)
}.flatten.uniq
inputs.flatten.uniq inputs.flatten.uniq
end end
def keep_email_and_hostnames(string) def keep_email_and_hostnames(string)
pattern = /\b((?:([-a-zA-Z0-9\._]+)@)?((?:[-a-zA-Z0-9\._]*)(?:\.[a-z]{2,})))\b/ results = string.scan(email_and_hostnames_pattern)
results = string.scan(pattern)
if results.present? if results.present?
results.map(&:first) results.map(&:first)
end end
end end
def clean_subdomains(value) def clean_subdomains(value)
[ clean_subdomains_patterns.filter_map { |pattern|
[/[-a-zA-Z0-9\._]+@([-a-zA-Z0-9]+).evolix.net/, '@\1'] if value.match?(pattern[:search])
].filter_map { |item| value.gsub!(pattern[:search], pattern[:replace])
if value.match?(item[0])
value.gsub!(item[0], item[1])
else else
value value
end end
} }
end end
def email_and_hostnames_pattern
/\b((?:([-a-zA-Z0-9\._]+)@)?((?:[-a-zA-Z0-9\._]*)(?:\.[a-z]{2,})))\b/
end
def clean_subdomains_patterns
[{
search: /[-a-zA-Z0-9\._]+@([-a-zA-Z0-9]+).evolix.net/,
replace: '@\1'
}]
end
end end
end end