http_utilities 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 07be36a100e4a796a3a8b7fe3c6bf91ae8868be2
4
- data.tar.gz: 86c9ab65ecd84b8fa513a40a55a71ead7436a1ab
3
+ metadata.gz: b1c0b02b5b8356187e86ff1e3f45e5899247ee4c
4
+ data.tar.gz: 124fb89e7a57864d5ec402838bd41002e8fe3470
5
5
  SHA512:
6
- metadata.gz: 7112683b789905dd8ef5c996daf79ae2b16d47364822a197f9e8577d09a488d146d0e89591b6668e9217f3e80a465c9882566922f83fb20a178715c0bbca3f8e
7
- data.tar.gz: 3dceaebd2bc4eb906f20bc894f9f8ca42086a5c6a2eafe2c06a7c4bf6b0e00f42c77820d981b8f069740b4bf4d46a66c24764db418f83b607afb179e58d6dee8
6
+ metadata.gz: e26b5382bedec9b718580c74e5f74a7c3ab9f6ee7a25c4724f9e19a2953ba8a0062462dcee452f833673a8eda2af18d2a20ac9abb31f9b7d86be2198d52b31d0
7
+ data.tar.gz: 2b6fe44dc2398763d0de8c107755ce24344fda557ec3a88080934fda0d141445440e60b4658a9fdf1cc5807007d6385a076722f9faad43119aad8acf55ff8a50
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 1.3.5") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = "http_utilities"
6
- s.version = "1.0.6"
6
+ s.version = "1.0.7"
7
7
 
8
8
  s.authors = ["Sebastian Johnsson"]
9
9
  s.description = "Wrapper for common Http Libraries (Net:HTTP/Open URI/Curl)"
@@ -1,6 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  module HttpUtilities
3
- VERSION = "1.0.6"
3
+ VERSION = "1.0.7"
4
4
 
5
5
  require File.join(File.dirname(__FILE__), 'http_utilities/railtie') if defined?(Rails)
6
6
 
@@ -35,7 +35,7 @@ module HttpUtilities
35
35
  self.proxy = specific_proxy
36
36
 
37
37
  elsif (proxy_model_defined?)
38
- proxy_object = Proxy.get_random_proxy(self.proxy[:protocol], self.proxy[:type])
38
+ proxy_object = Proxy.get_random_proxy(protocol: self.proxy[:protocol], proxy_type: self.proxy[:type])
39
39
 
40
40
  #log(:info, "[HttpUtilities::Http::ProxySupport] - Randomized Proxy object: #{proxy_object.inspect}")
41
41
 
@@ -19,13 +19,19 @@ module HttpUtilities
19
19
  self.maximum_failed_attempts = 10
20
20
  end
21
21
 
22
- def check_and_update_proxies(protocol = :all, proxy_type = :all, mode = :synchronous)
23
- check_proxies(protocol, proxy_type, mode)
22
+ def check_and_update_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts)
23
+ check_proxies(protocol: protocol, proxy_type: proxy_type, mode: mode, maximum_failed_attempts: maximum_failed_attempts)
24
24
  update_proxies
25
25
  end
26
26
 
27
- def check_proxies(protocol = :all, proxy_type = :all, mode = :synchronous)
28
- proxies = Proxy.should_be_checked(protocol, proxy_type, Time.now, self.limit)
27
+ def check_proxies(protocol: :all, proxy_type: :all, mode: :synchronous, maximum_failed_attempts: self.maximum_failed_attempts)
28
+ proxies = Proxy.should_be_checked(
29
+ protocol: protocol,
30
+ proxy_type: proxy_type,
31
+ date: Time.now,
32
+ limit: self.limit,
33
+ maximum_failed_attempts: maximum_failed_attempts
34
+ )
29
35
 
30
36
  if (proxies && proxies.any?)
31
37
  Rails.logger.info "Found #{proxies.size} #{proxy_type} proxies to check."
