From 2af525fbde1ee1d0370b47f0d5783c71bd3ec79f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Romain=20Tarti=C3=A8re?= Date: Sun, 25 Feb 2024 18:45:35 -1000 Subject: [PATCH] Reduce complexity reported by codeclimate --- lib/puffy/formatters/pf.rb | 10 +++++++++- lib/puffy/rule.rb | 3 +-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/puffy/formatters/pf.rb b/lib/puffy/formatters/pf.rb index 5b0e86a..0b83ded 100644 --- a/lib/puffy/formatters/pf.rb +++ b/lib/puffy/formatters/pf.rb @@ -116,13 +116,21 @@ def emit_address(host, if_unspecified = 'any') def emit_rdr_to(rule) return unless rule.rdr? - keyword = Puffy::Formatters::Base.loopback_addresses.include?(rule.rdr_to_host) ? 'divert-to' : 'rdr-to' + keyword = rdr_to_keyword(rule) destination = rule.rdr_to_host || loopback_address(rule.af) raise 'Unspecified address family' if destination.nil? emit_endpoint_specification(keyword, destination, rule.rdr_to_port) end + def rdr_to_keyword(rule) + if Puffy::Formatters::Base.loopback_addresses.include?(rule.rdr_to_host) + 'divert-to' + else + 'rdr-to' + end + end + def emit_nat_to(rule) "nat-to #{emit_address(rule.nat_to)}" if rule.nat_to end diff --git a/lib/puffy/rule.rb b/lib/puffy/rule.rb index 26d816a..8ec9313 100644 --- a/lib/puffy/rule.rb +++ b/lib/puffy/rule.rb @@ -64,8 +64,7 @@ class Rule def initialize(options = {}) send_options(options) - @af = detect_af unless af - + self.af ||= detect_af self.proto ||= from_proto_hint || to_proto_hint raise "unsupported action `#{options[:action]}'" unless valid_action?