5
3
mirror of https://github.com/tildeverse/lobsters synced 2024-06-14 13:06:41 +00:00
tilde.news/extras/countinual.rb
2017-06-07 15:51:42 -05:00

35 lines
671 B
Ruby

require "socket"
class Countinual
cattr_accessor :API_KEY
# this needs to be overridden in config/initializers/production.rb
@@API_KEY = nil
COUNTINUAL_HOST = "170.130.139.180"
COUNTINUAL_PORT = 1025
def self.count!(counter, value, time = nil)
if !@@API_KEY
return
end
if time
time = time.to_i
else
time = Time.now.to_i
end
line = "#{@@API_KEY} #{counter} #{value} #{time}\n"
begin
sock = UDPSocket.open
sock.send(line, 0, COUNTINUAL_HOST, COUNTINUAL_PORT)
rescue => e
Rails.logger.info "Countinual error: #{e.message} (#{line.inspect})"
ensure
sock.close
end
end
end