Module: Hostman

Defined in:
lib/hostman.rb,
lib/hostman/version.rb,
lib/hostman/executor.rb

Defined Under Namespace

Classes: Executor

Constant Summary collapse

BLOCK_UNBLOCK_ERROR =
"Both --block and --unblock set, doing nothing..."
VERSION =
"0.5.0"

Class Method Summary collapse

Class Method Details

.block(hosts_file, domain) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hostman.rb', line 8

def self.block hosts_file, domain
    not_blocked = true

    File.open(hosts_file, "r") do |f|
      f.each_line do |line|
        not_blocked = false if line == "127.0.0.1\t#{domain}\n"
      end
    end

    if not_blocked
      File.open(hosts_file, "a") do |f|
        f.puts "127.0.0.1\t#{domain}"
      end
    end
end

.unblock(hosts_file, domain) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/hostman.rb', line 24

def self.unblock hosts_file, domain
  contents = ""
  File.open(hosts_file, "r") do |f|
    f.each_line do |line|
      contents += line unless line == "127.0.0.1\t#{domain}\n"
    end
  end

  File.open(hosts_file, "w") do |f|
    f.puts contents
  end
end