Class: Blocklist::Line

Inherits:
Object
  • Object
show all
Defined in:
lib/blocklist.rb

Constant Summary collapse

COMMENT_PREFIX =
/\A#+\s+/
IPV4 =
/\A\d{1,3}(\.\d{1,3}){3}/
IPV6 =
/(\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

Class Method Summary collapse

Instance Method Summary collapse

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

#commentedObject

Returns the value of attribute commented.



68
69
70
# File 'lib/blocklist.rb', line 68

def commented
  @commented
end

#domainsObject

Returns the value of attribute domains.



68
69
70
# File 'lib/blocklist.rb', line 68

def domains
  @domains
end

#ipObject

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_sObject



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_commentObject



83
84
85
# File 'lib/blocklist.rb', line 83

def toggle_comment
  self.commented = !commented
end