proxy_service 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/proxy_service.rb +16 -23
- data/lib/proxy_service/version.rb +1 -1
- data/spec/lib/proxy_service_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a3a873eeb05db5f539ad28b92d19e2f343a98fd2
|
4
|
+
data.tar.gz: 18356cdbf3b6110a40632160c052a1562caf88fa
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f9cbda9e51af75dc088696d110e55ac0516fc1d45d8b6aa494639b92f4f91d84f4195db2648ecb15d9f04118dc9031f5e59427f1c13394efbc63a3e94f7a549f
|
7
|
+
data.tar.gz: 02fd4b2a19cbcbfe584935d366197c572c2096502cb2036997fe7371067abf61c8fb5fdc2a64e98d797ad81d28e971db2fd0cdc8f79dddaad77c50ade4209c92
|
data/lib/proxy_service.rb
CHANGED
@@ -2,34 +2,27 @@ require 'json'
|
|
2
2
|
|
3
3
|
class ProxyService
|
4
4
|
|
5
|
-
POLL_PERIOD = 1
|
6
|
-
MAX_FAILURES = 3
|
7
|
-
|
8
5
|
class << self
|
9
|
-
attr_accessor :proxies_enabled, :username, :password
|
6
|
+
attr_accessor :proxies_enabled, :username, :password, :failure_limit
|
10
7
|
|
11
8
|
def configure
|
12
9
|
yield self
|
13
10
|
end
|
14
11
|
end
|
15
12
|
|
16
|
-
attr_accessor :source
|
13
|
+
attr_accessor :source, :failure_limit
|
17
14
|
attr_writer :proxies_enabled
|
18
15
|
|
19
16
|
# = Create a new proxy service with for a specific ODS
|
20
17
|
#
|
21
|
-
# @example
|
22
|
-
#
|
23
|
-
# ProxyService.new(:queue_name).with_mechanize do |agent|
|
24
|
-
# agent.get('...')
|
25
|
-
# end
|
26
|
-
#
|
27
18
|
# @param [String|Symbol] source name of the ODS (e.g. :tripadvisor), will look for a queue with that name "proxy/#{source}"
|
28
19
|
# @param [Hash] options
|
29
|
-
# @option options [Boolean] :proxies_enabled override the
|
20
|
+
# @option options [Boolean] :proxies_enabled override the class configuration
|
21
|
+
# @option options [Integer] :failure_limit before blocking the proxy
|
30
22
|
def initialize(source, options = {})
|
31
23
|
@source = source
|
32
24
|
@proxies_enabled = options.fetch(:proxies_enabled, !!self.class.proxies_enabled)
|
25
|
+
@failure_limit = options.fetch(:failure_limit, self.class.failure_limit || 3)
|
33
26
|
end
|
34
27
|
|
35
28
|
# @yield [agent] Passes a [proxied] Mechanize agent to the block
|
@@ -40,7 +33,7 @@ class ProxyService
|
|
40
33
|
yield agent
|
41
34
|
proxy.reset_failures
|
42
35
|
rescue
|
43
|
-
if proxy.failures >=
|
36
|
+
if proxy.failures >= failure_limit
|
44
37
|
proxy.blocked!
|
45
38
|
else
|
46
39
|
proxy.increment_failures
|
@@ -53,19 +46,11 @@ class ProxyService
|
|
53
46
|
# Private
|
54
47
|
#
|
55
48
|
|
56
|
-
def new_worker
|
57
|
-
if proxies_enabled?
|
58
|
-
Worker.new("proxy/#{source}")
|
59
|
-
else
|
60
|
-
NullWorker.new
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
49
|
def proxies_enabled?
|
65
50
|
@proxies_enabled
|
66
51
|
end
|
67
52
|
|
68
|
-
# =
|
53
|
+
# = Sleeps until the worker receives a proxy (message)
|
69
54
|
#
|
70
55
|
# @return [Proxy] a new proxy object
|
71
56
|
def reserve_proxy
|
@@ -75,10 +60,18 @@ class ProxyService
|
|
75
60
|
if worker.ready?
|
76
61
|
return Proxy.new(worker)
|
77
62
|
else
|
78
|
-
sleep(
|
63
|
+
sleep(1)
|
79
64
|
end
|
80
65
|
end
|
81
66
|
end
|
67
|
+
|
68
|
+
def new_worker
|
69
|
+
if proxies_enabled?
|
70
|
+
Worker.new("proxy/#{source}")
|
71
|
+
else
|
72
|
+
NullWorker.new
|
73
|
+
end
|
74
|
+
end
|
82
75
|
end
|
83
76
|
|
84
77
|
require 'proxy_service/mechanize_agent'
|
@@ -51,7 +51,7 @@ describe ProxyService do
|
|
51
51
|
|
52
52
|
context 'when +failures+ exceeds max failures' do
|
53
53
|
it 'blocks the proxy' do
|
54
|
-
expect(proxy).to receive(:failures).and_return(
|
54
|
+
expect(proxy).to receive(:failures).and_return(subject.failure_limit + 1)
|
55
55
|
expect(proxy).to receive(:blocked!)
|
56
56
|
subject.with_mechanize { |_| raise 'Testing exception' }
|
57
57
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: proxy_service
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Buckley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|