Class: ProxyPool::Dealer

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/proxy_pool/dealer.rb

Overview

Pool

Defined Under Namespace

Classes: HTTPError, ParseError

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#poolsObject (readonly)

Returns the value of attribute pools.



29
30
31
# File 'lib/proxy_pool/dealer.rb', line 29

def pools
  @pools
end

Instance Method Details

#getHash

Get a random proxy



51
52
53
54
55
56
57
58
59
60
# File 'lib/proxy_pool/dealer.rb', line 51

def get
  update if _need_update?

  target_pools = if block_given?
                   @pools.select { |proxy| yield proxy }
                 else
                   @pools
                 end
  target_pools.sample
end

#remove(proxy) ⇒ nil

Remove this proxy from pool



66
67
68
69
70
# File 'lib/proxy_pool/dealer.rb', line 66

def remove(proxy)
  @pools.delete(proxy)

  nil
end

#updateObject

Update to latest proxy list from fate0/proxylist

Raises:



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/proxy_pool/dealer.rb', line 33

def update
  @pools = []

  res = HTTP.get 'https://raw.githubusercontent.com/fate0/proxylist/master/proxy.list'
  raise HTTPError, "invalid http code #{res.code}" if res.code != 200

  @updated_at = Time.now

  res.body.to_s.split("\n").each do |line|
    _pool_parse(line)
  rescue ParseError
    next
  end
end