class Blockr::Manager
Constants
- ACTIVATED
- DEACTIVATED
- DISABLED_BLOCKR_LINE_PREFIX
- DUMMY_IP
- FILE_EDITED_BY_BLOCKR_HEADER
- HOSTS_FILE
- KEY_BLOCKR_HOSTNAMES
- KEY_NON_BLOCKR_LINES
- KEY_STATUS
- LINE_EDITED_BY_BLOCKR
Attributes
parsed_data[R]
for testing
Public Class Methods
new()
click to toggle source
# File lib/blockr/manager.rb, line 25 def initialize @parsed_data = parse_file(HOSTS_FILE) end
Public Instance Methods
activate()
click to toggle source
# File lib/blockr/manager.rb, line 45 def activate() enable() serialize() end
block(hostnames)
click to toggle source
# File lib/blockr/manager.rb, line 29 def block(hostnames) return if blank?(hostnames) hostnames.each do |h| @parsed_data[KEY_BLOCKR_HOSTNAMES].push(h) if !blank?(h) end serialize() end
deactivate()
click to toggle source
# File lib/blockr/manager.rb, line 50 def deactivate() disable() serialize() end
unblock(hostnames)
click to toggle source
# File lib/blockr/manager.rb, line 38 def unblock(hostnames) return if blank?(hostnames) @parsed_data[KEY_BLOCKR_HOSTNAMES].reject! { |h| hostnames.include?(h)} serialize() end
Private Instance Methods
blank?(str)
click to toggle source
# File lib/blockr/manager.rb, line 74 def blank?(str) str.nil? || str.empty? end
clear_cache()
click to toggle source
# File lib/blockr/manager.rb, line 152 def clear_cache begin cmd = TTY::Command.new $stdout.puts "Clearing DNS cache" # XXX mac specific out, _ = cmd.run("sudo killall -HUP mDNSResponder", only_output_on_error: true) $stdout.puts out rescue $stderr.puts "Couldn't clear the cache, please clear cache manually." $stderr.puts "See https://www.abhinav.co/clear-dns-cache.html to findout command for your operating system" end end
disable()
click to toggle source
# File lib/blockr/manager.rb, line 114 def disable() @parsed_data[KEY_STATUS] = DEACTIVATED end
enable()
click to toggle source
# File lib/blockr/manager.rb, line 118 def enable() @parsed_data[KEY_STATUS] = ACTIVATED end
get_status(header_line)
click to toggle source
# File lib/blockr/manager.rb, line 65 def get_status(header_line) matched = header_line.match(/#{FILE_EDITED_BY_BLOCKR_HEADER}#{KEY_STATUS}:(?<status>\w+)/) matched['status'] == ACTIVATED ? ACTIVATED : DEACTIVATED end
header_line?(line)
click to toggle source
# File lib/blockr/manager.rb, line 61 def header_line?(line) line.include?(FILE_EDITED_BY_BLOCKR_HEADER) end
line_edited_by_blockr?(line)
click to toggle source
# File lib/blockr/manager.rb, line 70 def line_edited_by_blockr?(line) line.include?(LINE_EDITED_BY_BLOCKR) end
parse(lines)
click to toggle source
# File lib/blockr/manager.rb, line 83 def parse(lines) parsed_data = { KEY_STATUS => ACTIVATED, KEY_NON_BLOCKR_LINES => [], KEY_BLOCKR_HOSTNAMES => [] } parsed_data[KEY_NON_BLOCKR_LINES] = lines.select do |line| !header_line?(line) && !line_edited_by_blockr?(line) end header_line, _ = lines.select { |line| header_line?(line) } if !blank?(header_line) parsed_data[KEY_STATUS] = get_status(header_line) end blockr_lines = lines.select do |line| !blank?(line) && !header_line?(line) && line_edited_by_blockr?(line) end parsed_data[KEY_BLOCKR_HOSTNAMES] = blockr_lines.map do |line| line.sub!(DISABLED_BLOCKR_LINE_PREFIX, '') line.sub!(LINE_EDITED_BY_BLOCKR, '') words = line.split(' ') _ip_address, hostname = words.select { |word| !blank?(word) } hostname end parsed_data end
parse_file(filename)
click to toggle source
# File lib/blockr/manager.rb, line 78 def parse_file(filename) lines = read_file(filename) parse(lines) end
read_file(filename)
click to toggle source
# File lib/blockr/manager.rb, line 57 def read_file(filename) File.readlines(filename).map(&:chomp) end
serialize()
click to toggle source
# File lib/blockr/manager.rb, line 122 def serialize() serialized_output = @parsed_data[KEY_NON_BLOCKR_LINES].map { |line| line } blockr_line_prefix = @parsed_data[KEY_STATUS] == DEACTIVATED ? DISABLED_BLOCKR_LINE_PREFIX : "" serialized_output.push("#{FILE_EDITED_BY_BLOCKR_HEADER}#{KEY_STATUS}:#{@parsed_data[KEY_STATUS]}") # uniq @parsed_data[KEY_BLOCKR_HOSTNAMES].uniq! serialized_output = serialized_output + @parsed_data[KEY_BLOCKR_HOSTNAMES].map do |hostname| "#{blockr_line_prefix}#{DUMMY_IP} #{hostname} #{LINE_EDITED_BY_BLOCKR}" end if(write_file(serialized_output)) clear_cache() end end
write_file(serialized_output)
click to toggle source
# File lib/blockr/manager.rb, line 142 def write_file(serialized_output) begin TTY::File.create_file HOSTS_FILE, serialized_output.join("\n") rescue $stderr.puts "Error in writing file, please try using sudo" return false end return true end