@@ -62,6 +68,9 @@ module HttpUtilities
62
68
  client.write("#{test_query}\r\n")
63
69
  response = client.read
64
70
 
71
+ puts "SOCKS RESPONSE:\n"
72
+ puts response
73
+
65
74
  valid_proxy = (response && response.present?)
66
75
 
67
76
  rescue StandardError => e
@@ -8,40 +8,47 @@ module HttpUtilities
8
8
  end
9
9
 
10
10
  module ClassMethods
11
- def should_be_checked(protocol = :all, proxy_type = :all, date = Time.now, limit = 10)
12
- conditions = set_protocol_and_proxy_type_conditions(protocol, proxy_type)
13
- conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["(last_checked_at IS NULL OR last_checked_at < ?)", date])
14
- conditions << "failed_attempts <= 10"
15
- query = conditions.join(" AND ")
11
+ def should_be_checked(protocol: :all, proxy_type: :all, date: Time.now, limit: 10, maximum_failed_attempts: 10)
12
+ proxies = get_proxies_for_protocol_and_proxy_type(protocol, proxy_type)
13
+ proxies = proxies.where(["(last_checked_at IS NULL OR last_checked_at < ?)", date])
14
+ proxies = proxies.where(["failed_attempts <= ?", maximum_failed_attempts])
15
+ proxies = proxies.order("valid_proxy ASC, failed_attempts ASC, last_checked_at ASC")
16
+ proxies = proxies.limit(limit)
16
17
 
17
- where(query).order("valid_proxy ASC, failed_attempts ASC, last_checked_at ASC").limit(limit)
18
+ return proxies
18
19
  end
19
20
 
20
- def get_random_proxy(protocol = :all, proxy_type = :all)
21
- conditions = set_protocol_and_proxy_type_conditions(protocol, proxy_type)
22
- conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["valid_proxy = ?", true])
23
- conditions << "last_checked_at IS NOT NULL"
24
- query = conditions.join(" AND ")
21
+ def get_random_proxy(protocol: :all, proxy_type: :all, maximum_failed_attempts: nil)
22
+ proxies = get_proxies_for_protocol_and_proxy_type(protocol, proxy_type)
23
+ proxies = proxies.where(["valid_proxy = ? AND last_checked_at IS NOT NULL", true])
24
+ proxies = proxies.where(["failed_attempts <= ?", maximum_failed_attempts]) if maximum_failed_attempts
25
25
 
26
26
  order_clause = case ActiveRecord::Base.connection.class.name
27
- when "ActiveRecord::ConnectionAdapters::MysqlAdapter", "ActiveRecord::ConnectionAdapters::Mysql2Adapter" then "RAND() DESC"
28
- when "ActiveRecord::ConnectionAdapters::SQLite3Adapter" then "RANDOM() DESC"
27
+ when "ActiveRecord::ConnectionAdapters::MysqlAdapter", "ActiveRecord::ConnectionAdapters::Mysql2Adapter"
28
+ "RAND() DESC"
29
+ when "ActiveRecord::ConnectionAdapters::SQLite3Adapter"
30
+ "RANDOM() DESC"
31
+ else
32
+ "RAND() DESC"
29
33
  end
30
34
 
31
- proxy = nil
35
+ proxies = proxies.order(order_clause)
36
+
37
+ proxy = nil
32
38
 
33
39
  uncached do
34
- proxy = where(query).order(order_clause).limit(1).first
40
+ proxy = proxies.limit(1).first
35
41
  end
36
42
 
37
43
  return proxy
38
44
  end
39
45
 
