class Marina

Constants

VERSION

Attributes

host_entries[RW]
hosts_file_path[RW]
hostspec_file_path[RW]
specified_hosts[RW]

Public Class Methods

new(hosts_file_path = '/etc/hosts', hostspec_file_path = 'Hostspec') click to toggle source
# File lib/marina.rb, line 7
def initialize hosts_file_path = '/etc/hosts', hostspec_file_path = 'Hostspec'
  @hosts_file_path = hosts_file_path
  @hostspec_file_path = hostspec_file_path
  @specified_hosts = []
end

Public Instance Methods

add_to_hostfile() click to toggle source
# File lib/marina.rb, line 22
def add_to_hostfile
  begin
    puts "\nAdding hosts to hosts file:" if File.exists?(@hosts_file_path)
    specified_hosts.each do |host_spec|
      if host_entries.include? "127.0.0.1 #{host_spec}\n"
        puts "\t-\s#{host_spec} is already in the hosts file"
        next
      end
      hosts.write("127.0.0.1 #{host_spec}\n")
      puts "\t-\s#{host_spec} written to the hosts file"
    end
  rescue IOError
  rescue Errno::ENOENT
    puts "marina: #{@hosts_file_path}: No such file"
    exit(66)
  rescue Errno::EACCES
    puts "\tmarina: #{@hosts_file_path}: Permission denied"
    exit(66)
  end
end
host(host) click to toggle source
# File lib/marina.rb, line 18
def host host
  @specified_hosts << host
end
list() click to toggle source
# File lib/marina.rb, line 13
def list
  answer = clean_hosts || 'Host file does not exist'
  puts answer
end

Private Instance Methods

chaff_free(hosts_file) click to toggle source
# File lib/marina.rb, line 66
def chaff_free(hosts_file)
  hosts_file.each_line.reject do |line|
    is_a_comment?(line) or not is_localhost?(line)
  end
end
clean_hosts() click to toggle source
# File lib/marina.rb, line 58
def clean_hosts
  return nil unless File.exists?(@hosts_file_path)

  File.open(@hosts_file_path) do |hosts_file|
    return chaff_free hosts_file
  end
end
hosts() click to toggle source
# File lib/marina.rb, line 50
def hosts
  if File.exists?(@hosts_file_path)
    File.open(@hosts_file_path, 'a+')
  else
    raise Errno::ENOENT
  end
end
is_a_comment?(line) click to toggle source
# File lib/marina.rb, line 72
def is_a_comment?(line)
  line =~ /\A\#/
end
is_localhost?(line) click to toggle source
# File lib/marina.rb, line 76
def is_localhost?(line)
  line =~ /\A127\.0\.0\.1/
end