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

View File

@ -30,12 +30,26 @@ class Rate < ActiveRecord::Base
end end
def self.save def self.save
test = Speedtest::Test.new(debug: false) results = run_speedtest
result = test.run if results
Rate.create({
download: results[:download],
upload: results[:upload],
})
end
end
Rate.create({ def run_speedtest
download: result.pretty_download_rate, output = `speedtest`
upload: result.pretty_upload_rate, 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
end end