-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyzer_url.rb
88 lines (72 loc) · 2.09 KB
/
analyzer_url.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'net/http'
class UrlAnalyzer
attr_accessor :bad_urls, :good_urls
GREEN = ['100', '101', '102', '200', '201', '202', '203', '204', '205', '206',
'207', '226', '300', '301', '302', '303', '304', '305', '306', '307' ]
RED = [ '400', '401', '402', '403', '404', '405', '406' '406', '407', '408',
'409', '410', '411', '412', '413', '414', '415', '416', '417', '422',
'423', '424', '425', '426', '428', '429', '431', '434', '444', '449',
'451', '500', '501', '502', '503', '504', '505', '506', '507', '508',
'509', '510', '511', 'DSL_ERROR' ]
def initialize(urls)
@bad_urls = Array.new
@good_urls = Array.new
urls.split.each do |u|
@bad_urls << u if bad_url?(u)
@good_urls << u if good_url?(u)
end
end
def start_monitoring
threads = []
threads << Thread.new do
loop do
monitor_bad_urls
puts 'bad urls checked'
sleep 30
end
end
threads << Thread.new do
loop do
monitor_good_urls
puts 'good urls checked'
sleep 60
end
end
threads.each(&:join)
end
private
def check_url(url)
Net::HTTP.get_response(URI(url)).code
rescue SocketError, Errno::ECONNREFUSED
return 'DSL_ERROR'
end
def monitor_bad_urls
changed_state = select_good_urls(bad_urls)
unless changed_state.empty?
good_urls << changed_state
puts 'These urls became fine:', changed_state.join(',')
end
end
def monitor_good_urls
changed_state = select_bad_urls(good_urls)
unless changed_state.empty?
bad_urls << changed_state
puts 'These urls felt down:', changed_state.join(',')
end
end
def select_bad_urls(urls)
urls.select { |u| RED.include?(check_url(u)) }
end
def select_good_urls(urls)
urls.select { |u| GREEN.include?(check_url(u)) }
end
def bad_url?(url)
RED.include?(check_url(url))
end
def good_url?(url)
GREEN.include?(check_url(url))
end
end
puts "введети ссылки через пробел: "
text = gets.chomp
UrlAnalyzer.new("#{text}").start_monitoring