Refactor Speedtest Data Saving Logic

This commit is contained in:
Luiz F Picolo 2023-09-11 22:26:21 -04:00
parent 6a246665f4
commit 5c26cf91bc
1 changed files with 21 additions and 7 deletions

View File

@ -30,12 +30,26 @@ class Rate < ActiveRecord::Base
end
def self.save
test = Speedtest::Test.new(debug: false)
result = test.run
results = run_speedtest
if results
Rate.create({
download: results[:download],
upload: results[:upload],
})
end
end
Rate.create({
download: result.pretty_download_rate,
upload: result.pretty_upload_rate,
})
def run_speedtest
output = `speedtest`
if $?.success?
download_speed = output.match(/Download:\s+(\d+\.\d+)\sMbps/)[1]
upload_speed = output.match(/Upload:\s+(\d+\.\d+)\sMbps/)[1]
{
download: download_speed.to_f,
upload: upload_speed.to_f
}
else
nil
end
end
end