Skip to content

Commit

Permalink
Some code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nbulaj committed May 26, 2017
1 parent 8f3d31d commit 00b3af0
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 89 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ Also you can call next instance method for every Proxy object:

You can use two methods to get the first proxy from the list:

* `get` (will return first proxy and move it to the end of the list)
* `get!` (will return first **connectable** proxy and move it to the end of the list; all the proxies till the working one will be removed)
* `get` or aliased `pop` (will return first proxy and move it to the end of the list)
* `get!` or aliased `pop!` (will return first **connectable** proxy and move it to the end of the list; all the proxies till the working one will be removed)

If you wanna clear current proxy manager list from dead servers, you can just call `cleanup!` method:

Expand Down
87 changes: 1 addition & 86 deletions lib/proxy_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,92 +4,7 @@

require 'proxy_fetcher/configuration'
require 'proxy_fetcher/proxy'
require 'proxy_fetcher/manager'

module ProxyFetcher
class Manager
PROXY_PROVIDER_URL = 'http://proxylist.hidemyass.com/'.freeze

class << self
def config
@config ||= ProxyFetcher::Configuration.new
end
end

attr_reader :proxies

# refresh: true - load proxy list from the remote server on initialization
# refresh: false - just initialize the class, proxy list will be empty ([])
def initialize(refresh: true)
if refresh
refresh_list!
else
@proxies = []
end
end

# Update current proxy list from the provider
def refresh_list!
doc = Nokogiri::HTML(load_html(PROXY_PROVIDER_URL))
rows = doc.xpath('//table[@id="listable"]/tbody/tr')

@proxies = rows.map { |row| Proxy.new(row) }
end

alias_method :fetch!, :refresh_list!

# Pop just first proxy (and back it to the end of the proxy list)
def get
return if @proxies.empty?

first_proxy = @proxies.shift
@proxies << first_proxy

first_proxy
end

alias_method :pop, :get

# Pop first valid proxy (and back it to the end of the proxy list)
# Invalid proxies will be removed from the list
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

alias_method :pop!, :get!

# Clean current proxy list from dead proxies (doesn't respond by timeout)
def cleanup!
proxies.keep_if(&:connectable?)
end

alias_method :validate!, :cleanup!

# Just schema + host + port
def raw_proxies
proxies.map(&:url)
end

# No need to put all the attr_readers
def inspect
to_s
end

private

# Get HTML from the requested URL
def load_html(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
response = http.get(uri.request_uri)
response.body
end
end
end
88 changes: 88 additions & 0 deletions lib/proxy_fetcher/manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module ProxyFetcher
class Manager
PROXY_PROVIDER_URL = 'http://proxylist.hidemyass.com/'.freeze

class << self
def config
@config ||= ProxyFetcher::Configuration.new
end
end

attr_reader :proxies

# refresh: true - load proxy list from the remote server on initialization
# refresh: false - just initialize the class, proxy list will be empty ([])
def initialize(refresh: true)
if refresh
refresh_list!
else
@proxies = []
end
end

# Update current proxy list from the provider
def refresh_list!
doc = Nokogiri::HTML(load_html(PROXY_PROVIDER_URL))
rows = doc.xpath('//table[@id="listable"]/tbody/tr')

@proxies = rows.map { |row| Proxy.new(row) }
end

alias fetch! refresh_list!

# Pop just first proxy (and back it to the end of the proxy list)
def get
return if @proxies.empty?

first_proxy = @proxies.shift
@proxies << first_proxy

first_proxy
end

alias pop get

# Pop first valid proxy (and back it to the end of the proxy list)
# Invalid proxies will be removed from the list
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

alias pop! get!

# Clean current proxy list from dead proxies (doesn't respond by timeout)
def cleanup!
proxies.keep_if(&:connectable?)
end

alias validate! cleanup!

# Just schema + host + port
def raw_proxies
proxies.map(&:url)
end

# No need to put all the attr_readers
def inspect
to_s
end

private

# Get HTML from the requested URL
def load_html(url)
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
response = http.get(uri.request_uri)
response.body
end
end
end
2 changes: 1 addition & 1 deletion lib/proxy_fetcher/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def connectable?
false
end

alias_method :valid?, :connectable?
alias valid? connectable?

def http?
type.casecmp('http').zero?
Expand Down
1 change: 1 addition & 0 deletions spec/proxy_fetcher/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
it "not connectable if server doesn't respond to head" do
allow_any_instance_of(Net::HTTP).to receive(:start).and_return(false)
expect(proxy.connectable?).to be_falsey
expect(proxy.valid?).to be_falsey
end

it 'returns URI::Generic' do
Expand Down

0 comments on commit 00b3af0

Please sign in to comment.