Class: ProxyFetcher::Proxy

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

Overview

Proxy object

Constant Summary collapse

TYPES =

Proxy types

[
  HTTP = "HTTP",
  HTTPS = "HTTPS",
  SOCKS4 = "SOCKS4",
  SOCKS5 = "SOCKS5"
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Proxy

Initialize new Proxy

Parameters:

  • attributes (Hash) (defaults to: {})

    proxy attributes



65
66
67
68
69
# File 'lib/proxy_fetcher/proxy.rb', line 65

def initialize(attributes = {})
  attributes.each do |attr, value|
    public_send("#{attr}=", value)
  end
end

Instance Attribute Details

#addrString

Returns address (IP or domain).

Returns:

  • (String)

    address (IP or domain)



8
9
10
# File 'lib/proxy_fetcher/proxy.rb', line 8

def addr
  @addr
end

#anonymityString

Returns anonymity level (high, elite, transparent, etc).

Returns:

  • (String)

    anonymity level (high, elite, transparent, etc)



28
29
30
# File 'lib/proxy_fetcher/proxy.rb', line 28

def anonymity
  @anonymity
end

#countryString

Returns country or country code.

Returns:

  • (String)

    country or country code



20
21
22
# File 'lib/proxy_fetcher/proxy.rb', line 20

def country
  @country
end

#portInteger

Returns port.

Returns:

  • (Integer)

    port



12
13
14
# File 'lib/proxy_fetcher/proxy.rb', line 12

def port
  @port
end

#response_timeInteger

Returns response time (value and measurements depends on the provider).

Returns:

  • (Integer)

    response time (value and measurements depends on the provider)



24
25
26
# File 'lib/proxy_fetcher/proxy.rb', line 24

def response_time
  @response_time
end

#typeString

Returns type (SOCKS, HTTP(S)).

Returns:

  • (String)

    type (SOCKS, HTTP(S))



16
17
18
# File 'lib/proxy_fetcher/proxy.rb', line 16

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



108
109
110
# File 'lib/proxy_fetcher/proxy.rb', line 108

def ==(other)
  other.is_a?(Proxy) && addr == other.addr && port == other.port
end

#connectable?Boolean Also known as: valid?

Checks if proxy object is connectable (can be used as a proxy for HTTP requests).

Returns:

  • (Boolean)

    true if proxy connectable, otherwise false.



77
78
79
# File 'lib/proxy_fetcher/proxy.rb', line 77

def connectable?
  ProxyFetcher.config.proxy_validator.connectable?(addr, port)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
# File 'lib/proxy_fetcher/proxy.rb', line 112

def eql?(other)
  hash.eql?(other.hash)
end

#hashObject



116
117
118
# File 'lib/proxy_fetcher/proxy.rb', line 116

def hash
  [addr.hash, port.hash].hash
end

#proxy_typeBoolean

Proxy type predicates (#socks4?, #https?)

Returns:

  • (Boolean)

    true if proxy of requested type, otherwise false.



43
44
45
46
47
# File 'lib/proxy_fetcher/proxy.rb', line 43

TYPES.each do |proxy_type|
  define_method "#{proxy_type.downcase}?" do
    !type.nil? && type.upcase.include?(proxy_type)
  end
end

#ssl?Boolean

Returns true if proxy is secure (works through https, socks4 or socks5).

Returns:

  • (Boolean)

    true if proxy is secure, otherwise false.



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

def ssl?
  https? || socks4? || socks5?
end

#uriURI::Generic

Returns URI::Generic object with host and port values of the proxy.

Returns:

  • (URI::Generic)

    URI object.



88
89
90
# File 'lib/proxy_fetcher/proxy.rb', line 88

def uri
  URI::Generic.build(host: addr, port: port)
end

#url(scheme: false) ⇒ String

Returns String object with addr:port values of the proxy.

Parameters:

  • scheme (Boolean) (defaults to: false)

    Indicates if URL must include proxy type

Returns:

  • (String)

    true if proxy connectable, otherwise false.



100
101
102
103
104
105
106
# File 'lib/proxy_fetcher/proxy.rb', line 100

def url(scheme: false)
  if scheme
    URI::Generic.build(scheme: type, host: addr, port: port).to_s
  else
    URI::Generic.build(host: addr, port: port).to_s
  end
end