Class: Blocklist::Line
- Inherits:
-
Object
- Object
- Blocklist::Line
- Defined in:
- lib/blocklist.rb
Constant Summary collapse
- COMMENT_PREFIX =
/\A#+\s+/- IPV4 =
/\A\d{1,3}(\.\d{1,3}){3}/- IPV6 =
Took this huge regexp from vernon.mauery.com/content/projects/linux/ipv6_regex
/(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,6}\Z)|(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,5}\Z)|(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,4}\Z)|(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,3}\Z)|(\A([0-9a-f]{1,4}:){1,5}(:[0-9a-f]{1,4}){1,2}\Z)|(\A([0-9a-f]{1,4}:){1,6}(:[0-9a-f]{1,4}){1,1}\Z)|(\A(([0-9a-f]{1,4}:){1,7}|:):\Z)|(\A:(:[0-9a-f]{1,4}){1,7}\Z)|(\A((([0-9a-f]{1,4}:){6})(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3})\Z)|(\A(([0-9a-f]{1,4}:){5}[0-9a-f]{1,4}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3})\Z)|(\A([0-9a-f]{1,4}:){5}:[0-9a-f]{1,4}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,1}(:[0-9a-f]{1,4}){1,4}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,2}(:[0-9a-f]{1,4}){1,3}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,3}(:[0-9a-f]{1,4}){1,2}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)|(\A([0-9a-f]{1,4}:){1,4}(:[0-9a-f]{1,4}){1,1}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)|(\A(([0-9a-f]{1,4}:){1,5}|:):(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)|(\A:(:[0-9a-f]{1,4}){1,5}:(25[0-5]|2[0-4]d|[0-1]?d?d)(.(25[0-5]|2[0-4]d|[0-1]?d?d)){3}\Z)/
Instance Attribute Summary collapse
-
#commented ⇒ Object
Returns the value of attribute commented.
-
#domains ⇒ Object
Returns the value of attribute domains.
-
#ip ⇒ Object
Returns the value of attribute ip.
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(ip, *domains) ⇒ Line
constructor
A new instance of Line.
- #to_s ⇒ Object
- #toggle_comment ⇒ Object
Constructor Details
#initialize(ip, *domains) ⇒ Line
Returns a new instance of Line.
70 71 72 73 74 |
# File 'lib/blocklist.rb', line 70 def initialize(ip, *domains) self.ip = ip self.domains = domains self.commented = false end |
Instance Attribute Details
#commented ⇒ Object
Returns the value of attribute commented.
68 69 70 |
# File 'lib/blocklist.rb', line 68 def commented @commented end |
#domains ⇒ Object
Returns the value of attribute domains.
68 69 70 |
# File 'lib/blocklist.rb', line 68 def domains @domains end |
#ip ⇒ Object
Returns the value of attribute ip.
68 69 70 |
# File 'lib/blocklist.rb', line 68 def ip @ip end |
Class Method Details
.parse(line) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/blocklist.rb', line 92 def self.parse(line) stripped_line = line.strip return nil if stripped_line == '' return Line.new(*stripped_line.split(" ")) unless stripped_line =~ COMMENT_PREFIX uncommented_line = stripped_line.gsub(COMMENT_PREFIX, '') split_line = uncommented_line.split(" ") if split_line.first =~ IPV4 parsed_line = Line.new(*split_line) parsed_line.commented = true parsed_line elsif split_line.first =~ IPV6 parsed_line = Line.new(*split_line) parsed_line.commented = true parsed_line else # comment or title uncommented_line end end |
Instance Method Details
#==(other) ⇒ Object
76 77 78 79 80 81 |
# File 'lib/blocklist.rb', line 76 def ==(other) return false unless other.class == self.class return false unless other.ip == self.ip return false unless other.domains == self.domains true end |
#to_s ⇒ Object
87 88 89 90 |
# File 'lib/blocklist.rb', line 87 def to_s prefix = commented ? "# " : "" "%s%s%s%s" % [prefix, ip, " " * (16 - ip.size), domains.join(" ")] end |
#toggle_comment ⇒ Object
83 84 85 |
# File 'lib/blocklist.rb', line 83 def toggle_comment self.commented = !commented end |