Class: Blockr::Manager

Inherits:
Object
  • Object
show all
Defined in:
lib/blockr/manager.rb

Constant Summary collapse

HOSTS_FILE =
"/etc/hosts"
KEY_STATUS =
"status"
KEY_NON_BLOCKR_LINES =
"non_blockr_lines"
KEY_BLOCKR_HOSTNAMES =
"blocked_hostnames"
FILE_EDITED_BY_BLOCKR_HEADER =
"# NOTE: following entries were added by blockr, please DON'T manually edit this or any of the following lines  ; "
ACTIVATED =
"activated"
DEACTIVATED =
"deactivated"
LINE_EDITED_BY_BLOCKR =
"# XXX: added by blockr ;"
DISABLED_BLOCKR_LINE_PREFIX =
"#  "
DUMMY_IP =
"127.0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeManager

Returns a new instance of Manager.



25
26
27
# File 'lib/blockr/manager.rb', line 25

def initialize
  @parsed_data = parse_file(HOSTS_FILE)
end

Instance Attribute Details

#parsed_dataObject (readonly)

for testing



23
24
25
# File 'lib/blockr/manager.rb', line 23

def parsed_data
  @parsed_data
end

Instance Method Details

#activateObject



45
46
47
48
# File 'lib/blockr/manager.rb', line 45

def activate()
  enable()
  serialize()
end

#block(hostnames) ⇒ Object



29
30
31
32
33
34
35
36
# 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

#deactivateObject



50
51
52
53
# File 'lib/blockr/manager.rb', line 50

def deactivate()
  disable()
  serialize()
end

#unblock(hostnames) ⇒ Object



38
39
40
41
42
43
# 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