-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedchecker.rb
56 lines (44 loc) · 1.03 KB
/
speedchecker.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
require 'speedtest'
require 'csv'
require 'pry'
require_relative 'mailer'
class Speedchecker
attr_reader :tester, :speedlog
DEFAULT_LOG = "#{__dir__}/log/speedlog.csv".freeze
def initialize(log = nil)
@tester = Speedtest::Test.new(
download_runs: 4,
upload_runs: 4,
ping_runs: 4,
download_sizes: [750, 1500],
upload_sizes: [10000, 400000],
debug: true
)
@speedlog = CSV.open(log || DEFAULT_LOG, 'ab')
end
def run
log_speed_test
notify_low_speed
end
private
def log_speed_test
time = Time.now.strftime('%H:%M')
speedlog << [Date.today.to_s, time, download_rate, upload_rate, latency]
end
def notify_low_speed
return unless download_rate.to_f < CONFIG['settings']['min_download_rate']
Mailer.new(download_rate, upload_rate, latency).send_mail
end
def result
@result ||= tester.run
end
def download_rate
result.pretty_download_rate
end
def upload_rate
result.pretty_upload_rate
end
def latency
result.latency
end
end