module Hostscare
Constants
- LOCAL_HOSTS_FILE
- PLACEHOLDER_END
- PLACEHOLDER_START
- PUBLIC_HOSTS_FILE
Public Class Methods
build_hosts(hosts, ph_start, ph_end, someonewhocares_hosts)
click to toggle source
Compose the new hosts file
# File lib/hostscare.rb, line 29 def self.build_hosts(hosts, ph_start, ph_end, someonewhocares_hosts) final_hosts = [hosts, ph_start, "# -- Updated on #{Time.now}", someonewhocares_hosts, ph_end] final_hosts.map(&:strip) .join("\n\n") end
cleanup()
click to toggle source
# File lib/hostscare.rb, line 11 def self.cleanup hosts = File.read(LOCAL_HOSTS_FILE) write(raw_hosts(hosts), LOCAL_HOSTS_FILE) end
raw_hosts(txt)
click to toggle source
Extract hosts without those coming from someonewhocares
# File lib/hostscare.rb, line 50 def self.raw_hosts(txt) hosts = [] header_txt = txt.scan(/^((.|\n)*)#{PLACEHOLDER_START}/) hosts << header_txt[0][0] if header_txt.count.positive? footer_txt = txt.scan(/#{PLACEHOLDER_END}((.|\n)*)/) hosts << footer_txt[0][0] if footer_txt.count.positive? hosts.count.positive? ? hosts.map(&:strip).join("\n\n") : txt end
update()
click to toggle source
# File lib/hostscare.rb, line 16 def self.update hosts = File.read(LOCAL_HOSTS_FILE) someonewhocares_hosts = URI.open(PUBLIC_HOSTS_FILE).read new_hosts = build_hosts(raw_hosts(hosts), PLACEHOLDER_START, PLACEHOLDER_END, someonewhocares_hosts) write(new_hosts, LOCAL_HOSTS_FILE) end
write(txt, path)
click to toggle source
# File lib/hostscare.rb, line 40 def self.write(txt, path) File.open(path, 'w') do |f| f.write(txt) end puts "🍺 #{path} updated!" rescue Errno::EACCES puts "❌ Requires sudo to update the file #{path}" end