hidemyass 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,36 @@
1
+ module Hidemyass
2
+ module HTTP
3
+ def HTTP.start(address, opts = { :try_local => false }, *arg, &block)
4
+ Hidemyass.log 'Connecting to ' + address + ' from:'
5
+ response = nil
6
+
7
+ if opts[:try_local]
8
+ begin
9
+ Hidemyass.log 'localhost...'
10
+ response = Net::HTTP.start(address, *arg, &block)
11
+ Hidemyass.log response.class.to_s
12
+ if response.class.ancestors.include?(Net::HTTPSuccess)
13
+ return response
14
+ end
15
+ rescue *HTTP_ERRORS => error
16
+ Hidemyass.log :error, error
17
+ end
18
+ end
19
+
20
+ Hidemyass.proxies.each do |proxy|
21
+ begin
22
+ Hidemyass.log proxy[:host] + ':' + proxy[:port]
23
+ response = Net::HTTP::Proxy(proxy[:host], proxy[:port]).start(address, *arg, &block)
24
+ Hidemyass.log response.class.to_s
25
+ if response.class.ancestors.include?(Net::HTTPSuccess)
26
+ return response
27
+ end
28
+ rescue *HTTP_ERRORS => error
29
+ Hidemyass.log :error, error
30
+ end
31
+ end
32
+
33
+ response
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,19 @@
1
+ module Hidemyass
2
+ module Logger
3
+ def log(message)
4
+ logger.info("#{LOG_PREFIX} #{message}") if logging?
5
+ end
6
+
7
+ def logger #:nodoc:
8
+ @logger ||= options[:logger] || ::Logger.new(STDOUT)
9
+ end
10
+
11
+ def logger=(logger)
12
+ @logger = logger
13
+ end
14
+
15
+ def logging? #:nodoc:
16
+ options[:log]
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,3 @@
1
1
  module Hidemyass
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
data/lib/hidemyass.rb CHANGED
@@ -1,14 +1,17 @@
1
- require 'rubygems'
2
1
  require 'nokogiri'
3
2
  require 'open-uri'
4
3
  require 'net/http'
4
+ require 'logger'
5
5
 
6
- require "hidemyass/version"
6
+ require 'hidemyass/version'
7
+ require 'hidemyass/logger'
8
+ require 'hidemyass/http'
7
9
 
8
10
  module Hidemyass
11
+ extend Logger
9
12
 
10
13
  HOST = 'hidemyass.com'
11
- LOG_PREFIX = "** [hidemyass] "
14
+ LOG_PREFIX = '** [hidemyass] '
12
15
 
13
16
  HTTP_ERRORS = [Timeout::Error,
14
17
  Errno::EINVAL,
@@ -17,61 +20,21 @@ module Hidemyass
17
20
  Net::HTTPBadResponse,
18
21
  Net::HTTPHeaderSyntaxError,
19
22
  Net::ProtocolError]
20
-
21
- class << self
22
- attr_reader :host, :port, :first_without_proxy, :proxies
23
-
24
- def initialize(host = nil, port = nil, first_without_proxy = false, &block)
25
- @host = host
26
- @port = port
27
- @first_without_proxy = first_without_proxy
28
- end
29
-
30
- def proxies
31
- uri = URI.parse('http://%s/proxy-list/search-225729' % HOST)
32
- dom = Nokogiri::HTML(open(uri))
33
-
34
- @proxies ||= dom.xpath('//table[@id="listtable"]/tr').collect do |node|
35
- { port: node.at_xpath('td[3]').content.strip,
36
- host: node.at_xpath('td[2]/span').xpath('text() | *[not(contains(@style,"display:none"))]').
37
- map(&:content).compact.join.to_s }
38
- end
39
- end
40
-
41
- def request(host = @host, port = @port, first_without_proxy = @first_without_proxy)
42
- return unless host && port
43
-
44
- response = nil
45
- if first_without_proxy
46
- begin
47
- response = Net::HTTP.start(host, port, &block)
48
- rescue *HTTP_ERRORS => error
49
- log :error, error
50
- end
51
- end
52
-
53
- proxies.each do |proxy|
54
- begin
55
- response = Net::HTTP::Proxy(proxy[:host], proxy[:port]).start(host, port, &block)
56
- break if response == Net::HTTPSuccess
57
- rescue *HTTP_ERRORS => error
58
- log :error, error
59
- end
60
- end
61
-
62
- response
63
- end
64
-
65
- def logger
66
- if defined?(Rails.logger)
67
- Rails.logger
68
- elsif defined?(RAILS_DEFAULT_LOGGER)
69
- RAILS_DEFAULT_LOGGER
70
- end
71
- end
72
-
73
- def log(level, message, response = nil)
74
- logger.send level, LOG_PREFIX + message if logger
23
+
24
+ def self.options
25
+ @options ||= {
26
+ :log => true
27
+ }
28
+ end
29
+
30
+ def self.proxies
31
+ uri = URI.parse('http://%s/proxy-list/search-225729' % HOST)
32
+ dom = Nokogiri::HTML(open(uri))
33
+
34
+ @proxies ||= dom.xpath('//table[@id="listtable"]/tr').collect do |node|
35
+ { port: node.at_xpath('td[3]').content.strip,
36
+ host: node.at_xpath('td[2]/span').xpath('text() | *[not(contains(@style,"display:none"))]').
37
+ map(&:content).compact.join.to_s }
75
38
  end
76
39
  end
77
40
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hidemyass
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-14 00:00:00.000000000 Z
12
+ date: 2012-04-15 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: nokogiri
@@ -41,6 +41,8 @@ files:
41
41
  - Rakefile
42
42
  - hidemyass.gemspec
43
43
  - lib/hidemyass.rb
44
+ - lib/hidemyass/http.rb
45
+ - lib/hidemyass/logger.rb
44
46
  - lib/hidemyass/version.rb
45
47
  homepage: http://github.com/jassa/hidemyass
46
48
  licenses: []