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

Returns:

  • (Hash)

    Proxy



47
48
49
50
51
52
53
54
55
56
# File 'lib/proxy_pool/dealer.rb', line 47

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

Parameters:

  • proxy (Hash)

    Proxy

Returns:

  • (nil)


62
63
64
65
66
# File 'lib/proxy_pool/dealer.rb', line 62

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
# 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 { |line| _pool_parse(line) }
end