demarches-normaliennes/app/services/ip_service.rb
François Vantomme c9b1095d1e Refactor (Rubocop): replace map{ … }.compact by filter_map
In Ruby 2.7, Enumerable#filter_map has been added.
This cop identifies places where map { … }.compact can be replaced by filter_map.

See: https://docs.rubocop.org/rubocop-performance/cops_performance.html#performancemapcompact
2021-06-11 15:38:09 +02:00

30 lines
526 B
Ruby

class IPService
class << self
def ip_trusted?(ip)
ip_address = parse_address(ip)
trusted_networks.any? { |network| network.include?(ip_address) }
end
private
def trusted_networks
if ENV['TRUSTED_NETWORKS'].present?
ENV['TRUSTED_NETWORKS']
.split
.filter_map { |string| parse_address(string) }
else
[]
end
end
def parse_address(address)
begin
IPAddr.new(address)
rescue
nil
end
end
end
end