Added method to save rate in database

This commit is contained in:
Luiz F Picolo 2021-10-07 20:39:06 -04:00
parent c3e9d4b408
commit 7e53d858ca

View File

@ -1,3 +1,6 @@
require 'speedtest'
require 'sinatra/activerecord'
class Rate < ActiveRecord::Base class Rate < ActiveRecord::Base
def self.get_download def self.get_download
all.collect { |p| [p.created_at.strftime('%d/%m %H:%M'), p.download] } all.collect { |p| [p.created_at.strftime('%d/%m %H:%M'), p.download] }
@ -6,4 +9,14 @@ class Rate < ActiveRecord::Base
def self.get_upload def self.get_upload
all.collect { |p| [p.created_at.strftime('%d/%m %H:%M'), p.upload] } all.collect { |p| [p.created_at.strftime('%d/%m %H:%M'), p.upload] }
end end
def self.save
test = Speedtest::Test.new(debug: false)
result = test.run
Rate.create({
download: result.pretty_download_rate,
upload: result.pretty_upload_rate,
})
end
end end