Class: Discipline::Base
- Inherits:
-
Object
- Object
- Discipline::Base
- Defined in:
- lib/discipline.rb
Constant Summary collapse
- DISCIPLINE_CONFIG_FILE =
File.("~/.discipline_config.yml")
- HOSTS_FILE =
'/etc/hosts'- DELIMITER_START =
'# DISCIPLINE BLOCKED HOSTS START'- DELIMITER_END =
'# DISCIPLINE BLOCKED HOSTS END'
Instance Method Summary collapse
- #clear_hosts_file ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #off ⇒ Object
- #on ⇒ Object
- #write(append) ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
8 9 10 11 12 13 14 |
# File 'lib/discipline.rb', line 8 def initialize begin @files = YAML.load(File.read(DISCIPLINE_CONFIG_FILE)) rescue raise "Please create a valid config file at #{DISCIPLINE_CONFIG_FILE}" end end |
Instance Method Details
#clear_hosts_file ⇒ Object
31 32 33 34 35 |
# File 'lib/discipline.rb', line 31 def clear_hosts_file hosts_file = File.read(HOSTS_FILE) hosts_file.gsub!(/#{DELIMITER_START}(.*)#{DELIMITER_END}/m, '') hosts_file.strip end |
#off ⇒ Object
27 28 29 |
# File 'lib/discipline.rb', line 27 def off write([clear_hosts_file]) end |
#on ⇒ Object
16 17 18 19 20 21 22 23 24 25 |
# File 'lib/discipline.rb', line 16 def on append = [clear_hosts_file] append << DELIMITER_START @files.each do |file| append << "0.0.0.0 #{file}" end append << DELIMITER_END write(append) end |
#write(append) ⇒ Object
37 38 39 40 41 |
# File 'lib/discipline.rb', line 37 def write(append) File.open(HOSTS_FILE, 'w+') do |f| f.puts append.join("\n") end end |