# Improving Code

**URL:** <https://www.ruby-forum.com/t/improving-code/133127>\
**Category:** Ruby\
**Created:** [March 28, 2008, 4:22am UTC](https://www.ruby-forum.com/t/improving-code/133127 "2008-03-28T04:22:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![eder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@eder](https://www.ruby-forum.com/u/eder)\
**Post date:** [March 28, 2008, 4:22am UTC](https://www.ruby-forum.com/t/improving-code/133127/1 "2008-03-28T04:22:16Z")

</div>

Question

→ How would you improve this code

- The Error Handling sucks
- Bad Usage of Net::HTTP
- Bad Usage of Classes and Hierachy
- etc

Thanks in advance.

require ‘rubygems’  
require ‘hpricot’

require ‘net/http’  
class ProxyList \< Net::HTTP

```
URL = 'www.samair.ru'
FILE = File.dirname( __FILE__ ) + '/proxylist.dat'
IPADDR_AT = 4

@port_values = {}
@proxylist = []

def self.random_useragent

```

USER\_AGENT[rand(USER\_AGENT.size - 1)]  
end

```
def self.strip_html(str)

```

str.gsub(/\</?[^\>]\*\>/, ‘’)  
end

```
def self.retreive_proxylist(n = 10)

```

begin  
puts “Retreiving proxylist from server: #{URL}”  
http = self.new(URL, self.default\_port)  
begin  
@port\_values.clear if @port\_values.length \> 0  
file = File.new(FILE, ‘w’)  
1.upto(n) do |i|  
page = “/proxy/proxy-%02d.htm” % i  
puts “Retreiving #{URL}#{page} from #{URL}”

```
    response = http.get2(page)
    document = Hpricot(response.body)
    # Hidden Keys?
    script =

```

document.search(‘//head/script[@type=“text/javascript”]’)  
script.each do |element|  
str = element.inner\_html  
# /([\_a-zA-Z][\_a-zA-Z]_)(?:\s_=\s\*([^$]+))?;/  
str.gsub(/([^;]+)/) {  
|match|  
var, val = \*(match.split(/=/)[0, 2])  
@port\_values[var.to\_sym] = val  
}  
end  
# Proxies  
td = document.search(‘[//table.tablelist/tr/td](https://table.tablelist/tr/td)’)  
td.each\_with\_index do |element, index|  
# Hijos de la riviera maya  
if index % IPADDR\_AT == 0 then  
# wtf!!!  
s = element.inner\_html  
ip, port = \*(s.split(/\<script/, 2)[0, 2])

```
      next if port == nil

      port = port.scan(/(?:\+[a-z])+/).to_s
      port.gsub!('+', '')
      ip_port = ""
      port.each_byte {
    |c|
    begin
        ip_port << @port_values[c.chr.to_sym]
    rescue TypeError => e
        # do nothing
    end
      }
      @proxylist << "#{ip}:#{ip_port}"
  end
    end
end
@proxylist.each do |proxy|
    file.write(proxy + "\r\n")
end
  rescue Exception => e
puts "[file=#{e.inspect}]"
  ensure
file.close
  end

```

rescue Exception =\> e  
puts “[http=#{e.inspect}]”  
end  
end

end

---

<div class="post-metadata">

**Author:** ![eder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/a698b9/32.png) [@eder](https://www.ruby-forum.com/u/eder)\
**Post date:** [March 28, 2008, 4:28am UTC](https://www.ruby-forum.com/t/improving-code/133127/2 "2008-03-28T04:28:07Z")

</div>

Oops, sorry: [http://paste-it.net/7646](http://paste-it.net/7646)
