class Host

Public Class Methods

add(hostname, hashtag=hostname.sub(/^www./,'').gsub('-','').gsub('.','dot'), banlist: nil, block: false) click to toggle source
# File lib/blockhosts.rb, line 21
def self.add(hostname, 
             hashtag=hostname.sub(/^www./,'').gsub('-','').gsub('.','dot'),
             banlist: nil, block: false)

  b = block ? '' : '#'
  
  s = if banlist then

    hashtag = hostname.sub(/^#/,'')
    list, _ = RXFHelper.read(banlist)
    list.lines.map {|hostx| "#{b}#{@ip} #{hostx.chomp} ##{hashtag}" }\
        .join("\n")

  else
    "#{b}#{@ip} #{hostname} ##{hashtag}"
  end

  open(@file, 'a') { |f| f.puts s } unless File.read(@file).include? hostname
end
block(hashtag) click to toggle source
# File lib/blockhosts.rb, line 76
def self.block(hashtag)   self.disable(hashtag)  end
disable(hashtag) click to toggle source
# File lib/blockhosts.rb, line 41
def self.disable(hashtag)   

  modify() do |line|
    line.gsub(/^#([^#]+##{hashtag.sub(/^#/,'')}[^$]+)/,'\1')
  end

end
enable(hashtag) click to toggle source
# File lib/blockhosts.rb, line 49
def self.enable(hashtag)

  modify() do |line|
    
    if line[0] == '#' then
      line
    else
      line.gsub(/^([^^]+##{hashtag.sub(/^#/,'')}[^$]+$)/,'#\1')
    end
    
  end

end
exists?(host) click to toggle source
# File lib/blockhosts.rb, line 63
def self.exists?(host)
  s = File.read(@file)
  s.lines.grep(/\s#{host}\s/).any?
end
export(hashtag, filename=nil) click to toggle source
# File lib/blockhosts.rb, line 68
def self.export(hashtag, filename=nil)
  
  s = self.view(hashtag)
   
  filename ? FileX.write(filename, s) : s
   
end
modify() { |x| ... } click to toggle source
# File lib/blockhosts.rb, line 79
def self.modify()
 
  return unless block_given?

  s = File.read(@file)
  s2 = s.lines.map {|x| yield(x)}.join
  File.write(@file, s2) unless s == s2

end
rm(hashtag) click to toggle source
# File lib/blockhosts.rb, line 89
def self.rm(hashtag)   

  modify() {|line| line =~ /##{hashtag}/ ? '' : line }

end
unblock(hashtag) click to toggle source
# File lib/blockhosts.rb, line 77
def self.unblock(hashtag) self.enable(hashtag)   end
view(hashtag) click to toggle source
# File lib/blockhosts.rb, line 95
def self.view(hashtag)
  
  File.read(@file).lines.select {|x| x =~ /##{hashtag}/}\
       .map {|x| x[/(?<=#{@ip} )[^ ]+/]}.join("\n")    
  
end