40
- def set_protocol_and_proxy_type_conditions(protocol, proxy_type)
41
- conditions = []
42
- conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["protocol = ?", protocol]) if (protocol && !protocol.downcase.to_sym.eql?(:all))
43
- conditions << ActiveRecord::Base.send(:sanitize_sql_array, ["proxy_type = ?", proxy_type]) if (proxy_type && !proxy_type.downcase.to_sym.eql?(:all))
44
- return conditions
46
+ def get_proxies_for_protocol_and_proxy_type(protocol, proxy_type)
47
+ proxies = ::Proxy.where(nil)
48
+ proxies = proxies.where(protocol: protocol) if (protocol && !protocol.downcase.to_sym.eql?(:all))
49
+ proxies = proxies.where(proxy_type: proxy_type) if (proxy_type && !proxy_type.downcase.to_sym.eql?(:all))
50
+
51
+ return proxies
45
52
  end
46
53
 
47
54
  def format_proxy_address(proxy_host, proxy_port = 80, include_http = false)
@@ -7,13 +7,16 @@ namespace :http_utilities do
7
7
  seeder.seed
8
8
  end
9
9
 
10
- task :check_proxies, [:protocol, :proxy_type, :processing_method] => [:environment] do |task, args|
11
- protocol = (args.protocol) ? args.protocol.to_sym : :http
12
- proxy_type = (args.proxy_type) ? args.proxy_type.to_sym : :public
13
- processing_method = (args.processing_method) ? args.processing_method.to_sym : :synchronous
10
+ task :check_proxies, [:protocol, :proxy_type, :mode, :maximum_failed_attempts] => [:environment] do |task, args|
11
+ args.with_defaults(protocol: :http, proxy_type: :public, mode: :synchronous, maximum_failed_attempts: 10)
14
12
 
15
13
  proxy_checker = HttpUtilities::Proxies::ProxyChecker.new
16
- proxy_checker.check_and_update_proxies(protocol, proxy_type, processing_method)
14
+ proxy_checker.check_and_update_proxies(
15
+ protocol: args.protocol.to_sym,
16
+ proxy_type: args.proxy_type.to_sym,
17
+ mode: args.mode.to_sym,
18
+ maximum_failed_attempts: args.maximum_failed_attempts.to_i
19
+ )
17
20
  end
18
21
  end
19
22
  end
@@ -78,14 +78,14 @@ describe Proxy do
78
78
 
79
79
  [:http, :socks].each do |protocol|
80
80
  it "should return #{protocol.to_s} proxies that should be checked" do
81
- proxies = Proxy.should_be_checked(protocol)
81
+ proxies = Proxy.should_be_checked(protocol: protocol)
82
82
  proxies.should_not be_nil
83
83
  proxies.size.should == 3
84
84
  end
85
85
 
86
86
  [:public, :shared, :private].each do |proxy_type|
87
87
  it "should return #{proxy_type.to_s} #{protocol.to_s} proxies that should be checked" do
88
- proxies = Proxy.should_be_checked(protocol, proxy_type)
88
+ proxies = Proxy.should_be_checked(protocol: protocol, proxy_type: proxy_type)
89
89
  proxies.should_not be_nil
90
90
  proxies.size.should == 1
91
91
  end
@@ -94,14 +94,14 @@ describe Proxy do
94
94
 
95
95
  [:http, :socks].each do |protocol|
96
96
  it "should return a random #{protocol.to_s} proxy" do
97
- random_proxy = Proxy.get_random_proxy(protocol)
97
+ random_proxy = Proxy.get_random_proxy(protocol: protocol)
98
98
  random_proxy.should_not be_nil
99
99
  random_proxy.protocol.should == protocol.to_s
100
100
  end
101
101
 
102
102
  [:public, :shared, :private].each do |proxy_type|
103
103
  it "should return a random #{proxy_type.to_s} #{protocol.to_s} proxy" do
104
- random_proxy = Proxy.get_random_proxy(protocol, proxy_type)
104
+ random_proxy = Proxy.get_random_proxy(protocol: protocol, proxy_type: proxy_type)
105
105
  random_proxy.should_not be_nil
106
106
  random_proxy.protocol.should == protocol.to_s
107
107
  random_proxy.proxy_type.should == proxy_type.to_s
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: http_utilities
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sebastian Johnsson