proxy_service 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +20 -6
- data/lib/proxy_service/mechanize_agent.rb +1 -0
- data/lib/proxy_service/version.rb +1 -1
- data/lib/proxy_service.rb +7 -5
- data/spec/lib/proxy_service_spec.rb +5 -3
- 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: 7fdfe384bfb35ffcb1e776646c6b209d7f783415
|
4
|
+
data.tar.gz: cae3bb440cf6fcebabc4e10914dde221dcf9906c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62e29c6ab4d4c00140f4bd4b4cd7d2589968cef81b86d95fe92feb52a1e221e73b2a7ab9577a7e4d8989add8d2fbf95475ef3d7f29e37f95e922f996a4b7d6a9
|
7
|
+
data.tar.gz: 8ebabc07fb86da716f1dc175449c71e87e9fe37681308229094a155e671cb8504a3dc89140fba4a0602c3fdd9a2295e1bde87919a85d73c65302a1cc5be42095
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ProxyService
|
2
2
|
|
3
|
-
|
3
|
+
A service class that rotates a list of proxies stored in a queue. The queueing system must support the STOMP protocol. Can be used with or without proxies. A queue name should be given and the queue should have a list of proxies in the format of `{ failures: 0, ip: '127.0.0.1', port: '80' }`. The service uses this info to set the proxy of a mechanize agent. If an exception occurs in the block, the proxy's `:failures` count is incremented and it's put back in the queue to be used again. Once the `:failures` count exceeds the `#failure_limit` it will be removed from the queue.
|
4
4
|
|
5
5
|
## Installation
|
6
6
|
|
@@ -17,11 +17,25 @@ Or install it yourself as:
|
|
17
17
|
$ gem install proxy_service
|
18
18
|
|
19
19
|
## Usage
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
20
|
+
Configure the service, for example, in an initializer
|
21
|
+
```ruby
|
22
|
+
ProxyService.configure do |config|
|
23
|
+
config.proxies_enabled = true
|
24
|
+
config.username = '...' # only used when proxies enabled
|
25
|
+
config.password = '...' # only used when proxies enabled
|
26
|
+
config.failure_limit = 3 # only used when proxies enabled
|
27
|
+
end
|
28
|
+
```
|
29
|
+
And then use it in your app
|
30
|
+
```ruby
|
31
|
+
ProxyService.new('queue_with_proxies').with_mechanize do |agent|
|
32
|
+
agent.get('http://...')
|
33
|
+
end
|
34
|
+
```
|
35
|
+
Some config settings can be overwritten on initialize
|
36
|
+
```ruby
|
37
|
+
ProxyService.new('queue_with_proxies', proxies_enabled: false)
|
38
|
+
```
|
25
39
|
## Contributing
|
26
40
|
|
27
41
|
1. Fork it ( https://github.com/ridiculous/proxy_service/fork )
|
@@ -14,6 +14,7 @@ class ProxyService::MechanizeAgent < DelegateClass(Mechanize)
|
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
|
+
# @note proxy.ip should only be nil in the case where it's held by a NullWorker
|
17
18
|
# @param [#ip, #port] proxy object that holds the ip and port
|
18
19
|
def set_proxy(proxy)
|
19
20
|
return unless proxy.ip
|
data/lib/proxy_service.rb
CHANGED
@@ -32,11 +32,13 @@ class ProxyService
|
|
32
32
|
agent.set_proxy(proxy)
|
33
33
|
yield agent
|
34
34
|
proxy.reset_failures
|
35
|
-
rescue
|
36
|
-
if
|
37
|
-
proxy.
|
38
|
-
|
39
|
-
|
35
|
+
rescue Mechanize::ResponseCodeError => e
|
36
|
+
if e.response_code == '403' # "forbidden"
|
37
|
+
if proxy.failures >= failure_limit
|
38
|
+
proxy.blocked!
|
39
|
+
else
|
40
|
+
proxy.increment_failures
|
41
|
+
end
|
40
42
|
end
|
41
43
|
ensure
|
42
44
|
proxy.release
|
@@ -43,24 +43,26 @@ describe ProxyService do
|
|
43
43
|
end
|
44
44
|
|
45
45
|
context 'when there is an exception' do
|
46
|
+
let(:mechanize_error) { Mechanize::ResponseCodeError.new(OpenStruct.new(code: '403')) }
|
47
|
+
|
46
48
|
it 'does not reset the failures count' do
|
47
49
|
expect(proxy).to_not receive(:reset_failures)
|
48
50
|
expect(proxy).to receive(:release)
|
49
|
-
subject.with_mechanize { |_| raise
|
51
|
+
subject.with_mechanize { |_| raise mechanize_error }
|
50
52
|
end
|
51
53
|
|
52
54
|
context 'when +failures+ exceeds max failures' do
|
53
55
|
it 'blocks the proxy' do
|
54
56
|
expect(proxy).to receive(:failures).and_return(subject.failure_limit + 1)
|
55
57
|
expect(proxy).to receive(:blocked!)
|
56
|
-
subject.with_mechanize { |_| raise
|
58
|
+
subject.with_mechanize { |_| raise mechanize_error }
|
57
59
|
end
|
58
60
|
end
|
59
61
|
|
60
62
|
context 'when +failures+ does not exceed max failures' do
|
61
63
|
it "increments the proxy's +failures+ count" do
|
62
64
|
expect(proxy).to receive(:increment_failures)
|
63
|
-
subject.with_mechanize { |_| raise
|
65
|
+
subject.with_mechanize { |_| raise mechanize_error }
|
64
66
|
end
|
65
67
|
end
|
66
68
|
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.1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Buckley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-01-22 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|