Class: ProxyFetcher::Manager

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

Constant Summary collapse

EmptyProxyList =
Class.new(StandardError)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(refresh: true) ⇒ Manager

refresh: true - load proxy list from the remote server on initialization refresh: false - just initialize the class, proxy list will be empty ([])



9
10
11
12
13
14
15
# File 'lib/proxy_fetcher/manager.rb', line 9

def initialize(refresh: true)
  if refresh
    refresh_list!
  else
    @proxies = []
  end
end

Instance Attribute Details

#proxiesObject (readonly)

Returns the value of attribute proxies.



5
6
7
# File 'lib/proxy_fetcher/manager.rb', line 5

def proxies
  @proxies
end

Instance Method Details

#cleanup!Object Also known as: validate!

Clean current proxy list from dead proxies (doesn’t respond by timeout)



54
55
56
# File 'lib/proxy_fetcher/manager.rb', line 54

def cleanup!
  proxies.keep_if(&:connectable?)
end

#getObject Also known as: pop

Pop just first proxy (and back it to the end of the proxy list)



26
27
28
29
30
31
32
33
# File 'lib/proxy_fetcher/manager.rb', line 26

def get
  return if @proxies.empty?

  first_proxy = @proxies.shift
  @proxies << first_proxy

  first_proxy
end

#get!Object Also known as: pop!

Pop first valid proxy (and back it to the end of the proxy list) Invalid proxies will be removed from the list



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/proxy_fetcher/manager.rb', line 39

def get!
  index = proxies.find_index(&:connectable?)
  return if index.nil?

  proxy = proxies.delete_at(index)
  tail = proxies[index..-1]

  @proxies = tail << proxy

  proxy
end

#inspectObject

No need to put all the attr_readers



71
72
73
# File 'lib/proxy_fetcher/manager.rb', line 71

def inspect
  to_s
end

#randomObject

Return random proxy



61
62
63
# File 'lib/proxy_fetcher/manager.rb', line 61

def random
  proxies.sample
end

#raw_proxiesObject

Returns array of proxy URLs (just schema + host + port)



66
67
68
# File 'lib/proxy_fetcher/manager.rb', line 66

def raw_proxies
  proxies.map(&:url)
end

#refresh_list!Object Also known as: fetch!

Update current proxy list from the provider



18
19
20
21
# File 'lib/proxy_fetcher/manager.rb', line 18

def refresh_list!
  rows = ProxyFetcher.config.provider.load_proxy_list
  @proxies = rows.map { |row| Proxy.new(row) }
end