proxy_manager 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fe7f910d9310e38469d40a4a72286f72217544da
4
+ data.tar.gz: 4567b6b40b3b556ab08eca47bd7f5f6df03aabce
5
+ SHA512:
6
+ metadata.gz: 3155dc554b0ce53f2758095a2ad5acdf1f14c64d25bf56ba97ae3432d566df34063945c84b497c6ec755012fac144defda9791210b11cb2d5694cf9e4cfe8027
7
+ data.tar.gz: 67efd91bad3b9d432ba8fd46c634e7776b2fe79fff1cb73f053ee9c766629f3fc962eea0327a2bfd083c951ce9fa4d9df8267cc25b6ae4deba4d2517e1ab371d
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --drb
2
+ --format Fuubar
3
+ --color
data/MIT-LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2014 Kirill Platonov
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = ProxyManager
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,20 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ProxyManager'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'rspec/core/rake_task'
18
+
19
+ RSpec::Core::RakeTask.new
20
+ task default: :spec
@@ -0,0 +1,94 @@
1
+ module ProxyManager
2
+ class Main
3
+ attr_reader :list, :bad_list, :list_file, :bad_list_file
4
+
5
+ def initialize(proxies, bad_proxies)
6
+ @list = []
7
+ @bad_list = []
8
+
9
+ if proxies.is_a? Array
10
+ load_list_from_array(proxies)
11
+ else
12
+ if proxies.empty? or bad_proxies.empty?
13
+ raise 'Both arguments "proxies" and "bad_proxies" required'
14
+ end
15
+
16
+ @list_file, @bad_list_file = proxies, bad_proxies
17
+
18
+ load_list_from_file(proxies)
19
+ end
20
+ end
21
+
22
+ def connectable?(proxy)
23
+ proxy = proxy.chomp.split(':') if proxy.is_a? String
24
+ Net::Ping::TCP.new(proxy[0], proxy[1].to_i).ping
25
+ end
26
+
27
+ def get(count = 1)
28
+ raise 'List is empty' if @list.empty?
29
+
30
+ items = []
31
+
32
+ @list.each_with_index do |proxy, key|
33
+ @list.delete_at(key)
34
+
35
+ if connectable? proxy
36
+ @list << proxy
37
+
38
+ if count == 1
39
+ items = proxy
40
+ break
41
+ else
42
+ items << proxy
43
+ break if items.size == count
44
+ end
45
+ else
46
+ @bad_list << proxy
47
+ end
48
+ end
49
+
50
+ raise 'There are no available proxy' if items.empty?
51
+
52
+ if @list_file && @bad_list_file
53
+ File.open(@list_file, "w+") do |f|
54
+ source = ''
55
+
56
+ @list.each_with_index do |p, index|
57
+ source << "#{p[0]}:#{p[1]}"
58
+ source << "\n" if @list[index + 1]
59
+ end
60
+
61
+ f.write(source)
62
+ end
63
+
64
+ File.open(@bad_list_file, "w+") do |f|
65
+ source = ''
66
+
67
+ @bad_list.each_with_index do |p, index|
68
+ source << "#{p[0]}:#{p[1]}"
69
+ source << "\n" if @bad_list[index + 1]
70
+ end
71
+
72
+ f.write(source)
73
+ end
74
+ end
75
+
76
+ items
77
+ end
78
+
79
+ private
80
+
81
+ def load_list_from_array(proxies)
82
+ @list = proxies.map { |arg| [arg.split(':')[0], arg.split(':')[1].to_i] }
83
+ end
84
+
85
+ def load_list_from_file(proxies)
86
+ File.open(proxies, "r").each do |line|
87
+ line = line.chomp.split(':')
88
+ if line[0].is_a? String and line[1].is_a? String
89
+ @list << [line[0], line[1].to_i]
90
+ end
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,12 @@
1
+ require 'proxy_manager/main'
2
+ require 'net/ping'
3
+
4
+ module ProxyManager
5
+ def self.root
6
+ File.expand_path '../..', __FILE__
7
+ end
8
+
9
+ def self.load(proxies, bad_proxies = nil)
10
+ Main.new(proxies, bad_proxies)
11
+ end
12
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :proxy_manager do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,76 @@
1
+ require 'spec_helper'
2
+ require 'proxy_manager'
3
+
4
+ describe ProxyManager do
5
+ it { expect(subject.root).not_to be_empty }
6
+
7
+ context 'proxies' do
8
+ let(:list) { [['0.0.1.0', 9000], ['0.0.0.1', 2000], ['0.0.0.0', 7000]] }
9
+ let(:proxies_file) { File.join(subject.root, 'spec', 'support', 'proxies.txt') }
10
+ let(:bad_proxies_file) { File.join(subject.root, 'spec', 'support', 'bad_proxies.txt') }
11
+ let(:proxy) { subject.load(['0.0.1.0:9000', '0.0.0.1:2000', '0.0.0.0:7000']) }
12
+
13
+ it 'should return list' do
14
+ expect(proxy.list).to match_array list
15
+ end
16
+
17
+ context '#connectable' do
18
+ it 'should response' do
19
+ expect(proxy).to respond_to(:connectable?)
20
+ end
21
+
22
+ it 'should receive array' do
23
+ expect(proxy.connectable?(['google.com', 80])).to be_true
24
+ end
25
+
26
+ it 'should receive string' do
27
+ expect(proxy.connectable?('google.com:80')).to be_true
28
+ end
29
+ end
30
+
31
+ context '#get' do
32
+ it 'should response' do
33
+ expect(proxy).to respond_to(:get)
34
+ end
35
+
36
+ it 'should return array' do
37
+ expect(proxy.get).to be_a Array
38
+ end
39
+
40
+ it 'should return many proxies' do
41
+ expect { proxy.get(3) }.not_to raise_error
42
+ end
43
+ end
44
+
45
+ it { expect(proxy.bad_list).to be_a Array }
46
+
47
+ context 'when load from file' do
48
+ let(:proxy) { subject.load(proxies_file, bad_proxies_file) }
49
+
50
+ it 'should return list' do
51
+ expect(proxy.list).to match_array list
52
+ end
53
+
54
+ context 'and save file' do
55
+ before { @source = File.read(proxies_file) }
56
+
57
+ it 'should update proxies source' do
58
+ proxy.get(2)
59
+
60
+ source = ''
61
+ proxy.list.each_with_index do |p, index|
62
+ source << "#{p[0]}:#{p[1]}"
63
+ source << "\n" if proxy.list[index + 1]
64
+ end
65
+
66
+ expect(File.open(proxies_file, "rb").read).to eq(source)
67
+ end
68
+
69
+ after do
70
+ File.open(proxies_file, 'w').write(@source)
71
+ File.open(bad_proxies_file, 'w').truncate(0)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,17 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
7
+ RSpec.configure do |config|
8
+ config.treat_symbols_as_metadata_keys_with_true_values = true
9
+ config.run_all_when_everything_filtered = true
10
+ config.filter_run :focus
11
+
12
+ # Run specs in random order to surface order dependencies. If you find an
13
+ # order dependency and want to debug it, you can fix the order by providing
14
+ # the seed, which is printed after each run.
15
+ # --seed 1234
16
+ config.order = 'random'
17
+ end
File without changes
@@ -0,0 +1,3 @@
1
+ 0.0.1.0:9000
2
+ 0.0.0.1:2000
3
+ 0.0.0.0:7000
metadata ADDED
@@ -0,0 +1,144 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proxy_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Kirill Platonov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: net-ping
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: turn
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: growl
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: fuubar
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: |2
98
+ This gem is for easy usage proxy in your parsers/web-bots. It will manage your proxy
99
+ list and check availability.
100
+ email:
101
+ - platonov.kd@gmail.com
102
+ executables: []
103
+ extensions: []
104
+ extra_rdoc_files: []
105
+ files:
106
+ - ".rspec"
107
+ - MIT-LICENSE
108
+ - README.rdoc
109
+ - Rakefile
110
+ - lib/proxy_manager.rb
111
+ - lib/proxy_manager/main.rb
112
+ - lib/tasks/proxy_manager_tasks.rake
113
+ - spec/proxy_manager/proxy_manager_spec.rb
114
+ - spec/spec_helper.rb
115
+ - spec/support/bad_proxies.txt
116
+ - spec/support/proxies.txt
117
+ homepage: https://github.com/bloodyhistory/proxy_manager
118
+ licenses: []
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Ruby proxy manager. Gem for easy usage proxy in parser/web bots.
140
+ test_files:
141
+ - spec/proxy_manager/proxy_manager_spec.rb
142
+ - spec/spec_helper.rb
143
+ - spec/support/bad_proxies.txt
144
+ - spec/support/proxies.txt