Class: ProxyFetcher::Manager
- Inherits:
-
Object
- Object
- ProxyFetcher::Manager
- Defined in:
- lib/proxy_fetcher/manager.rb
Constant Summary collapse
- EmptyProxyList =
Class.new(StandardError)
Instance Attribute Summary collapse
-
#proxies ⇒ Object
readonly
Returns the value of attribute proxies.
Instance Method Summary collapse
-
#cleanup! ⇒ Object
(also: #validate!)
Clean current proxy list from dead proxies (doesn’t respond by timeout).
-
#get ⇒ Object
(also: #pop)
Pop just first proxy (and back it to the end of the proxy list).
-
#get! ⇒ Object
(also: #pop!)
Pop first valid proxy (and back it to the end of the proxy list) Invalid proxies will be removed from the list.
-
#initialize(refresh: true) ⇒ Manager
constructor
refresh: true - load proxy list from the remote server on initialization refresh: false - just initialize the class, proxy list will be empty ([]).
-
#inspect ⇒ Object
No need to put all the attr_readers.
-
#random ⇒ Object
Return random proxy.
-
#raw_proxies ⇒ Object
Returns array of proxy URLs (just schema + host + port).
-
#refresh_list! ⇒ Object
(also: #fetch!)
Update current proxy list from the provider.
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
#proxies ⇒ Object (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 |
#get ⇒ Object 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 |
#inspect ⇒ Object
No need to put all the attr_readers
71 72 73 |
# File 'lib/proxy_fetcher/manager.rb', line 71 def inspect to_s end |
#random ⇒ Object
Return random proxy
61 62 63 |
# File 'lib/proxy_fetcher/manager.rb', line 61 def random proxies.sample end |
#raw_proxies ⇒ Object
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 |