Refactor Speedtest Data Saving Logic
This commit is contained in:
parent
6a246665f4
commit
5c26cf91bc
@ -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({
|
Rate.create({
|
||||||
download: result.pretty_download_rate,
|
download: results[:download],
|
||||||
upload: result.pretty_upload_rate,
|
upload: results[:upload],
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
end
|
Loading…
Reference in New Issue
Block a user