Class: Mallory::Backend::File

Inherits:
Object
  • Object
show all
Defined in:
lib/mallory/backend/file.rb

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ File

It would be cool to add signal trap to refresh proxy list when file contents change (with initial validation, so if file is malformed, old list stays)



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

def initialize(filename)
  @proxies = []
  begin
    lines = ::File.readlines(filename)
    raise if lines.nil?
    raise if lines.empty?
  rescue
    raise("Proxy file missing or empty")
  end
  lines.each do |line|
    if line.strip.match(/.*:\d{2,6}/)
      @proxies << line.strip
    else raise("Wrong format") end
  end
end

Instance Method Details

#allObject



28
29
30
# File 'lib/mallory/backend/file.rb', line 28

def all
  @proxies
end

#anyObject



24
25
26
# File 'lib/mallory/backend/file.rb', line 24

def any
  @proxies.sample
end